January 27, 2023

Python's Counter

The problem Python has a Counter object – it’s found in the collections module – and I’ve become convinced that it’s… a bit weird? The basic idea is fine: it’s a dictionary but the keys are things you want to count, and the values are the amount of the thing you have counted. The neat thing is if you initialise a counter with an iterable like a string or a list, it’ll give you the counts for the number of times each element appears. And it gives you convenience methods for finding the most common elements and that sort of thing. It’s pretty neat and I’ve used it a few times. Read more

© Seamus Bradley 2021–6

Powered by Hugo & Kiss.