Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

例如此列表:

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


# 进行频率统计,例如输出结果为:
("['software','foundation']", 3), ("['of', 'the']", 2), ("['the', 'python']", 1)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
373 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...