如何对列表中的列表进行频率统计?

例如此列表:

[['software', 'foundation'], ['of', 'the'], ['the', 'python'], ['software', 'foundation'],['of', 'the'], ['software', 'foundation']]


# 进行频率统计,例如输出结果为:
("['software','foundation']", 3), ("['of', 'the']", 2), ("['the', 'python']", 1)
阅读 4.4k
3 个回答
# 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()  # 以列表形式返回统计结果
from collections import Counter
data = [['software', 'foundation'], ['of', 'the'], ['the', 'python'], ['software', 'foundation'],['of', 'the'], ['software', 'foundation']]
cnt = Counter(map(tuple, data))
print(list(cnt.items()))
from itertools import groupby
data = ....
print [(k, len(list(g)))for k, g in groupby(sorted(data))]
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题