Python has the Counter class in the collections
module. It is a class for counting hashable objects. E.g.:
cnt = Counter()cnt['Anna'] += 3cnt['John'] += 2cnt['Anna'] += 4print(cnt)=> Counter({'Anna': 7, 'John': 2})print(cnt['Mario'])=> 0
What is the equivalent of Counter
in Ruby?
EDIT:
The Counter
class provides also the following mathematical operations and helper methods:
c = Counter(a=3, b=1)d = Counter(a=1, b=2)c + d => Counter({'a': 4, 'b': 3})c - d=> Counter({'a': 2})c.most_common(1)=> ['a']