美文网首页
字典排序及列表查数

字典排序及列表查数

作者: 玫瑰哥哥 | 来源:发表于2018-09-20 16:38 被阅读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)可转为正常字典

相关文章

网友评论

      本文标题:字典排序及列表查数

      本文链接:https://www.haomeiwen.com/subject/fbennftx.html