荐 python学习——collections模块之Counter
程序员文章站
2022-04-12 08:41:40
一、Counter是用来统计个数的下面是用传统方法统计个数,随机生成长度为20的字符串,用defaultdict初始化字典用Counter来统计在实例化的之后直接将待统计的可迭代对象传进去即可,注意是可迭代对象,下图是源码的一些使用方法__init__Counter有6个方法,下面咱们就讲讲这些方法的用法most_commomdef most_common(self, n=None): '''List the n most common elements and...
一、Counter
是用来统计个数的
- 下面是用传统方法统计个数,随机生成长度为20的字符串,用
defaultdict
初始化字典
- 用
Counter
来统计
- 在实例化的之后直接将待统计的可迭代对象传进去即可,注意是可迭代对象,下图是源码的一些使用方法
-
__init__
-
Counter
有6个方法,下面咱们就讲讲这些方法的用法
-
most_commom
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('abracadabra').most_common(3) [('a', 5), ('b', 2), ('r', 2)] ''' # 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))
- 可以看到,
Counter(iterable).most_common
返回的是一个列表,列表里面是元组,元组的第一个元素是统计的值,元组的第二个元素是该值的个数,如果传入n,那么结果就是前n个元素的统计结果
-
elements
,返回的是itertools.chain
对象,可以使用list转换类型。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()))
-
itertools.chain
可以查看itertools.chain() --在不同的容器中进行迭代
-
subtract
两个统计结果相减,会改变原来的数值,下图是c的结果减去a的结果
- 该方法传入的参数是可迭代对象,不一定是Counter实例化对象,如果c中没有该值,则该值置为0,然后个数相减
-
copy
浅拷贝,不多赘述def copy(self): 'Return a shallow copy.' return self.__class__(self)
-
fromkeys
@classmethod def fromkeys(cls, iterable, v=None): # There is no equivalent method for counters because the semantics # would be ambiguous in cases such as Counter.fromkeys('aaabbc', v=2). # Initializing counters to zero values isn't necessary because zero # is already the default value for counter lookups. Initializing # to one is easily accomplished with Counter(set(iterable)). For # more exotic cases, create a dictionary first using a dictionary # comprehension or dict.fromkeys(). raise NotImplementedError( 'Counter.fromkeys() is undefined. Use Counter(iterable) instead.')
- 该方法是抛出
NotImplementedError
异常的,咱们知道dict也有fromkeys
方法,Counter
是继承dict的,作者认为用Counter
去初始化一个Counter
对象是没必要的,因为初始化的时候可以直接Counter(itrable)
就行了,没有必要再去使用fromkeys
去初始化,所以这里选择覆盖了dict
的fromkeys
方法,让其抛出异常
-
update
该方法重写了dict的update
方法,如果该值存在,就累加,如果不存在就新添加进去,接受一个可迭代对象,也可以是关键字参数,或者字典def update(self, iterable=None, /, **kwds): '''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 iterable is not None: if isinstance(iterable, _collections_abc.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)
二、全局完!!
本文地址:https://blog.csdn.net/weixin_44024393/article/details/107315887
上一篇: 美国禁售天河2号不怕!每周成本200万
下一篇: 一体电脑为何始终作为边缘化产品存在于世?
推荐阅读