排序:

1:整理顺序

#冒泡
lista = [5,7,11,19,99,63,3,9,1]
list = []
while lista != []:
number = 0
for i in lista:
if number < i:
number = i
lista.remove(number)
list.append(number)
print(list)
打印结果:
D:\untitled\1\venv\Scripts\python.exe D:/untitled/1/venv/main.py
[99, 63, 19, 11, 9, 7, 5, 3, 1] Process finished with exit code 0

#选择
lista = [5,7,11,19,99,63,3,9,1]
list = []
while lista != []:
for i in lista:
number = 1
for j in lista:
if number < j:
number = j
lista.remove(number)
list.append(number)
print(list)
打印结果:
D:\untitled\1\venv\Scripts\python.exe D:/untitled/1/venv/main.py
[99, 63, 19, 11, 9, 7, 5, 3, 1] Process finished with exit code 0

2:sort()函数

#sort函数
lista =[5,7,11,19,99,63,3,9,1]
lista.sort()
print(lista)
打印结果:
D:\untitled\1\venv\Scripts\python.exe D:/untitled/1/venv/main.py
[1, 3, 5, 7, 9, 11, 19, 63, 99] Process finished with exit code 0

3:加上一个数

#若a=18,在原来的list中按顺序加上这个个数
list = [1,4,7,9,11,14,19,34,60,79,98]
print("前:%s" %list)
a = 18
list.append(a)
list_1 = []
while list != []:
number = 100
for i in list:
if i < number:
number = i
list.remove(number)
list_1.append(number)
print("后:%s" %list_1)
打印结果:
D:\untitled\1\venv\Scripts\python.exe D:/untitled/1/venv/main.py
前:[1, 4, 7, 9, 11, 14, 19, 34, 60, 79, 98]
后:[1, 4, 7, 9, 11, 14, 18, 19, 34, 60, 79, 98] Process finished with exit code 0

实例:

3*3表

#3*3表
listx = [1,2,3,]
for i in listx:
for j in listx:
if i >= j:
x = i*j
print("%s*%s=%s" %(i,j,x) ,end=" ")
print()
打印结果:
D:\untitled\1\venv\Scripts\python.exe D:/untitled/1/venv/main.py
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9 Process finished with exit code 0

列表反向打印:list_num = ["1","2","3"]打印顺序:3,2,1

使用len()函数

#反向列表(len() 方法返回对象(字符、列表、元组等)长度或项目个数)
list_num = ["","",""]
i = len(list_num)
print(list_num[i-1::-1])
打印结果:
D:\untitled\1\venv\Scripts\python.exe D:/untitled/1/venv/main.py
['', '', ''] Process finished with exit code 0

使用reverse()函数

list_num = ["","",""]
list_num.reverse()
print(list_num)
打印结果:
D:\untitled\1\venv\Scripts\python.exe D:/untitled/1/venv/main.py
['', '', ''] Process finished with exit code 0

使用for语句

a = [1,2,3,4,5,6]
num = len(a)
for i in range(int(num/2)):
a[i],a[num -i -1 ] = a[num-i-1],a[i]
print(a)
D:\untitled\1\venv\Scripts\python.exe D:/untitled/1/venv/main.py
[6, 5, 4, 3, 2, 1] Process finished with exit code 0

小知识:

list_num = ["","",""]
for i in list_num[::-1]:#从后往前取值
print(i)
打印结果:
D:\untitled\1\venv\Scripts\python.exe D:/untitled/1/venv/main.py
3
2
1 Process finished with exit code 0

最新文章

  1. [LeetCode] Restore IP Addresses 复原IP地址
  2. Android深度探索--HAL与驱动开发----第五章读书笔记
  3. 第二次作业:Github的优点和缺点
  4. Android 隐式意图和显示意图的使用场景
  5. bzoj1104: [POI2007]洪水pow
  6. CF460B Little Dima and Equation (水题?
  7. view
  8. memcache的一致性hash算法使用
  9. UnityShader快速上手指南(三)
  10. 「python」: arp脚本的两种方法
  11. MEMO:UIButton 中的图片和标题 左对齐
  12. java统计英文字母、空格、数字和其它字符的数目
  13. vue-router项目实战总结
  14. Java 数组及数组常用算法
  15. [Day7]循环、数组方法、排序查找
  16. 【洛谷P1052【NOIP2005提高T2】】过河
  17. E - 487--3279
  18. 在Mockplus中,如何做鼠标悬停时菜单下拉的效果?
  19. 如何设置Windows server 2008 R2登陆密码?
  20. POJ 3762 The Bonus Salary!(最小K覆盖)

热门文章

  1. test20190308
  2. 如何将常规的web 应用程序转化为云上多租户 SaaS 解决方案
  3. Create A .NET Core Development Environment Using Visual Studio Code
  4. Open Asset Import Library(assimp) vs2010编译
  5. winform 多线程中ShowDialog()无效的解决办法
  6. Junit+ant+JaCoCo集成使用
  7. 一些IPC常用头文件
  8. 【转】VC 线程间通信的三种方式
  9. 监控mysql状态并发送Email
  10. SQL中利用脚本恢复数据库