import collections
word_count = collections.defaultdict(int)
for word in ["foo", "bar", "foo", "foobar"]:
word_count[word] += 1
print word_count
# >>> defaultdict(<type 'int'>, {'foobar': 1, 'foo': 2, 'bar': 1})
If you don't know what key is given to the dictionary type, it is often the case that if the key does not exist, it is initialized with foo, and collections.defaultdict fills it. If you do defaultdict (int)
, it will be initialized to 0 at the first access like ↑, but what does this mean after all?
import collections
word_count = {}
for word in ["foo", "bar", "foo", "foobar"]:
if not word in word_count:
word_count[word] = int()
word_count[word] += 1
print word_count
The process is the same as this. After all, it's fine if it is a function with no arguments and returns some value. So it's pretty easy to understand if you use lambda and do something like defaultdict (lambda: [])
. ** Addendum: In the example of generating this empty list, it is easier to read if you pass list () without using lambda. A slightly more complicated example below where you should use lambda. ** **
If you develop the previous example, you can count the frequency of appearance of each alphabet.
import collections
word_count = collections.defaultdict(lambda: [0 for i in range(26)])
for word in ["foo", "bar", "foo", "foobar"]:
for chara in word:
word_count[word][ord(chara)-ord("a")] += 1
print word_count
# >defaultdict(<function <lambda> at 0x10e5147d0>, {'foobar': [1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0], 'foo': [0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 'bar': [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0]})
defaultdict () itself also returns defaultdict, so you can of course defaultdict (defaultdict (lambda: {"name ":" null "," age ": 20}))
.
Recommended Posts