字典排序及列表查数

只有第一个参数用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)可转为正常字典

你可能感兴趣的:(字典排序及列表查数)