You wondered if counters are hard. Hell! You may not even have used counters at all. Let me tell you something. Despite what you think, Counters are awesome!
You do not believe me? Well, how do you count the frequency of each letter in the following string?
s = "ggggggieur8errhg3035gmfvnfeghrty84gkhgudvj83735rdjgndru383wdsjf"
One easy(!) way:
d = dict()
for c in s:
if d.get(c) is None:
d[c] = 1
else:
d[c] += 1
Not so elegant :( Not so performant :((
One easier way :)
from collections import Counter
letter_count = dict(Counter(s))
print(letter_count)
Easy, Elegant, Performant!
Next time you need to count something, think `Counters`
But, that’s not all. The same code will run if the string would have been a list in reality. And also here
some_elements = {'x': 3, 'y': 16, 'z': "Hello", 'a': "Hello}
value_count = dict(Counter(some_elements.values())
print(value_count) # What is the frequency of each value?
Before signing off
“That is very nice and good Shubhadeep, but what if my iterable consists of UN-hashable types. Such as this?”
unhash_test = [10, 5, 10, ['a', 10, 'b', 5], 15]
Well, we can still count the frequency of each of the characters. Although a bit round-about way
unhash_ctr = Counter(tuple(item) if isinstance(item, list) is True else item for item in unhash_test)
The result looks like this -
Counter({10: 2, 5: 1, ('a', 10, 'b', 5): 1, 15: 1})
“Ok Ok! I get it. Counters are great. But if this is just a list, then I could have just used count. Like so” -
d = [2, 3, 2, 4, 10, 83, 2, 3, 10]
d.count(2) # will print # of times 2 has occurred in the list
Right!
BUT
If you want to count more than one element then you start a loop and call `count` inside it for each of the element you want. This can be catastrophic. As `count` will go over the whole list for each call!
From Around the web
Free Python Books - https://github.com/pamoroso/free-python-books
Printstack: Add Stacktrace to print function - https://github.com/morefigs/printstack
Everyone is still terrible at creating software at scale - https://margint.blog/2021/04/05/creating-software-at-scale/
Downloading files from S3 with multithreading and Boto3 - https://emasquil.github.io/posts/multithreading-boto3/
Using PyTorch and NumPy? You're making a mistake - https://tanelp.github.io/posts/a-bug-that-plagues-thousands-of-open-source-ml-projects/