473,385 Members | 2,162 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Please explain collections.defaultdict(lambda: 1)

I'm reading http://norvig.com/spell-correct.html

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
Per http://docs.python.org/lib/defaultdict-examples.html

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.

Nov 6 '07 #1
2 5805
On Nov 6, 8:54 am, "metaperl.com" <metap...@gmail.comwrote:
I'm readinghttp://norvig.com/spell-correct.html

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

Perhttp://docs.python.org/lib/defaultdict-examples.html

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.
The explanation is a little further down on that same page, on the
discussion of "novel" words and avoiding the probablity of them being
0 just because they have not yet been seen in the training text.

-- Paul

Nov 6 '07 #2
"metaperl.com" <me******@gmail.comwrote:
Per http://docs.python.org/lib/defaultdict-examples.html

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.
The author explains his reasoning in the article: he wants to treat novel
words (i.e. those which did not appear in the training corpus) as having
been seen once.

Nov 6 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

53
by: Oliver Fromme | last post by:
Hi, I'm trying to write a Python function that parses an expression and builds a function tree from it (recursively). During parsing, lambda functions for the the terms and sub-expressions...
181
by: Tom Anderson | last post by:
Comrades, During our current discussion of the fate of functional constructs in python, someone brought up Guido's bull on the matter: http://www.artima.com/weblogs/viewpost.jsp?thread=98196 ...
8
by: rubbishemail | last post by:
Hello, I need your help understanding lambda (and doing it a better way without). f = lambda x : x*x # this is a list of functions
23
by: Kaz Kylheku | last post by:
I've been reading the recent cross-posted flamewar, and read Guido's article where he posits that embedding multi-line lambdas in expressions is an unsolvable puzzle. So for the last 15 minutes...
5
by: Octal | last post by:
How does the lambda library actually works. How does it know how to evaluate _1, how does it recognize _1 as a placeholder, how does it then calculate _1+_2, or _1+2 etc. The source files seem a...
3
by: Raymond Hettinger | last post by:
FWIW, here are three ways of writing constant functions for collections.defaultdict(): d = defaultdict(int) # slowest way; works only for zero d = defaultdict(lambda: 0) # faster way;...
2
by: tutufan | last post by:
It seems like x = defaultdict(defaultdict(list)) should do the obvious, but it doesn't. This seems to work y = defaultdict(lambda: defaultdict(list)) though is a bit uglier.
3
by: fizilla | last post by:
Hello all! I have the following weird problem and since I am new to Python I somehow cannot figure out an elegant solution. The problem reduces to the following question: How to pickle a...
21
by: globalrev | last post by:
i have a rough understanding of lambda but so far only have found use for it once(in tkinter when passing lambda as an argument i could circumvent some tricky stuff). what is the point of the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.