Jamie's Blog

Talk is cheap, show me the code.

How to sort value in dict

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
#第一种方法 sorted
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
#第二种方法 zip+sorted

n = data.keys()
s = data.values()
i = data.items()
dict_items([('x', 74), ('y', 90), ('z', 64), ('a', 79), ('b', 79), ('c', 99)])
1
zip(s,n)
<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')]
1
sorted(l)
[(64, 'z'), (74, 'x'), (79, 'a'), (79, 'b'), (90, 'y'), (99, 'c')]

Proudly powered by Hexo and Theme by Hacker
© 2019 Jamie