欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

荐 python学习——collections模块之Counter

程序员文章站 2022-07-03 18:20:31
一、Counter是用来统计个数的下面是用传统方法统计个数,随机生成长度为20的字符串,用defaultdict初始化字典用Counter来统计在实例化的之后直接将待统计的可迭代对象传进去即可,注意是可迭代对象,下图是源码的一些使用方法__init__Counter有6个方法,下面咱们就讲讲这些方法的用法most_commomdef most_common(self, n=None): '''List the n most common elements and...

一、Counter是用来统计个数的

  • 下面是用传统方法统计个数,随机生成长度为20的字符串,用defaultdict初始化字典

荐
                                                        python学习——collections模块之Counter

  • Counter来统计
    荐
                                                        python学习——collections模块之Counter
  • 在实例化的之后直接将待统计的可迭代对象传进去即可,注意是可迭代对象,下图是源码的一些使用方法
    荐
                                                        python学习——collections模块之Counter
  • __init__
    荐
                                                        python学习——collections模块之Counter
  • Counter有6个方法,下面咱们就讲讲这些方法的用法
    荐
                                                        python学习——collections模块之Counter
  1. 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个元素的统计结果
    荐
                                                        python学习——collections模块之Counter
  1. 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()))
    
  1. subtract两个统计结果相减,会改变原来的数值,下图是c的结果减去a的结果
    荐
                                                        python学习——collections模块之Counter
  • 该方法传入的参数是可迭代对象,不一定是Counter实例化对象,如果c中没有该值,则该值置为0,然后个数相减
    荐
                                                        python学习——collections模块之Counter
  1. copy浅拷贝,不多赘述

    def copy(self):
        'Return a shallow copy.'
        return self.__class__(self)
    
  2. 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去初始化,所以这里选择覆盖了dictfromkeys方法,让其抛出异常
    荐
                                                        python学习——collections模块之Counter
  1. 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)
    

    荐
                                                        python学习——collections模块之Counter
    荐
                                                        python学习——collections模块之Counter

二、全局完!!

本文地址:https://blog.csdn.net/weixin_44024393/article/details/107315887