Dict lookup shortcut?

M

M. Clift

Hi All,

Can someone tell me is there a shorthand version to do this?

l1 = ['n1', 'n3', 'n1'...'n23'...etc...]

Names = {'n1':'Cuthbert','n2' :'Grub','n3' :'Dibble' etc...}

for name in l1:
print Names[name],

Rather than listing all the name+numbers keys in the dictionary can these
keys be shortened somehow into one key and a range?

Thanks,

M
 
D

Daniel Ellison

M. Clift said:
Hi All,

Can someone tell me is there a shorthand version to do this?

l1 = ['n1', 'n3', 'n1'...'n23'...etc...]

Names = {'n1':'Cuthbert','n2' :'Grub','n3' :'Dibble' etc...}

for name in l1:
print Names[name],

Rather than listing all the name+numbers keys in the dictionary can these
keys be shortened somehow into one key and a range?

Thanks,

M

I'm not sure what you're trying to do, but you should probably be doing
this instead:

names = {'n1':'Cuthbert', 'n2':'Grub', 'n3':'Dibble'}

for key in names.keys():
print names[key],

No need for the initial list. Alternatively, you could just have all the
names in a list to start with and iterate over that:

names = ['Cuthbert', 'Grub', 'Dibble']

for name in names:
print name,

Dan

NB: all code untested
 
M

Mitja

M. Clift said:
Hi All,

Can someone tell me is there a shorthand version to do
this?

l1 = ['n1', 'n3', 'n1'...'n23'...etc...]

Names = {'n1':'Cuthbert','n2' :'Grub','n3' :'Dibble'
etc...}

for name in l1:
print Names[name],

Rather than listing all the name+numbers keys in the
dictionary can these keys be shortened somehow into one
key and a range?

I'm not sure what you want to do:

l1= [...]
Names = {'n': l1}
print Names[n][12]

Or maybe you meant:

for i in range(1,24):
Names['n'+`i`]=blah

If that didn't answer your question, please rephrase it. And BTW, variables (like Names) are spelled lowercase (i.e., names) by
convention; it's only class names that usually get capitalized (though it's no fixed rule).
 
M

Michael Foord

M. Clift said:
Hi All,

Can someone tell me is there a shorthand version to do this?

l1 = ['n1', 'n3', 'n1'...'n23'...etc...]

Names = {'n1':'Cuthbert','n2' :'Grub','n3' :'Dibble' etc...}

for name in l1:
print Names[name],

Rather than listing all the name+numbers keys in the dictionary can these
keys be shortened somehow into one key and a range?

You could just use the integers as keys.

*Or* you could do :
alist = Names.items()
alist.sort()
alist[n][1] is the nth name
(alist[n] = (nth key, nth name) )

HTH

Fuzzy
 
B

Bengt Richter

Hi All,

Can someone tell me is there a shorthand version to do this?
First question is why you want to do "this" -- and what "this" really is ;-)
l1 = ['n1', 'n3', 'n1'...'n23'...etc...]
Where does that list of names come from?
Names = {'n1':'Cuthbert','n2' :'Grub','n3' :'Dibble' etc...}

for name in l1:
print Names[name],

Rather than listing all the name+numbers keys in the dictionary can these
keys be shortened somehow into one key and a range?

>>> Names = dict([('n%s'%(i+1), name) for i,name in enumerate('Cuthbert Grub Dibble'.split())])
>>> Names {'n1': 'Cuthbert', 'n2': 'Grub', 'n3': 'Dibble'}
>>> for key in Names: print Names[key],
...
Cuthbert Grub Dibble

I doubt that's what you really wanted to do, but who knows ;-)

BTW, in general, you can't depend on the order of keys gotten from a dictionary.

So if the dictionary pre-existed with those systematic sortable keys, you could
get them without knowing how many keys there were, and sort them instead of making
a manual list. E.g.,
>>> keys = Names.keys()
>>> keys.sort()
>>> for key in keys: print Names[key],
...
Cuthbert Grub Dibble

OTOH, if you are storing names in numerical order and want to retrieve them by
a key that represents their numerical position in the order, why not just use
a list and index it by numbers? E.g.
>>> NameList = 'Cuthbert Grub Dibble'.split()
>>> NameList ['Cuthbert', 'Grub', 'Dibble']
>>> NameList[0] 'Cuthbert'
>>> NameList[2]
'Dibble'

Plus, you don't have to bother with indices or sortable names at all
if you want to process them in order:
...
Cuthbert Grub Dibble

And if you do want an associated number, there's enumerate:
...
Name # 1: "Cuthbert"
Name # 2: "Grub"
Name # 3: "Dibble"

Notice that I added 1 to i in order to print starting with # 1, since enumerate starts with 0 ;-)

Regards,
Bengt Richter
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top