Back
Close

Computing with Data

elgeish
134K views

Counters

Python provides a dict subclass called Counter, which is mainly used for frequency-counting of its keys:

from collections import Counter
counts = Counter(apples=3, oranges=5, bananas=1)
print(counts)
# Example: split the text into a list of words then count them
counts = Counter('to be or not to be'.split())
print(counts)
print(counts['question']) # counters can be used as sparse vectors
# Example: repeat the keys as many times as their respective counts
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants