python_01_list_structure
sort && sorted
sort 作用于list,返回None,对list本身进行排序
sorted 作用于list,返回一个排序好的列表,原列表顺序不作处理;(PS:sorted 作用于可迭代对象,都生成一个排序好的列表)
>>> l=[1,2,3,5,6,7,6,5,4,3,2] >>> l.sort() >>> l [1, 2, 2, 3, 3, 4, 5, 5, 6, 6, 7] >>> k=[1,2,3,4,5,6,7,8,7,6,5,4,3,2,1] >>> k [1, 2, 3, 4, 5, 6, 7, 8, 7, 6, 5, 4, 3, 2, 1] >>> sorted(k) [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8] >>> k [1, 2, 3, 4, 5, 6, 7, 8, 7, 6, 5, 4, 3, 2, 1]