Repository URL to install this package:
|
Version:
1.0.2-20151008 ▾
|
Ñò
qì©Uc @@ s² d d k l Z y d d k l Z Wn e j
o{ d d k l Z d d k l Z d d k l
Z
l Z d e f d YZ e
d j o d d k Z e i GHn n Xd S(
i ( t absolute_import( t Counter( t
itemgetter( t nlargest( t repeatt ifilterR c B@ sª e Z d Z d d Z d Z d d Z d Z e d d Z d d Z
d d Z d Z d Z
d
Z d Z d Z d
Z d Z d Z RS( s' 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
15
>>> c['a'] # count of letter 'a'
5
>>> 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'
7
>>> del c['b'] # remove all 'b'
>>> c['b'] # now there are zero 'b'
0
>>> d = Counter('simsalabim') # make another counter
>>> c.update(d) # add in the second counter
>>> c['a'] # now there are nine 'a'
9
>>> 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)]
c K@ s | i | | d S( s% 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
N( t update( t selft iterablet kwds( ( sh /home/zelgadis/synfig-buildroot/linux64/build/lib/gobject-introspection/giscanner/collections/counter.pyt __init__V s c C@ s d S( s1 The count of elements not in the Counter is zero.i ( ( R t key( ( sh /home/zelgadis/synfig-buildroot/linux64/build/lib/gobject-introspection/giscanner/collections/counter.pyt __missing__c s c C@ sO | d j o# t | i d t d d t St | | i d t d S( s 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)]
R i t reverseN( t Nonet sortedt iteritemsR t TrueR ( R t n( ( sh /home/zelgadis/synfig-buildroot/linux64/build/lib/gobject-introspection/giscanner/collections/counter.pyt most_commonh s
#c c@ s@ x9 | i D]+ \ } } x t d | D] } | Vq) Wq
Wd S( sµ 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.
N( R R R ( R t elemt countt _( ( sh /home/zelgadis/synfig-buildroot/linux64/build/lib/gobject-introspection/giscanner/collections/counter.pyt elementsu s
c C@ s t d d S( Ns@ Counter.fromkeys() is undefined. Use Counter(iterable) instead.( t NotImplementedError( t clsR t v( ( sh /home/zelgadis/synfig-buildroot/linux64/build/lib/gobject-introspection/giscanner/collections/counter.pyt fromkeys s c K@ sÊ | d j o¡ t | d o\ | oA | i } xE | i D]# \ } } | | d | | | <q: Wqª t i | | q® | i } x) | D] } | | d d | | <q Wn | o | i | n d S( sé 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
R i i N( R t hasattrt getR t dictR ( R R R t self_getR R ( ( sh /home/zelgadis/synfig-buildroot/linux64/build/lib/gobject-introspection/giscanner/collections/counter.pyR s
c K@ sf t | d o1 xO | i D] \ } } | | c | 8<q Wn" x | D] } | | c d 8<qH Wd S( sã 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
R i N( R R ( R R R R R ( ( sh /home/zelgadis/synfig-buildroot/linux64/build/lib/gobject-introspection/giscanner/collections/counter.pyt subtract¹ s
c C@ s
| i | S( s Return a shallow copy.( t __class__( R ( ( sh /home/zelgadis/synfig-buildroot/linux64/build/lib/gobject-introspection/giscanner/collections/counter.pyt copyÐ s c C@ s | i t | f f S( N( R! R ( R ( ( sh /home/zelgadis/synfig-buildroot/linux64/build/lib/gobject-introspection/giscanner/collections/counter.pyt
__reduce__Ô s c C@ s% | | j o t i | | n d S( sG Like dict.__delitem__() but does not raise KeyError for missing values.N( R t __delitem__( R R ( ( sh /home/zelgadis/synfig-buildroot/linux64/build/lib/gobject-introspection/giscanner/collections/counter.pyR$ × s
c C@ sK | p d | i i Sd i t d i | i } d | i i | f S( Ns %s()s , s %r: %rs %s({%s})( R! t __name__t joint mapt __mod__R ( R t items( ( sh /home/zelgadis/synfig-buildroot/linux64/build/lib/gobject-introspection/giscanner/collections/counter.pyt __repr__Ü s !c C@ sp t | t p t St } xK t | t | BD]3 } | | | | } | d j o | | | <q5 q5 W| S( s Add counts from two counters.
>>> Counter('abbb') + Counter('bcc')
Counter({'b': 4, 'c': 2, 'a': 1})
i ( t
isinstanceR t NotImplementedt set( R t othert resultR t newcount( ( sh /home/zelgadis/synfig-buildroot/linux64/build/lib/gobject-introspection/giscanner/collections/counter.pyt __add__ë s
c C@ sp t | t p t St } xK t | t | BD]3 } | | | | } | d j o | | | <q5 q5 W| S( s¤ Subtract count, but keep only results with positive counts.
>>> Counter('abbbc') - Counter('bccd')
Counter({'b': 2, 'a': 1})
i ( R+ R R, R- ( R R. R/ R R0 ( ( sh /home/zelgadis/synfig-buildroot/linux64/build/lib/gobject-introspection/giscanner/collections/counter.pyt __sub__û s
c C@ s{ t | t p t St } t } xP t | t | BD]8 } | | | | | } | d j o | | | <q; q; W| S( s¬ Union is the maximum of value in either of the input counters.
>>> Counter('abbb') | Counter('bcc')
Counter({'b': 3, 'c': 2, 'a': 1})
i ( R+ R R, t maxR- ( R R. t _maxR/ R R0 ( ( sh /home/zelgadis/synfig-buildroot/linux64/build/lib/gobject-introspection/giscanner/collections/counter.pyt __or__ s
c C@ s¡ t | t p t St } t } t | t | j o | | } } n xL t | i | D]8 } | | | | | } | d j o | | | <qa qa W| S( s Intersection is the minimum of corresponding counts.
>>> Counter('abbb') & Counter('bcc')
Counter({'b': 1})
i ( R+ R R, t mint lenR t __contains__( R R. t _minR/ R R0 ( ( sh /home/zelgadis/synfig-buildroot/linux64/build/lib/gobject-introspection/giscanner/collections/counter.pyt __and__ s
N( R% t
__module__t __doc__R R
R R R t classmethodR R R R" R# R$ R* R1 R2 R5 R: ( ( ( sh /home/zelgadis/synfig-buildroot/linux64/build/lib/gobject-introspection/giscanner/collections/counter.pyR # s" +
# t __main__N( t
__future__R t collectionsR t ImportErrort operatorR t heapqR t itertoolsR R R R% t doctestt testmod( ( ( sh /home/zelgadis/synfig-buildroot/linux64/build/lib/gobject-introspection/giscanner/collections/counter.pyt <module> s ÿ