python之collections之counter

一、定义

Counter(计数器)是对字典的补充,用于追踪值的出现次数。

Counter是一个继承了字典的类(Counter(dict))

二、相关方法

继承了字典的类,有关字典的相关方法也一并继承过来。

比如items()方法

 

def most_common(self, n=None):
'''List the n most common elements and their counts from the most
common to the least. If n is None, then list all element counts.

>>> Counter('abcdeabcdabcaba').most_common(3)
[('a', 5), ('b', 4), ('c', 3)]
  截取指定位数的值
'''
# Emulate Bag.sortedByCount from Smalltalk
if n is None:
return sorted(self.items(), key=_itemgetter(1), reverse=True)
return _heapq.nlargest(n, self.items(), key=_itemgetter(1))
eg:

 



def elements(self):
'''
  显示计数器中所有的元素
  Iterator over elements repeating each as many times as its count.

>>> c = Counter('ABCABC')
>>> sorted(c.elements())
['A', 'A', 'B', 'B', 'C', 'C']

# Knuth's example for prime factors of 1836: 2**2 * 3**3 * 17**1
>>> prime_factors = Counter({2: 2, 3: 3, 17: 1})
>>> product = 1
>>> for factor in prime_factors.elements(): # loop over factors
... product *= factor # and multiply them
>>> product
1836

Note, if an element's count has been set to zero or is a negative
number, elements() will ignore it.

'''
# Emulate Bag.do from Smalltalk and Multiset.begin from C++.
return _chain.from_iterable(_starmap(_repeat, self.items()))

# Override dict methods where necessary
eg:

 



@classmethod
def fromkeys(cls, iterable, v=None):
# There is no equivalent method for counters because setting v=1
# means that no element can have a count greater than one.
此功能没有实现
raise NotImplementedError(
'Counter.fromkeys() is undefined. Use Counter(iterable) instead.')

def update(*args, **kwds):
'''
  更新Counter,对于已有的元素计数加一,对没有的元素进行添加
  Like dict.update() but add counts instead of replacing them.

Source can be an iterable, a dictionary, or another Counter instance.

>>> c = Counter('which')
>>> c.update('witch') # add elements from another iterable
>>> d = Counter('watch')
>>> c.update(d) # add elements from another counter
>>> c['h'] # four 'h' in which, witch, and watch
4

'''
# The regular dict.update() operation makes no sense here because the
# replace behavior results in the some of original untouched counts
# being mixed-in with all of the other counts for a mismash that
# doesn't have a straight-forward interpretation in most counting
# contexts. Instead, we implement straight-addition. Both the inputs
# and outputs are allowed to contain zero and negative counts.

if not args:
raise TypeError("descriptor 'update' of 'Counter' object "
"needs an argument")
self, *args = args
if len(args) > 1:
raise TypeError('expected at most 1 arguments, got %d' % len(args))
iterable = args[0] if args else None
if iterable is not None:
if isinstance(iterable, Mapping):
if self:
self_get = self.get
for elem, count in iterable.items():
self[elem] = count + self_get(elem, 0)
else:
super(Counter, self).update(iterable) # fast path when counter is empty
else:
_count_elements(self, iterable)
if kwds:
self.update(kwds)
eg:

 

 


def subtract(*args, **kwds):
'''Like dict.update() but subtracts counts instead of replacing them.
Counts can be reduced below zero. Both the inputs and outputs are
allowed to contain zero and negative counts.

Source can be an iterable, a dictionary, or another Counter instance.

>>> c = Counter('which')
>>> c.subtract('witch') # subtract elements from another iterable
>>> c.subtract(Counter('watch')) # subtract elements from another counter
>>> c['h'] # 2 in which, minus 1 in witch, minus 1 in watch
0
>>> c['w'] # 1 in which, minus 1 in witch, minus 1 in watch
-1
  对指定的Counter元素做减法运算,对出现过的累计减一(可以出现负数),对没有出现过的进行0-1运算
'''
if not args:
raise TypeError("descriptor 'subtract' of 'Counter' object "
"needs an argument")
self, *args = args
if len(args) > 1:
raise TypeError('expected at most 1 arguments, got %d' % len(args))
iterable = args[0] if args else None
if iterable is not None:
self_get = self.get
if isinstance(iterable, Mapping):
for elem, count in iterable.items():
self[elem] = self_get(elem, 0) - count
else:
for elem in iterable:
self[elem] = self_get(elem, 0) - 1
if kwds:
self.subtract(kwds)
eg:

 


def copy(self):
'Return a shallow copy.'
return self.__class__(self)
  Counter的浅拷贝

转载于:https://www.cnblogs.com/baotouzhangce/p/6179911.html

Python 中,`collections.Counter` 是一个非常强大且实用的工具类,用于统计可迭代对象中元素的出现次数。其设计目的是提供一种高效、简洁的方式来处理计数问题,适用于数据分析、文本处理、算法优化等多个场景。 ### 基本用法 可以通过多种方式创建 `Counter` 对象,例如传入列表、字符串、元组等可哈希对象: ```python from collections import Counter # 统计列表中元素的出现次数 c1 = Counter(['apple', 'banana', 'apple', 'orange', 'banana', 'banana']) # 输出: Counter({'banana': 3, 'apple': 2, 'orange': 1}) # 统计字符串中字符的出现次数 c2 = Counter('abracadabra') # 输出: Counter({'a': 5, 'b': 2, 'r': 2, 'c': 1, 'd': 1}) ``` ### 更新计数器 可以使用 `update()` 方法向已有的 `Counter` 对象中添加新的数据: ```python c1.update(['apple', 'grape']) # 输出: Counter({'banana': 3, 'apple': 3, 'orange': 1, 'grape': 1}) ``` ### 获取最常见元素 `most_common(n)` 方法返回一个包含最多出现元素的列表,按频率降序排列: ```python top_two = c1.most_common(2) # 输出: [('banana', 3), ('apple', 3)] ``` ### 集合运算 `Counter` 支持类似集合的操作,如加法、减法、交集和并集: ```python c3 = Counter(a=3, b=1) c4 = Counter(a=1, b=2) # 加法 c_add = c3 + c4 # Counter({'a': 4, 'b': 3}) # 减法 c_sub = c3 - c4 # Counter({'a': 2}) # 交集(取最小) c_and = c3 & c4 # Counter({'a': 1, 'b': 1}) # 并集(取最大) c_or = c3 | c4 # Counter({'a': 3, 'b': 2}) ``` ### 示例:统计词频并更新计数 以下是一个完整的示例程序,展示如何初始化 `Counter`、更新数据并获取高频词: ```python from collections import Counter words = ['hello', 'world', 'hello', 'python'] word_counter = Counter(words) new_words = ['hello', 'programming'] word_counter.update(new_words) print(word_counter.most_common(2)) # 输出: [('hello', 3), ('world', 1)] 或类似结果 ``` ### 总结 `collections.Counter` 提供了丰富的功能来处理计数任务,包括初始化、更新、获取高频项以及执行集合操作。熟练掌握其用法能够显著提升代码效率与可读性[^1]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值