B
bearophileHUGS
This post sums some things I have written in another Python newsgroup.
More than 40% of the times I use defaultdict like this, to count
things:
But I have seen that if keys are quite sparse, and int() becomes called
too much often, then code like this is faster:
.... if c in d: d[c] += 1
.... else: d[c] = 1
....{'a': 5, 'r': 2, 'b': 2, 'c': 1, 'd': 1}
So to improve the speed for such special but common situation, the
defaultdict can manage the case with default_factory=int in a different
and faster way.
Bye,
bearophile
More than 40% of the times I use defaultdict like this, to count
things:
defaultdict(<type 'int'>, {'a': 5, 'r': 2, 'b': 2, 'c': 1, 'd': 1})from collections import defaultdict as DD
s = "abracadabra"
d = DD(int)
for c in s: d[c] += 1 ....
d
But I have seen that if keys are quite sparse, and int() becomes called
too much often, then code like this is faster:
.... if c in d: d[c] += 1
.... else: d[c] = 1
....{'a': 5, 'r': 2, 'b': 2, 'c': 1, 'd': 1}
So to improve the speed for such special but common situation, the
defaultdict can manage the case with default_factory=int in a different
and faster way.
Bye,
bearophile