对列表排序的方法
- 使用a.sort()方法
- 使用a = sorted(b)方法
a = [1,7,4,2,45,3]
a.sort()
print('a:',a)
b = [1,7,4,2,45,3]
x = sorted(b)
print('b:',x)

列表Sort方法与sorted方法的区别
- sort是改变列表本身,是属于列表
- sorted是一个函数,创建一个列表副本,不改变列表本身
b = [1,7,4,2,45,3]
x = sorted(b)
print('b:',x)
print(b ==x ) # False

如何倒序排列一个列表
a = [1,7,4,2,45,3]
a.sort(reverse=True)
b = [1,7,4,2,45,3]
c = sorted(b,reverse=True)
print('a:',a)
print('c:',c)

总结
1.使用sort,sorted都可以对列表进行排序,使用方法a.sort() sorted(a)
2.sort方法是修改列表本身,而sorted函数是返回一个列表副本
3.将reverse=True 参数使用以后,可以对列表进行倒序排列
加油2020-2-25
网友评论