473,320 Members | 2,088 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,320 software developers and data experts.

dicts & lists together

I have a dict that associates strings to numbers like this {'abc': 1,
'xyz':2, '123':3,...} I am then given a list of strings ['abc', 'xyz',
'123',...] and asked to associate a number to each string in the list
according to the string's value in the dict. I am at a loss as to how to
do this, can someone show me where to start?
Jul 18 '05 #1
5 1107
Brad Tilley wrote:
I have a dict that associates strings to numbers like this {'abc': 1,
'xyz':2, '123':3,...} I am then given a list of strings ['abc', 'xyz',
'123',...] and asked to associate a number to each string in the list
according to the string's value in the dict. I am at a loss as to how to
do this, can someone show me where to start?

xdict={'abc': 1, 'xyz':2, '123':3}
xlist=['abc', 'xyz', '123']
tlist=[xdict.get(v, None) for v in xlist]
print tlist

[1, 2, 3]

Larry Bates
Syscon, Inc.
Jul 18 '05 #2
Larry Bates wrote:
Brad Tilley wrote:
I have a dict that associates strings to numbers like this {'abc': 1,
'xyz':2, '123':3,...} I am then given a list of strings ['abc', 'xyz',
'123',...] and asked to associate a number to each string in the list
according to the string's value in the dict. I am at a loss as to how
to do this, can someone show me where to start?


>>> xdict={'abc': 1, 'xyz':2, '123':3}
>>> xlist=['abc', 'xyz', '123']
>>> tlist=[xdict.get(v, None) for v in xlist]
>>> print tlist

[1, 2, 3]

Larry Bates
Syscon, Inc.


Thank you Larry, I don't understand it, but it works ;)
Jul 18 '05 #3
On Tue, 02 Nov 2004 13:45:38 -0500, Brad Tilley <br********@gmail.com> wrote:
I have a dict that associates strings to numbers like this {'abc': 1,
'xyz':2, '123':3,...} I am then given a list of strings ['abc', 'xyz',
'123',...] and asked to associate a number to each string in the list
according to the string's value in the dict. I am at a loss as to how to
do this, can someone show me where to start?


Associate as in list of number,string pairs?
dct = {'abc':1, 'xyz':2, '123':3}
strings = ['abc','xyz','123','error']
[(dct[s],s) for s in strings] Traceback (most recent call last):
File "<stdin>", line 1, in ?
KeyError: 'error' [(dct.get(s, 99999),s) for s in strings]

[(1, 'abc'), (2, 'xyz'), (3, '123'), (99999, 'error')]

Regards,
Bengt Richter
Jul 18 '05 #4
Brad Tilley a écrit :
Larry Bates wrote:
Brad Tilley wrote:
I have a dict that associates strings to numbers like this {'abc': 1,
'xyz':2, '123':3,...} I am then given a list of strings ['abc',
'xyz', '123',...] and asked to associate a number to each string in
the list according to the string's value in the dict. I am at a loss
as to how to do this, can someone show me where to start?

>>> xdict={'abc': 1, 'xyz':2, '123':3}
>>> xlist=['abc', 'xyz', '123']
>>> tlist=[xdict.get(v, None) for v in xlist]
>>> print tlist

[1, 2, 3]

Larry Bates
Syscon, Inc.

Thank you Larry, I don't understand it, but it works ;)


If you don't understand what Larry's solution does, you probably should
have a look there :

http://www.python.org/doc/2.3.4/tut/...00000000000000

or look in the web for "list comprehensions" in Python.

Pierre
Jul 18 '05 #5
"Brad Tilley" <br********@gmail.com>
I have a dict that associates strings to numbers like this {'abc': 1,
'xyz':2, '123':3,...} I am then given a list of strings ['abc', 'xyz',
'123',...] and asked to associate a number to each string in the list
according to the string's value in the dict. I am at a loss as to how to
do this, can someone show me where to start?


Is this a homework assignment?
Raymond Hettinger
Jul 18 '05 #6

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

Similar topics

4
by: Ben | last post by:
Hi all, I'm trying to figure out how how complex map, filter and reduce work based on the following piece of code from http://www-106.ibm.com/developerworks/linux/library/l-prog.html : ...
19
by: John Keeling | last post by:
Dear all, I tried the test program below. My interest is to examine timing differences between insert vs. append & reverse for a list. My results on my XP Python 2.3.4 are as follows:...
0
by: sean peters | last post by:
Hi all, i've been weighing the pros and cons of running multiple concurrent mysqld's on one server, to have better control over what databases are on what physical disks. System: 4 processor sun...
9
by: Dave H | last post by:
Hello, I have a query regarding definition lists. Is it good practice semantically to use the dt and dd elements to mark up questions and answers in a frequently asked questions list, or FAQ? ...
3
by: s_subbarayan | last post by:
Dear all, 1)In one of our implementation for an application we are supposed to collate two linked lists.The actual problem is like this: There are two singularly linked lists, the final output...
9
by: rbt | last post by:
What's a good way to write a dictionary out to a file so that it can be easily read back into a dict later? I've used realines() to read text files into lists... how can I do the same thing with...
27
by: Chad | last post by:
The problem is: Write a recursive version of the function reverse(s), which reverses the string s in place. In "The C Answer Book", Second Edition, near the bottom of page 95, the authors say...
6
by: bearophileHUGS | last post by:
I have found that in certain situations ordered dicts are useful. I use an Odict class written in Python by ROwen that I have improved and updated some for personal use. So I'm thinking about a...
4
by: rn5a | last post by:
I have a ListBox which should list all the files & directories that exist in a particular directory. The problem is I can get the ListBox to list either all the files or all the directories but not...
0
by: Chris Rebert | last post by:
On Thu, Oct 16, 2008 at 12:19 PM, John Townsend <jtownsen@adobe.comwrote: Right, this clobbers the existing entry with this new blank one. This is evidenced by the fact that you're performing an...
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...
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)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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...

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.