久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術(shù)文章
文章詳情頁

python - 如何對列表中的列表進行頻率統(tǒng)計?

瀏覽:211日期:2022-06-30 16:46:10

問題描述

例如此列表:

[[’software’, ’foundation’], [’of’, ’the’], [’the’, ’python’], [’software’, ’foundation’],[’of’, ’the’], [’software’, ’foundation’]]# 進行頻率統(tǒng)計,例如輸出結(jié)果為:('[’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) # 以字典形式返回統(tǒng)計結(jié)果print Counter(str(i) for i in a).items() # 以列表形式返回統(tǒng)計結(jié)果# -------------- map方法 --------print Counter(map(str, a)) # 以字典形式返回統(tǒng)計結(jié)果print Counter(map(str, a)).items() # 以列表形式返回統(tǒng)計結(jié)果回答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))]

標簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 新竹市| 上虞市| 麻江县| 油尖旺区| 和龙市| 迭部县| 淄博市| 万源市| 随州市| 丰原市| 固阳县| 岳普湖县| 温泉县| 通渭县| 永州市| 体育| 华亭县| 延长县| 黄梅县| 卢湾区| 沙坪坝区| 宁津县| 金昌市| 高邑县| 花垣县| 类乌齐县| 瑞金市| 通山县| 盖州市| 新密市| 即墨市| 宜兴市| 阿合奇县| 玉林市| 鲁甸县| 新化县| 库伦旗| 张家川| 曲靖市| 龙山县| 华池县|