目录
使用 collections.Counter 统计元素出现次数
使用 collections.Counter
统计元素出现次数
collections.Counter
是 Python 标准库 collections
模块中的一个类,用于统计元素出现次数。它提供了一种简单高效的方式来统计列表、字符串或其他可迭代对象中每个元素的出现次数。本文将详细介绍 Counter
的基本概念、常见用法和一些实用的代码示例。
1. Counter
的基本概念
Counter
是一个字典的子类,专门用于计数。它接受一个可迭代对象(如列表、字符串等)作为输入,并返回一个字典,其中键是元素,值是该元素出现的次数。
2. 创建 Counter
对象
从列表创建 Counter
from collections import Counter
# 创建一个列表
lst = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]
# 创建 Counter 对象
counter = Counter(lst)
print(counter) # 输出 Counter({4: 4, 3: 3, 2: 2, 1: 1})
从字符串创建 Counter
from collections import Counter
# 创建一个字符串
s = "hello world"
# 创建 Counter 对象
counter = Counter(s)
print(counter) # 输出 Counter({'l': 3, 'o': 2, 'h': 1, 'e': 1, ' ': 1, 'w': 1, 'r': 1, 'd': 1})
3. Counter
的常见方法
elements()
elements()
方法返回一个迭代器,按元素出现次数的顺序生成元素。
from collections import Counter
# 创建一个 Counter 对象
counter = Counter(a=4, b=2, c=0, d=-2)
# 获取所有元素
elements = list(counter.elements())
print(elements) # 输出 ['a', 'a', 'a', 'a', 'b', 'b']
most_common(n)
most_common(n)
方法返回一个列表,包含出现次数最多的前 n 个元素及其出现次数。
from collections import Counter
# 创建一个 Counter 对象
counter = Counter([1, 2, 2, 3, 3, 3, 4, 4, 4, 4])
# 获取出现次数最多的前 3 个元素
top_3 = counter.most_common(3)
print(top_3) # 输出 [(4, 4), (3, 3), (2, 2)]
subtract()
subtract()
方法用于减少 Counter
对象中元素的计数。
from collections import Counter
# 创建一个 Counter 对象
counter = Counter(a=4, b=2, c=0, d=-2)
# 创建另一个 Counter 对象
other_counter = Counter(a=1, b=1)
# 减少计数
counter.subtract(other_counter)
print(counter) # 输出 Counter({'a': 3, 'b': 1, 'c': 0, 'd': -2})
update()
update()
方法用于增加 Counter
对象中元素的计数。
from collections import Counter
# 创建一个 Counter 对象
counter = Counter(a=4, b=2, c=0, d=-2)
# 创建另一个 Counter 对象
other_counter = Counter(a=1, b=1)
# 增加计数
counter.update(other_counter)
print(counter) # 输出 Counter({'a': 5, 'b': 3, 'c': 0, 'd': -2})
4. Counter
的运算操作
Counter
对象支持一些基本的数学运算操作,如加法、减法、交集和并集。
加法
from collections import Counter
# 创建两个 Counter 对象
counter1 = Counter(a=3, b=1)
counter2 = Counter(a=1, b=2)
# 加法
combined = counter1 + counter2
print(combined) # 输出 Counter({'a': 4, 'b': 3})
减法
from collections import Counter
# 创建两个 Counter 对象
counter1 = Counter(a=3, b=1)
counter2 = Counter(a=1, b=2)
# 减法
difference = counter1 - counter2
print(difference) # 输出 Counter({'a': 2})
交集
from collections import Counter
# 创建两个 Counter 对象
counter1 = Counter(a=3, b=1)
counter2 = Counter(a=1, b=2)
# 交集
intersection = counter1 & counter2
print(intersection) # 输出 Counter({'a': 1, 'b': 1})
并集
from collections import Counter
# 创建两个 Counter 对象
counter1 = Counter(a=3, b=1)
counter2 = Counter(a=1, b=2)
# 并集
union = counter1 | counter2
print(union) # 输出 Counter({'a': 3, 'b': 2})
5. 实际应用示例
统计单词频率
from collections import Counter
# 创建一个句子
sentence = "the quick brown fox jumps over the lazy dog"
# 分割句子为单词列表
words = sentence.split()
# 创建 Counter 对象
word_count = Counter(words)
# 获取最常见的 3 个单词
top_3_words = word_count.most_common(3)
print(top_3_words) # 输出 [('the', 2), ('quick', 1), ('brown', 1)]
统计字符频率
from collections import Counter
# 创建一个字符串
s = "hello world"
# 创建 Counter 对象
char_count = Counter(s)
# 获取最常见的 3 个字符
top_3_chars = char_count.most_common(3)
print(top_3_chars) # 输出 [('l', 3), ('o', 2), ('h', 1)]