国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

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

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

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

問題描述

例如此列表:

[[’software’, ’foundation’], [’of’, ’the’], [’the’, ’python’], [’software’, ’foundation’],[’of’, ’the’], [’software’, ’foundation’]]# 進(jìn)行頻率統(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))]

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 灵武市| 班戈县| 普定县| 锡林浩特市| 车致| 尼玛县| 道真| 包头市| 平和县| 长寿区| 红河县| 自治县| 兴安盟| 海晏县| 白玉县| 昌江| 克拉玛依市| 开平市| 宁国市| 马公市| 大城县| 慈利县| 桑日县| 双鸭山市| 晴隆县| 普兰县| 盐亭县| 宜兰市| 丹寨县| 工布江达县| 昂仁县| 巴中市| 蕉岭县| 来凤县| 万年县| 健康| 广东省| 内丘县| 内江市| 紫阳县| 苍山县|