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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 什么是collections模块? collectionsPython内置模块之一,提供了一些有用的集合类,可以用来扩展Python内置类型的功能。 2. collections模块中常用的几个类有哪些? 常用的集合类包括:Counter、deque、defaultdict、OrderedDict、namedtuple等。 3. Counter类有什么作用? Counter类可以帮助我们统计一个可迭代对象中各元素出现的次数,并返回一个字典。 4. deque类有什么特点? deque类是双向队列,可以从两端进行操作,支持高效地添加、删除和旋转元素等操作。 5. defaultdict类与普通字典有什么区别? defaultdict类与普通字典的区别在于,当我们访问一个不存在的键时,defaultdict会自动为其创建一个默认值,而不是抛出KeyError异常。 6. OrderedDict类有什么特点? OrderedDict类是有序字典,可以按照元素添加的顺序进行遍历,而普通字典是无序的。 7. namedtuple类有什么作用? namedtuple类可以用来创建一个具名元组,具有元组的不可变性和字典的可访问性和可读性。 8. 如何使用collections模块中的类? 我们可以使用类似于以下的方式导入collections模块中的类,并使用其提供的方法和属性。 ```python from collections import Counter, deque, defaultdict, OrderedDict, namedtuple # 使用Counter类 c = Counter('hello world') print(c) # 使用deque类 d = deque([1, 2, 3]) d.appendleft(0) d.rotate(1) print(d) # 使用defaultdict类 dd = defaultdict(int) dd['a'] += 1 print(dd) # 使用OrderedDict类 od = OrderedDict() od['b'] = 1 od['a'] = 2 print(od) # 使用namedtuple类 Point = namedtuple('Point', ['x', 'y']) p = Point(1, 2) print(p.x, p.y) ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值