dicts & lists together

B

Brad Tilley

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?
 
L

Larry Bates

Brad said:
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.
 
B

Brad Tilley

Larry said:
Brad said:
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 ;)
 
B

Bengt Richter

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) for s in strings]

Traceback (most recent call last):
File said:
>>> [(dct.get(s, 99999),s) for s in strings]
[(1, 'abc'), (2, 'xyz'), (3, '123'), (99999, 'error')]

Regards,
Bengt Richter
 
P

Pierre Barbier de Reuille

Brad Tilley a écrit :
Larry said:
Brad said:
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/node7.html#SECTION007140000000000000000

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

Pierre
 
R

Raymond Hettinger

Brad Tilley said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top