473,322 Members | 1,846 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,322 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 1024
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: 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...
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...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.