python list 中元素的统计与排序

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python list 中元素的统计与排序相关的知识,希望对你有一定的参考价值。

1.  用count和dict.  dict的存储是散乱的, 不方面打印. 

2. 用sorted.  注意, 得到的是一个元组list, 而不再是dict.

 

dict_x = {}
for item in list_all:
    dict_x[item] = list_all.count(item)

sorted_x = sorted(dict_x.items(), key=operator.itemgetter(1), reverse=True)

for k, v in sorted_x:
    print k, v

 

also can refer to:

http://www.saltycrane.com/blog/2007/09/how-to-sort-python-dictionary-by-keys/

http://stackoverflow.com/questions/613183/sort-a-python-dictionary-by-value

 

以上是关于python list 中元素的统计与排序的主要内容,如果未能解决你的问题,请参考以下文章