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的浅拷贝
Counter是dict的子类,所以它其实也是字典。只不过它的键对应的值都是计数,值可以是任意整数。下面是四种创建Counter实例的例子:

1
2
3
4

c = Counter() # a new, empty counter
c = Counter(‘gallahad’) # a new counter from an iterable
c = Counter({‘red’: 4, ‘blue’: 2}) # a new counter from a mapping
c = Counter(cats=4, dogs=8) # a new counter from keyword args
以第二种为例,看下效果

1
2
c = Counter(‘gallahad’)
print©
输出就是一个字典

1
Counter({‘a’: 3, ‘l’: 2, ‘g’: 1, ‘d’: 1, ‘h’: 1})
Counter能自动对字符串,列表等可迭代的对象里面的元素计数并转换成字典,非常好用

下面是一些Counter对象的常用函数

1
2
3
4
5
6
7
8
9
sum(c.values()) # total of all counts
c.clear() # reset all counts
list© # list unique elements
set© # convert to a set
dict© # convert to a regular dictionary
c.items() # convert to a list of (elem, cnt) pairs
Counter(dict(list_of_pairs)) # convert from a list of (elem, cnt) pairs
c.most_common()[:-n-1:-1] # n least common elements
+c # remove zero and negative counts
并且Counter对象还可以做加法,如下

1
2
3
4
5
6
7
8
9
10

c = Counter(a=3, b=1)
d = Counter(a=1, b=2)
c + d # add two counters together: c[x] + d[x]
Counter({‘a’: 4, ‘b’: 3})

c - d # subtract (keeping only positive counts)
Counter({‘a’: 2})

c & d # intersection: min(c[x], d[x])
Counter({‘a’: 1, ‘b’: 1})

c | d # union: max(c[x], d[x])
Counter({‘a’: 3, ‘b’: 2})

注:例子全部来自python3.5的官方文档

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值