RE: newbie: dictionary - howto get key value

Discussion in 'Python' started by Michael.Coll-Barth@VerizonWireless.com, Mar 10, 2005.

  1. Guest

    how about?

    test = 3 #find person with this number
    for x in xrange(len(phone.keys())):
    print x
    if phone[phone.keys()[x]] == test:
    print phone.keys()[x]
    break

    Being a newbie myself, I'd love a little critique on the above. Be kind as
    I don't know what else needs to be done in Gerhard's process. Of course, we
    could put this loop in a call and return the name


    -----Original Message-----
    From:
    python-list-bounces+michael.coll-barth=
    [mailto:python-list-bounces+michael.coll-barth=verizonwireless.com@pytho
    n.org]On Behalf Of G. Völkl
    Sent: Thursday, March 10, 2005 12:19 PM
    To:
    Subject: newbie: dictionary - howto get key value


    Hello,

    I use a dictionary:

    phone = {'mike':10,'sue':8,'john':3}

    phone['mike'] --> 10

    I want to know who has number 3?

    3 --> 'john'

    How to get it in the python way ?

    Thanks
    Gerhard


    --
    http://mail.python.org/mailman/listinfo/python-list
    ___________________________________________________________________
    The information contained in this message and any attachment may be
    proprietary, confidential, and privileged or subject to the work
    product doctrine and thus protected from disclosure. If the reader
    of this message is not the intended recipient, or an employee or
    agent responsible for delivering this message to the intended
    recipient, you are hereby notified that any dissemination,
    distribution or copying of this communication is strictly prohibited.
    If you have received this communication in error, please notify me
    immediately by replying to this message and deleting it and all
    copies and backups thereof. Thank you.
    , Mar 10, 2005
    #1
    1. Advertising

  2. <> wrote in message
    news:...

    > test = 3 #find person with this number
    > for x in xrange(len(phone.keys())):
    > print x
    > if phone[phone.keys()[x]] == test:
    > print phone.keys()[x]
    > break
    >
    >Being a newbie myself, I'd love a little critique on the above. Be kind


    Constructs like xrange(len(something)) are fairly typical for somone
    moving to Python from another language; usually there is a more
    idiomatic alternative.

    In this case, writing "for k in phone.keys()" would be a good start.
    Richard Brodie, Mar 10, 2005
    #2
    1. Advertising

  3. wrote:

    (top-post corrected)
    >>
    >> -----Original Message-----
    >> From:
    >> python-list-bounces+michael.coll-barth=
    >> [mailto:python-list-bounces+michael.coll-barth=verizonwireless.com@pytho
    >> n.org]On Behalf Of G. Völkl
    >> Sent: Thursday, March 10, 2005 12:19 PM
    >> To:
    >> Subject: newbie: dictionary - howto get key value
    >>
    >>
    >> Hello,
    >>
    >> I use a dictionary:
    >>
    >> phone = {'mike':10,'sue':8,'john':3}
    >>
    >> phone['mike'] --> 10
    >>
    >> I want to know who has number 3?
    >>
    >> 3 --> 'john'
    >>
    >> How to get it in the python way ?


    > how about?
    >
    > test = 3 #find person with this number
    > for x in xrange(len(phone.keys())):
    > print x
    > if phone[phone.keys()[x]] == test:
    > print phone.keys()[x]
    > break
    >
    > Being a newbie myself, I'd love a little critique on the above.


    0/ does not retrieve all the information (just the first match)
    1/ not reusable (hint : make it a function)
    2/ does not retrieve the information, just print it
    3/ also print some useless informations ('print x')
    4/ makes len(phone.keys()) + 1 calls to phone.keys()
    hint :
    for key in phone.keys():
    if phone[key] == test:
    print phone[key]
    or better:
    for key, value in phone.items():
    if value == test:
    print key

    > Be kind as
    > I don't know what else needs to be done in Gerhard's process.


    Hope I haven't been to harsh !-)

    > Of course, we
    > could put this loop in a call and return the name


    yeps.

    --
    bruno desthuilliers
    python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
    p in ''.split('@')])"
    bruno modulix, Mar 10, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Finney
    Replies:
    15
    Views:
    500
    John Machin
    Aug 6, 2003
  2. G. Völkl
    Replies:
    9
    Views:
    404
    Peter Otten
    Mar 14, 2005
  3. M P
    Replies:
    1
    Views:
    465
  4. Une bévue
    Replies:
    5
    Views:
    148
    Une bévue
    Aug 10, 2006
  5. Antonio Quinonez
    Replies:
    2
    Views:
    167
    Antonio Quinonez
    Aug 14, 2003
Loading...

Share This Page