参考资料:
list.sort()
sorted()
sortinghowtodo
python3中移除了cmp
从可迭代的项目中返回新的排序列表。
无返回值,改变原list
random = [(2, 2), (3, 4), (4, 1), (1, 3)]
random.sort(key=lambda x:x[1])
print(random)//[(4, 1), (2, 2), (1, 3), (3, 4)]
random.sort(key=lambda x:x[1],reverse=True)
print(random)//[(3, 4), (1, 3), (2, 2), (4, 1)]
random = [(2, 2), (3, 4), (4, 1), (1, 3)]
print(sorted(random,key=lambda x:x[1]))//[(4, 1), (2, 2), (1, 3), (3, 4)]
print(random)//[(2, 2), (3, 4), (4, 1), (1, 3)]
sorted(list)排序完生成一个新的list,原始list并未改变