sort 和sorted的 区别

sort(cmp=None, key=None, reverse=False)
sorted(iterable, cmp=None, key=None, reverse=False)
 
 
sort是容器的函数,用List的成员函数sort进行排序
sorted是Python的内建函数相同的参数,用built-in函数sorted进行排序
sorted(iterable,key=None,reverse=False),返回新的列表,对所有可迭代的对象均有效
sort(key=None,reverse=False) 就地改变列表 reverse:True反序;False 正序
1 a = [1,2,3,5,2,6,8,0]
2 print(a.sort())# None
3 print(a)# [0, 1, 2, 2, 3, 5, 6, 8]
4 a = [1,2,3,5,2,6,8,0]
5 print(sorted(a)) # [0, 1, 2, 2, 3, 5, 6, 8]
6 
7 print(a) # [1, 2, 3, 5, 2, 6, 8, 0]

猜你喜欢

转载自www.cnblogs.com/JerryZao/p/9433026.html