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? 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.
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 ;)
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
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
"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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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 :
...
|
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:...
|
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...
|
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?
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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...
|
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: 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...
|
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....
|
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...
| |