python - 如何对列表中的列表进行频率统计?
本文介绍了python - 如何对列表中的列表进行频率统计?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
例如此列表:
[['software', 'foundation'], ['of', 'the'], ['the', 'python'], ['software', 'foundation'],['of', 'the'], ['software', 'foundation']]
# 进行频率统计,例如输出结果为:
("['software','foundation']", 3), ("['of', 'the']", 2), ("['the', 'python']", 1)
解决方案
# coding:utf8
from collections import Counter
a = [['software', 'foundation'], ['of', 'the'], ['the', 'python'], ['software', 'foundation'],['of', 'the'], ['software', 'foundation']]
print Counter(str(i) for i in a) # 以字典形式返回统计结果
print Counter(str(i) for i in a).items() # 以列表形式返回统计结果
# -------------- map方法 --------
print Counter(map(str, a)) # 以字典形式返回统计结果
print Counter(map(str, a)).items() # 以列表形式返回统计结果
这篇关于python - 如何对列表中的列表进行频率统计?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文