字典排序及列表查数

2018-09-20  本文已影响0人  玫瑰哥哥

只有第一个参数用items()才能赋给一个新字典

#对key排序

dict = sorted(dict.items(), key = lambda x:x[0], reverse = False)    #reverse = False为升序

#对value排序

dict = sorted(dict.items(), key = lambda x:x[1])


l = [11,11,12,12,432,32,43,33,33,43,12,22,432,21,33,43]

from collections import Counter

r = Counter(l)

print (r)

#Counter({11: 2, 12: 3, 432: 2, 32: 1, 43: 3, 33: 3, 22: 1, 21: 1})

#之后直接dict(r)可转为正常字典

上一篇 下一篇

猜你喜欢

热点阅读