Quantcast
Channel: Active questions tagged ruby - Stack Overflow
Viewing all articles
Browse latest Browse all 4627

Equivalent of Python's Counter in Ruby

$
0
0

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']

Viewing all articles
Browse latest Browse all 4627

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>