1 2 3 4 5 6
| from random import randint
data = {x: randint(60,100) for x in 'xyzabc'} print(data)
sorted(data)
|
{'x': 74, 'y': 90, 'z': 64, 'a': 79, 'b': 79, 'c': 99}
['a', 'b', 'c', 'x', 'y', 'z']
第一种方法 sorted
1 2 3
| v = sorted(data.items(),key = lambda x:x[1]) v
|
[('z', 64), ('x', 74), ('a', 79), ('b', 79), ('y', 90), ('c', 99)]
第二种方法 zip+sorted
1 2 3 4 5
|
n = data.keys() s = data.values() i = data.items()
|
dict_items([('x', 74), ('y', 90), ('z', 64), ('a', 79), ('b', 79), ('c', 99)])
<zip at 0x10d40bf08>
1 2
| l = list(zip(data.values(),data.keys())) l
|
[(74, 'x'), (90, 'y'), (64, 'z'), (79, 'a'), (79, 'b'), (99, 'c')]
[(64, 'z'), (74, 'x'), (79, 'a'), (79, 'b'), (90, 'y'), (99, 'c')]