Dict lookup shortcut?

Discussion in 'Python' started by M. Clift, Oct 12, 2004.

  1. M. Clift

    M. Clift Guest

    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
     
    M. Clift, Oct 12, 2004
    #1
    1. Advertisements

  2. 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
     
    Daniel Ellison, Oct 12, 2004
    #2
    1. Advertisements

  3. M. Clift

    Mitja Guest

    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).
     
    Mitja, Oct 12, 2004
    #3
  4. 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
     
    Michael Foord, Oct 12, 2004
    #4
  5. First question is why you want to do "this" -- and what "this" really is ;-)
    Where does that list of names come from?
    ...
    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.,
    ...
    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.
    '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
     
    Bengt Richter, Oct 13, 2004
    #5
  6. M. Clift

    M. Clift Guest

    Hi,

    Thankyou all for your replies, they are of great help.

    Malcolm
     
    M. Clift, Oct 13, 2004
    #6
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.