ó ¸&,Yc@@s®ddlmZyddlmZWnƒek r©ddlmZddlmZddl m Z m Z de fd„ƒYZe dkr¦dd lZejƒGHnnXd S( i(tabsolute_import(tCounter(t itemgetter(tnlargest(trepeattifilterRcB@sªeZdZdd„Zd„Zdd„Zd„Zedd„ƒZ dd„Z dd„Z d„Z d „Z d „Zd „Zd „Zd „Zd„Zd„ZRS(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)] cK@s|j||dS(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(tupdate(tselftiterabletkwds((s}/home/vagrant/ruby-gnome2.win32/gobject-introspection/vendor/local/lib/gobject-introspection/giscanner/collections/counter.pyt__init__Vs cC@sdS(s1The count of elements not in the Counter is zero.i((Rtkey((s}/home/vagrant/ruby-gnome2.win32/gobject-introspection/vendor/local/lib/gobject-introspection/giscanner/collections/counter.pyt __missing__cscC@sM|dkr.t|jƒdtdƒdtƒSt||jƒdtdƒƒS(sList 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 itreverseN(tNonetsortedt iteritemsRtTrueR(Rtn((s}/home/vagrant/ruby-gnome2.win32/gobject-introspection/vendor/local/lib/gobject-introspection/giscanner/collections/counter.pyt most_commonhs "cc@s@x9|jƒD]+\}}xtd|ƒD] }|Vq)Wq WdS(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(RRR(Rtelemtcountt_((s}/home/vagrant/ruby-gnome2.win32/gobject-introspection/vendor/local/lib/gobject-introspection/giscanner/collections/counter.pytelementsuscC@stdƒ‚dS(Ns@Counter.fromkeys() is undefined. Use Counter(iterable) instead.(tNotImplementedError(tclsRtv((s}/home/vagrant/ruby-gnome2.win32/gobject-introspection/vendor/local/lib/gobject-introspection/giscanner/collections/counter.pytfromkeysscK@sÂ|dk r¨t|dƒrt|ra|j}xD|jƒD]#\}}||dƒ|||>> 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 RiiN(RthasattrtgetRtdictR(RRR tself_getRR((s}/home/vagrant/ruby-gnome2.win32/gobject-introspection/vendor/local/lib/gobject-introspection/giscanner/collections/counter.pyR–s    cK@sdt|dƒr?xN|jƒD]\}}||c|8>> 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 RiN(RR(RRR RR((s}/home/vagrant/ruby-gnome2.win32/gobject-introspection/vendor/local/lib/gobject-introspection/giscanner/collections/counter.pytsubtract¹s  cC@s |j|ƒS(sReturn a shallow copy.(t __class__(R((s}/home/vagrant/ruby-gnome2.win32/gobject-introspection/vendor/local/lib/gobject-introspection/giscanner/collections/counter.pytcopyÐscC@s|jt|ƒffS(N(R!R(R((s}/home/vagrant/ruby-gnome2.win32/gobject-introspection/vendor/local/lib/gobject-introspection/giscanner/collections/counter.pyt __reduce__ÔscC@s#||krtj||ƒndS(sGLike dict.__delitem__() but does not raise KeyError for missing values.N(Rt __delitem__(RR((s}/home/vagrant/ruby-gnome2.win32/gobject-introspection/vendor/local/lib/gobject-introspection/giscanner/collections/counter.pyR$×s cC@sI|sd|jjSdjtdj|jƒƒƒ}d|jj|fS(Ns%s()s, s%r: %rs%s({%s})(R!t__name__tjointmapt__mod__R(Rtitems((s}/home/vagrant/ruby-gnome2.win32/gobject-introspection/vendor/local/lib/gobject-introspection/giscanner/collections/counter.pyt__repr__Üs!cC@slt|tƒstStƒ}xIt|ƒt|ƒBD]1}||||}|dkr3|||>> Counter('abbb') + Counter('bcc') Counter({'b': 4, 'c': 2, 'a': 1}) i(t isinstanceRtNotImplementedtset(RtothertresultRtnewcount((s}/home/vagrant/ruby-gnome2.win32/gobject-introspection/vendor/local/lib/gobject-introspection/giscanner/collections/counter.pyt__add__ës  cC@slt|tƒstStƒ}xIt|ƒt|ƒBD]1}||||}|dkr3|||>> Counter('abbbc') - Counter('bccd') Counter({'b': 2, 'a': 1}) i(R+RR,R-(RR.R/RR0((s}/home/vagrant/ruby-gnome2.win32/gobject-introspection/vendor/local/lib/gobject-introspection/giscanner/collections/counter.pyt__sub__ûs  cC@swt|tƒstSt}tƒ}xNt|ƒt|ƒBD]6}|||||ƒ}|dkr9|||>> Counter('abbb') | Counter('bcc') Counter({'b': 3, 'c': 2, 'a': 1}) i(R+RR,tmaxR-(RR.t_maxR/RR0((s}/home/vagrant/ruby-gnome2.win32/gobject-introspection/vendor/local/lib/gobject-introspection/giscanner/collections/counter.pyt__or__ s  cC@s›t|tƒstSt}tƒ}t|ƒt|ƒkrJ||}}nxJt|j|ƒD]6}|||||ƒ}|dkr]|||>> Counter('abbb') & Counter('bcc') Counter({'b': 1}) i(R+RR,tmintlenRt __contains__(RR.t_minR/RR0((s}/home/vagrant/ruby-gnome2.win32/gobject-introspection/vendor/local/lib/gobject-introspection/giscanner/collections/counter.pyt__and__s  N(R%t __module__t__doc__RR R RRt classmethodRRR R"R#R$R*R1R2R5R:(((s}/home/vagrant/ruby-gnome2.win32/gobject-introspection/vendor/local/lib/gobject-introspection/giscanner/collections/counter.pyR#s"+   #        t__main__N(t __future__Rt collectionsRt ImportErrortoperatorRtheapqRt itertoolsRRRR%tdoctestttestmod(((s}/home/vagrant/ruby-gnome2.win32/gobject-introspection/vendor/local/lib/gobject-introspection/giscanner/collections/counter.pyts ÿ