Home | History | Annotate | Download | only in collections

Lines Matching refs:Counter

8 * Counter      dict subclass for counting hashable objects
18 'UserString', 'Counter', 'OrderedDict', 'ChainMap']
454 ### Counter
468 class Counter(dict):
473 >>> c = Counter('abcdeabcdabcaba') # count elements from a string
494 >>> d = Counter('simsalabim') # make another counter
495 >>> c.update(d) # add in the second counter
499 >>> c.clear() # empty the counter
501 Counter()
504 in the counter until the entry is deleted or the counter is cleared:
506 >>> c = Counter('aaabbc')
520 '''Create a new, empty Counter object. And if given, count elements
524 >>> c = Counter() # a new, empty counter
525 >>> c = Counter('gallahad') # a new counter from an iterable
526 >>> c = Counter({'a': 4, 'b': 2}) # a new counter from a mapping
527 >>> c = Counter(a=4, b=2) # a new counter from keyword args
531 raise TypeError("descriptor '__init__' of 'Counter' object "
536 super(Counter, self).__init__()
540 'The count of elements not in the Counter is zero.'
548 >>> Counter('abcdeabcdabcaba').most_common(3)
560 >>> c = Counter('ABCABC')
565 >>> prime_factors = Counter({2: 2, 3: 3, 17: 1})
586 'Counter.fromkeys() is undefined. Use Counter(iterable) instead.')
591 Source can be an iterable, a dictionary, or another Counter instance.
593 >>> c = Counter('which')
595 >>> d = Counter('watch')
596 >>> c.update(d) # add elements from another counter
609 raise TypeError("descriptor 'update' of 'Counter' object "
622 super(Counter, self).update(iterable) # fast path when counter is empty
633 Source can be an iterable, a dictionary, or another Counter instance.
635 >>> c = Counter('which')
637 >>> c.subtract(Counter('watch')) # subtract elements from another counter
645 raise TypeError("descriptor 'subtract' of 'Counter' object "
690 # To strip negative and zero counts, add-in an empty counter:
691 # c += Counter()
696 >>> Counter('abbb') + Counter('bcc')
697 Counter({'b': 4, 'c': 2, 'a': 1})
700 if not isinstance(other, Counter):
702 result = Counter()
715 >>> Counter('abbbc') - Counter('bccd')
716 Counter({'b': 2, 'a': 1})
719 if not isinstance(other, Counter):
721 result = Counter()
734 >>> Counter('abbb') | Counter('bcc')
735 Counter({'b': 3, 'c': 2, 'a': 1})
738 if not isinstance(other, Counter):
740 result = Counter()
754 >>> Counter('abbb') & Counter('bcc')
755 Counter({'b': 1})
758 if not isinstance(other, Counter):
760 result = Counter()
769 'Adds an empty counter, effectively stripping negative and zero counts'
770 result = Counter()
777 '''Subtracts from an empty counter. Strips positive and zero counts,
781 result = Counter()
795 '''Inplace add from another counter, keeping only positive counts.
797 >>> c = Counter('abbb')
798 >>> c += Counter('bcc')
800 Counter({'b': 4, 'c': 2, 'a': 1})
808 '''Inplace subtract counter, but keep only results with positive counts.
810 >>> c = Counter('abbbc')
811 >>> c -= Counter('bccd')
813 Counter({'b': 2, 'a': 1})
821 '''Inplace union is the maximum of value from either counter.
823 >>> c = Counter('abbb')
824 >>> c |= Counter('bcc')
826 Counter({'b': 3, 'c': 2, 'a': 1})
838 >>> c = Counter('abbb')
839 >>> c &= Counter('bcc')
841 Counter({'b': 1})