Maison  >  Questions et réponses  >  le corps du texte

python - Comment faire des statistiques de fréquence sur une liste dans une liste?

Par exemple cette liste :

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


# 进行频率统计,例如输出结果为:
("['software','foundation']", 3), ("['of', 'the']", 2), ("['the', 'python']", 1)
仅有的幸福仅有的幸福2711 Il y a quelques jours621

répondre à tous(3)je répondrai

  • 漂亮男人

    漂亮男人2017-05-18 11:04:05

    # 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()  # 以列表形式返回统计结果

    répondre
    0
  • 伊谢尔伦

    伊谢尔伦2017-05-18 11:04:05

    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()))

    répondre
    0
  • 習慣沉默

    習慣沉默2017-05-18 11:04:05

    from itertools import groupby
    data = ....
    print [(k, len(list(g)))for k, g in groupby(sorted(data))]

    répondre
    0
  • Annulerrépondre