python sorted()因变量

python sorted()因变量

sorted()因变量是对可迭代东西举行排序操纵,且获得的截止为介入排序的元素构成的列表。

l1 = [1, 3, 4, 2, 5, 7, 6, 8, 0, 9]

# 对列表中的元素举行排序

print(sorted(l1))

'[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]'

print(sorted(l1, reverse=True))

'[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]'

# 按字典值的长度举行排序

d1 = {'a': 'aa', 'b': 'b', 'c': 'ccc'}

print(sorted(d1.items(), key=lambda x: len(x[1])))

"[('b', 'b'), ('a', 'aa'), ('c', 'ccc')]"

分享到 :

Leave a Reply

Your email address will not be published. Required fields are marked *