I have this dictionary defined by:
def train(features):
model = collections.defaultdict(lambda: 1)
for f in features:
model[f] += 1
return model
Later along the way, I want to to use pickle and dump the dictionary into a text file:
f = open('dict.txt', 'wb')
pickle.dump(Nwords, f)
However the code doesn't work and I receive an error. Apparently pickle
can't work with lambda
and I'm better off defining the model
using a module-level function. I have already read the answers here
Unfortunately as I am not experienced with Python I am not exactly sure how to do this. I tried:
def dd():
return defaultdict(int)
def train(features):
## model = defaultdict(lambda: 1)
model = defaultdict(dd)
for f in features:
model[f] += 1
return model
I receive the error:
TypeError: unsupported operand type(s) for +=: 'collections.defaultdict' and 'int'
Other than that, return defaultdict(int)
would always assign a zero to the first occurrence of a key, whereas I want it to assign 1. Any ideas on how I can fix this?