Feb-21-2021, 06:36 PM
One of the very few uses of counter that make it almost worth the disk space it occupies. It was brand new for Python 2.7
I would use dict.get(key, default) which is a safe way to retrieving a value from a dictionary. If the key is not found .get() returns the default value instead of raising a key error. Sorry, but I cannot make myself write this in Python 2.7.
I would use dict.get(key, default) which is a safe way to retrieving a value from a dictionary. If the key is not found .get() returns the default value instead of raising a key error. Sorry, but I cannot make myself write this in Python 2.7.
from random import randint frequency = {} for i in range(1000): num = randint(1, 10) frequency[num] += frequency.get(num, 0) print(frequency)