MCPcopy Index your code
hub / github.com/RustPython/RustPython / Counter

Class Counter

Lib/collections/__init__.py:552–989  ·  view source on GitHub ↗

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 m

Source from the content-addressed store, hash-verified

550 pass
551
552class Counter(dict):
553 '''Dict subclass for counting hashable items. Sometimes called a bag
554 or multiset. Elements are stored as dictionary keys and their counts
555 are stored as dictionary values.
556
557 >>> c = Counter('abcdeabcdabcaba') # count elements from a string
558
559 >>> c.most_common(3) # three most common elements
560 [('a', 5), ('b', 4), ('c', 3)]
561 >>> sorted(c) # list all unique elements
562 ['a', 'b', 'c', 'd', 'e']
563 >>> ''.join(sorted(c.elements())) # list elements with repetitions
564 'aaaaabbbbcccdde'
565 >>> sum(c.values()) # total of all counts
566 15
567
568 >>> c['a'] # count of letter 'a'
569 5
570 >>> for elem in 'shazam': # update counts from an iterable
571 ... c[elem] += 1 # by adding 1 to each element's count
572 >>> c['a'] # now there are seven 'a'
573 7
574 >>> del c['b'] # remove all 'b'
575 >>> c['b'] # now there are zero 'b'
576 0
577
578 >>> d = Counter('simsalabim') # make another counter
579 >>> c.update(d) # add in the second counter
580 >>> c['a'] # now there are nine 'a'
581 9
582
583 >>> c.clear() # empty the counter
584 >>> c
585 Counter()
586
587 Note: If a count is set to zero or reduced to zero, it will remain
588 in the counter until the entry is deleted or the counter is cleared:
589
590 >>> c = Counter('aaabbc')
591 >>> c['b'] -= 2 # reduce the count of 'b' by two
592 >>> c.most_common() # 'b' is still in, but its count is zero
593 [('a', 3), ('c', 1), ('b', 0)]
594
595 '''
596 # References:
597 # http://en.wikipedia.org/wiki/Multiset
598 # http://www.gnu.org/software/smalltalk/manual-base/html_node/Bag.html
599 # http://www.java2s.com/Tutorial/Cpp/0380__set-multiset/Catalog0380__set-multiset.htm
600 # http://code.activestate.com/recipes/259174/
601 # Knuth, TAOCP Vol. II section 4.6.3
602
603 def __init__(self, iterable=None, /, **kwds):
604 '''Create a new, empty Counter object. And if given, count elements
605 from an input iterable. Or, initialize the count from another mapping
606 of elements to their counts.
607
608 >>> c = Counter() # a new, empty counter
609 >>> c = Counter('gallahad') # a new counter from an iterable

Callers 15

modeFunction · 0.90
multimodeFunction · 0.90
_count_diff_hashableFunction · 0.90
test_ReversibleMethod · 0.90
test_basicsMethod · 0.90
test_initMethod · 0.90
test_totalMethod · 0.90
test_updateMethod · 0.90
test_copyingMethod · 0.90
test_conversionsMethod · 0.90

Calls

no outgoing calls

Tested by 15

test_ReversibleMethod · 0.72
test_basicsMethod · 0.72
test_initMethod · 0.72
test_totalMethod · 0.72
test_updateMethod · 0.72
test_copyingMethod · 0.72
test_conversionsMethod · 0.72