collections.Counter类接收任意由可哈希(hashable)元素构成的序列对象。在底层实现上一个Counter对象就是一个字典,将元素映射在其出现的次数上,十分适合统计序列元素数量:

    1. from collections import Counter
    2. words = [
    3. "look", "into", "my", "eyes", "look", "into", "my", "eyes",
    4. "the", "eyes", "the", "eyes", "the", "eyes", "not", "around", "the",
    5. "eyes", "don't", "look", "around", "the", "eyes", "look", "into",
    6. "my", "eyes", "you're", "under"
    7. ]
    8. word_counts = Counter(words)
    9. # 出现频率最高的3个单词
    10. top_three = word_counts.most_common(3)
    11. print(top_three) # [("eyes", 8), ("the", 5), ("look", 4)]
    12. print(word_counts["eyes"]) # 8
    13. # 使用update方法更新
    14. more_words = ["eyes", "the"]
    15. word_counts.update(more_words)
    16. print(word_counts["eyes"]) # 9
    17. print(word_counts["the"]) # 6

    Counter实例进行数学运算:

    1. from collections import Counter
    2. words = [
    3. "eyes", "look", "into", "eyes", "the",
    4. "eyes", "the", "eyes", "the", "eyes", "the"
    5. ]
    6. more_words = ["eyes", "the"]
    7. a = Counter(words)
    8. b = Counter(more_words)
    9. add_words = a + b
    10. print(add_words) # Counter({"eyes": 6, "the": 5, "look": 1, "into": 1})
    11. sub_words = a - b
    12. print(sub_words) # Counter({"eyes": 4, "the": 3, "look": 1, "into": 1})