zoukankan      html  css  js  c++  java
  • Python collections系列之计数器

    计数器(counter)

    Counter是对字典(无序)类型的补充,用于追踪值的出现次数。

    使用counter需要导入 collections 类

    ps:具备字典的所有功能 + 自己的功能

     

    1、创建一个计数器

    >>> import collections
    >>> obj = collections.Counter('aaabbccsdfsdfdfsdfsdf')

    2、查看计数器变量

    >>> print(obj)
    Counter({'d': 5, 'f': 5, 's': 4, 'a': 3, 'c': 2, 'b': 2})

    3、查看计数器可使用的方法

    >>> dir(obj)
    ['__add__', '__and__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__iand__', '__init__', '__ior__', '__isub__', '__iter__', '__le__', '__len__', '__lt__', '__missing__', '__module__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__weakref__', '_keep_positive', 'clear', 'copy', 'elements', 'fromkeys', 'get', 'items', 'keys', 'most_common', 'pop', 'popitem', 'setdefault', 'subtract', 'update', 'values']
    ########################################################################
    ###  Counter
    ########################################################################
    
    class Counter(dict):
        '''Dict subclass for counting hashable items.  Sometimes called a bag
        or multiset.  Elements are stored as dictionary keys and their counts
        are stored as dictionary values.
    
        >>> c = Counter('abcdeabcdabcaba')  # count elements from a string
    
        >>> c.most_common(3)                # three most common elements
        [('a', 5), ('b', 4), ('c', 3)]
        >>> sorted(c)                       # list all unique elements
        ['a', 'b', 'c', 'd', 'e']
        >>> ''.join(sorted(c.elements()))   # list elements with repetitions
        'aaaaabbbbcccdde'
        >>> sum(c.values())                 # total of all counts
    
        >>> c['a']                          # count of letter 'a'
        >>> for elem in 'shazam':           # update counts from an iterable
        ...     c[elem] += 1                # by adding 1 to each element's count
        >>> c['a']                          # now there are seven 'a'
        >>> del c['b']                      # remove all 'b'
        >>> c['b']                          # now there are zero 'b'
    
        >>> d = Counter('simsalabim')       # make another counter
        >>> c.update(d)                     # add in the second counter
        >>> c['a']                          # now there are nine 'a'
    
        >>> c.clear()                       # empty the counter
        >>> c
        Counter()
    
        Note:  If a count is set to zero or reduced to zero, it will remain
        in the counter until the entry is deleted or the counter is cleared:
    
        >>> c = Counter('aaabbc')
        >>> c['b'] -= 2                     # reduce the count of 'b' by two
        >>> c.most_common()                 # 'b' is still in, but its count is zero
        [('a', 3), ('c', 1), ('b', 0)]
    
        '''
        # References:
        #   http://en.wikipedia.org/wiki/Multiset
        #   http://www.gnu.org/software/smalltalk/manual-base/html_node/Bag.html
        #   http://www.demo2s.com/Tutorial/Cpp/0380__set-multiset/Catalog0380__set-multiset.htm
        #   http://code.activestate.com/recipes/259174/
        #   Knuth, TAOCP Vol. II section 4.6.3
    
        def __init__(self, iterable=None, **kwds):
            '''Create a new, empty Counter object.  And if given, count elements
            from an input iterable.  Or, initialize the count from another mapping
            of elements to their counts.
    
            >>> c = Counter()                           # a new, empty counter
            >>> c = Counter('gallahad')                 # a new counter from an iterable
            >>> c = Counter({'a': 4, 'b': 2})           # a new counter from a mapping
            >>> c = Counter(a=4, b=2)                   # a new counter from keyword args
    
            '''
            super(Counter, self).__init__()
            self.update(iterable, **kwds)
    
        def __missing__(self, key):
            """ 对于不存在的元素,返回计数器为0 """
            'The count of elements not in the Counter is zero.'
            # Needed so that self[missing_item] does not raise KeyError
            return 0
    
        def most_common(self, n=None):
            """ 数量大于等n的所有元素和计数器 """
            '''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.iteritems(), key=_itemgetter(1), reverse=True)
            return _heapq.nlargest(n, self.iteritems(), key=_itemgetter(1))
    
        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
    
            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.iteritems()))
    
        # Override dict methods where necessary
    
        @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(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
    
            '''
            # 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, Mapping):
                    if self:
                        self_get = self.get
                        for elem, count in iterable.iteritems():
                            self[elem] = self_get(elem, 0) + count
                    else:
                        super(Counter, self).update(iterable) # fast path when counter is empty
                else:
                    self_get = self.get
                    for elem in iterable:
                        self[elem] = self_get(elem, 0) + 1
            if kwds:
                self.update(kwds)
    
        def subtract(self, iterable=None, **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
            >>> c['w']                          # 1 in which, minus 1 in witch, minus 1 in watch
            -1
    
            '''
            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)
    
        def copy(self):
            """ 拷贝 """
            'Return a shallow copy.'
            return self.__class__(self)
    
        def __reduce__(self):
            """ 返回一个元组(类型,元组) """
            return self.__class__, (dict(self),)
    
        def __delitem__(self, elem):
            """ 删除元素 """
            'Like dict.__delitem__() but does not raise KeyError for missing values.'
            if elem in self:
                super(Counter, self).__delitem__(elem)
    
        def __repr__(self):
            if not self:
                return '%s()' % self.__class__.__name__
            items = ', '.join(map('%r: %r'.__mod__, self.most_common()))
            return '%s({%s})' % (self.__class__.__name__, items)
    
        # Multiset-style mathematical operations discussed in:
        #       Knuth TAOCP Volume II section 4.6.3 exercise 19
        #       and at http://en.wikipedia.org/wiki/Multiset
        #
        # Outputs guaranteed to only include positive counts.
        #
        # To strip negative and zero counts, add-in an empty counter:
        #       c += Counter()
    
        def __add__(self, other):
            '''Add counts from two counters.
    
            >>> Counter('abbb') + Counter('bcc')
            Counter({'b': 4, 'c': 2, 'a': 1})
    
            '''
            if not isinstance(other, Counter):
                return NotImplemented
            result = Counter()
            for elem, count in self.items():
                newcount = count + other[elem]
                if newcount > 0:
                    result[elem] = newcount
            for elem, count in other.items():
                if elem not in self and count > 0:
                    result[elem] = count
            return result
    
        def __sub__(self, other):
            ''' Subtract count, but keep only results with positive counts.
    
            >>> Counter('abbbc') - Counter('bccd')
            Counter({'b': 2, 'a': 1})
    
            '''
            if not isinstance(other, Counter):
                return NotImplemented
            result = Counter()
            for elem, count in self.items():
                newcount = count - other[elem]
                if newcount > 0:
                    result[elem] = newcount
            for elem, count in other.items():
                if elem not in self and count < 0:
                    result[elem] = 0 - count
            return result
    
        def __or__(self, other):
            '''Union is the maximum of value in either of the input counters.
    
            >>> Counter('abbb') | Counter('bcc')
            Counter({'b': 3, 'c': 2, 'a': 1})
    
            '''
            if not isinstance(other, Counter):
                return NotImplemented
            result = Counter()
            for elem, count in self.items():
                other_count = other[elem]
                newcount = other_count if count < other_count else count
                if newcount > 0:
                    result[elem] = newcount
            for elem, count in other.items():
                if elem not in self and count > 0:
                    result[elem] = count
            return result
    
        def __and__(self, other):
            ''' Intersection is the minimum of corresponding counts.
    
            >>> Counter('abbb') & Counter('bcc')
            Counter({'b': 1})
    
            '''
            if not isinstance(other, Counter):
                return NotImplemented
            result = Counter()
            for elem, count in self.items():
                other_count = other[elem]
                newcount = count if count < other_count else other_count
                if newcount > 0:
                    result[elem] = newcount
            return result
    Counter

    4、常用的计数器操作

    # most_common 输出出现次数最多的字符
    
    >>> import collections
    >>> obj = collections.Counter('aaabbccsdfsdfdfsdfsdf')
    >>> print(obj.most_common(2))
    [('d', 5), ('f', 5)]
    # items 计数器以k/v方式输出统计结果
    
    import collections
    
    obj = collections.Counter('aaabbccsdfsdfdfsdfsdf')
    
    for k,v in obj.items():
        print(k,v)
    
    输出结果:
    f 5
    s 4
    d 5
    a 3
    c 2
    b 2
    # elements 把所有的元素拿到,并进行统计
    
    >>> import collections
    >>> obj = collections.Counter('aaabbccsdfsdfdfsdfsdf')
    >>> print(obj.elements)
    <bound method Counter.elements of Counter({'d': 5, 'f': 5, 's': 4, 'a': 3, 'c': 2, 'b': 2})>
    # update 更新计数器中的字符串或字符
    
    import collections
    
    obj = collections.Counter(['11', '22', '33', '22'])
    print(obj)
    obj.update(['eric', '11', '11'])
    print(obj)
    
    输出结果:
    Counter({'22': 2, '33': 1, '11': 1})
    Counter({'11': 3, '22': 2, '33': 1, 'eric': 1})
    # subtract 删除计数器中的字符串
    
    import collections
    
    obj = collections.Counter(['11', '22', '33', '22'])
    print(obj)
    obj.update([
    'eric', '11', '11']) print(obj) obj.subtract(['eric', '11', '11', 'root']) print(obj) 输出结果: Counter({'22': 2, '11': 1, '33': 1}) Counter({'11': 3, '22': 2, '33': 1, 'eric': 1}) Counter({'22': 2, '11': 1, '33': 1, 'eric': 0, 'root': -1})
  • 相关阅读:
    assert函数(python)
    局域网内计算机访问FTP服务器时,提示“无法访问服务器名称或地址
    Win10更新故障修复之路!
    对每个人都有用的人生哲理,值得一看!
    如何定制属于自己的个性化win7主题?
    快来看看我的刷机历程,你有同样的经历么?
    今儿个一起来看看安装系统时关于BIOS的那些事儿
    你是如何在DOS中查看这个电脑的硬盘分区的呢?
    这里有搭建本地服务器的方法合集,一起来看一下吧!
    Windows下快速搭建NodeJS本地服务器
  • 原文地址:https://www.cnblogs.com/evescn/p/7510871.html
Copyright © 2011-2022 走看看