sort()
官方文档:
sort()
https://www.runoob.com/python3/python3-att-list-sort.html
sorted()
https://www.runoob.com/python3/python3-func-sorted.html
一、sort 与 sorted 区别:
sort 是应用在 list 上的方法,sorted 可以对所有可迭代的对象进行排序操作。
list 的 sort 方法返回的是对已经存在的列表进行操作,而内建函数 sorted 方法返回的是一个新的 list,而不是在原来的基础上进行的操作。
有点绕,总的来说是str.sort()改变原字符串,sorted()返回一个新的排序后的结果。
示例:
def how_sort_run():
listA = [('h', 4), ('e', 2), ('!', 5), ('l', 3), ('o', 1)]
listB = [('h', 4), ('e', 2), ('!', 5), ('l', 3), ('o', 1)]
print(sorted(listA, ke