sorted()函数:对所有可迭代的对象进行排序操作。
sort 是应用在 list 上的方法,sorted 可以对所有可迭代的对象进行排序操作。
list 的 sort 方法返回的是对已经存在的列表进行操作,而内建函数 sorted 方法返回的是一个新的 list,而不是在原来的基础上进行的操作。
sorted(iterable, key=None, reverse=False)
返回重新排序的列表。
# 对字符串排序 s1 = ‘138297456‘ print(sorted(s1)) # [‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘] # 对列表排序 l1 = [‘a‘, ‘c‘, ‘b‘, ‘e‘, ‘d‘] print(sorted(l1)) # [‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘]
原文:https://www.cnblogs.com/yujiemeigui/p/14612010.html