473,287 Members | 1,616 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,287 software developers and data experts.

help on dictionary

Hi,

I have a defaultdict which I have initialized as follows:

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

def_dict = train(large_set_of_words)

where large_set_of_words is the list of possible strings.

I'd like to use such a default_dict as follows:

return max(list_of_strings, key=lambda w: dic[w])

that is, among the strings in list_of_strings, I'd like to return the
one with the highest score in def_dict.

The problem with such approach is that if the list_of_strings contains
a string that is not part of the def_dict, such a string gets added to
it while I'd like to keep the original def_dict unchanged/frozen.

Do you have any suggestion on how to do that?

Thanks and regards
Francesco

P.S. the code above is part from Norvig's spelling corrector

Nov 1 '07 #1
1 1023
On Nov 1, 11:21 pm, cesco <fd.calabr...@gmail.comwrote:
Hi,

I have a defaultdict which I have initialized as follows:

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

def_dict = train(large_set_of_words)

where large_set_of_words is the list of possible strings.

I'd like to use such a default_dict as follows:

return max(list_of_strings, key=lambda w: dic[w])

that is, among the strings in list_of_strings, I'd like to return the
one with the highest score in def_dict.

The problem with such approach is that if the list_of_strings contains
a string that is not part of the def_dict, such a string gets added to
it while I'd like to keep the original def_dict unchanged/frozen.

Do you have any suggestion on how to do that?
Use .get instead of [], and use sensible variable names:
return max(words, key=lambda word: model.get(word, 1))

--
Paul Hankin

Nov 1 '07 #2

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

Similar topics

2
by: dont bother | last post by:
Hi Buddies, I am facing this problem and I dont know what to use as EOF in python: I want to read a file, and put all the individual words in a dictionary with their index: For example if the...
4
by: dont bother | last post by:
This is really driving me crazy. I have a dictionary feature_vectors{}. I try to sort its keys using #apply sorting on feature_vectors sorted_feature_vector=feature_vectors.keys()...
1
by: ±è»ó¿Ï | last post by:
I'm a newbie in Python and Python/C API.. I have a simple question. I want to access python dictionary data type from C extension module. ---- spam.c ---- #include <Python.h> PyObject...
8
by: rh0dium | last post by:
Hi all, I have a dict which looks like this.. dict={'130nm': {'umc': }, '180nm': {'chartered': , 'tsmc': }, '250nm': {'umc': , 'tsmc': } }
3
by: aking | last post by:
Dear Python people, im a newbie to python and here...so hello! Im trying to iterate through values in a dictionary so i can find the closest value and then extract the key for that...
1
hpbutterbeer
by: hpbutterbeer | last post by:
We have a Machine Project and my brain is currently in a clouded state. Sorry, I'm just a beginner in C Programming... Text twist is a windows game whose main objective is to form words out of the...
2
by: muchexie | last post by:
i have two scripts that are not running to reset a password that has been forgotten and the other to change old password. here are the scripts. change_passwd.php session_start();...
6
by: djm | last post by:
i know this may seem really stupid and silly, but i just cant figure out whats the problem with this coding. any help from you guys will be greatly appreciated. thanks ive the header file and the...
2
by: hcaptech | last post by:
This is my Test.can you help me ? 1.Which of the following statement about C# varialble is incorrect ? A.A variable is a computer memory location identified by a unique name B.A variable's name...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.