python - 如何对列表中的列表进行频率统计?
问题描述
例如此列表:
[[’software’, ’foundation’], [’of’, ’the’], [’the’, ’python’], [’software’, ’foundation’],[’of’, ’the’], [’software’, ’foundation’]]# 进行频率统计,例如输出结果为:('[’software’,’foundation’]', 3), ('[’of’, ’the’]', 2), ('[’the’, ’python’]', 1)
问题解答
回答1:# coding:utf8from collections import Countera = [[’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() # 以列表形式返回统计结果回答2:
from collections import Counterdata = [[’software’, ’foundation’], [’of’, ’the’], [’the’, ’python’], [’software’, ’foundation’],[’of’, ’the’], [’software’, ’foundation’]]cnt = Counter(map(tuple, data))print(list(cnt.items()))回答3:
from itertools import groupbydata = ....print [(k, len(list(g)))for k, g in groupby(sorted(data))]
相关文章:
1. docker 17.03 怎么配置 registry mirror ?2. docker 下面创建的IMAGE 他们的 ID 一样?这个是怎么回事????3. mac连接阿里云docker集群,已经卡了2天了,求问?4. 上传图片老是失败是什么原因?SAE_TMP_PATH.后面跟的路径在哪看5. vue 子组件watch监听不到prop的解决6. mobile-web-design - html5 touchmove 怎么获取经过的元素?7. html5 - weex H5端的使用,怎么跑起来?8. java - Web开发 - POI导出带有下拉框的Excel和解决下拉中数组过多而产生的异常9. 为啥总显示密码错误10. 微信订阅号如何用渠道二维码统计地推效果?
