Please explain collections.defaultdict(lambda: 1) metaperl at
Tue Nov 6 15:54:38 CET 2007

I'm reading

and do not understand the expression listed in the subject which is
part of this function:

def train(features):
    model = collections.defaultdict(lambda: 1)
    for f in features:
      model[f] += 1
    return model


It seems that there is a default factory which initializes each key to
1. So by the end of train(), each member of the dictionary model will
have value >= 1

But why wouldnt he set the value to zero and then increment it each
time a "feature" (actually a word) is encountered? It seems that each
model value would be 1 more than it should be.

More information about the Python-list mailing list