Re: newbie: dictionary - howto get key value

2005-03-14 Thread Peter Otten
Joal Heagney wrote: > Does python guarantee that the lists given by phone.values() and > phone.keys() are in mutual order? Or is it possible that python will > return the lists in different orders for .values() and .keys()? Yes. Quoted from http://docs.python.org/lib/typesmapping.html: Keys and

Re: newbie: dictionary - howto get key value

2005-03-13 Thread Bengt Richter
On Mon, 14 Mar 2005 05:02:25 GMT, Joal Heagney <[EMAIL PROTECTED]> wrote: >Tim Roberts wrote: >> "G. Völkl" <[EMAIL PROTECTED]> wrote: >> >>>I use a dictionary: >>> >>>phone = {'mike':10,'sue':8,'john':3} >>> >>>phone['mike'] --> 10 >>> >>>I want to know who has number 3? >>> >>>3 --> 'john' >

Re: newbie: dictionary - howto get key value

2005-03-13 Thread Joal Heagney
Tim Roberts wrote: "G. Völkl" <[EMAIL PROTECTED]> wrote: 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 ? If you need to do this a lot, just keep two dictionaries, where the keys in each

Re: newbie: dictionary - howto get key value

2005-03-12 Thread Bengt Richter
On Thu, 10 Mar 2005 18:56:41 +0100, bruno modulix <[EMAIL PROTECTED]> wrote: >G. Völkl wrote: >> 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' > >Note that you can have many keys

Re: newbie: dictionary - howto get key value

2005-03-12 Thread Peter Hansen
John Machin wrote: G. Völkl wrote: I use a dictionary: phone = {'mike':10,'sue':8,'john':3} Of course in the real world using given name as a unique key is ludicrous: 'mike' is a.k.a. 'michael' (or 'mikhail' or 'michele' (which may be a typo for 'michelle')), and if there's only one 'sue' in your l

Re: newbie: dictionary - howto get key value

2005-03-11 Thread John Machin
bruno modulix wrote: > G. Völkl wrote: > > 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' > > Note that you can have many keys with the same value: > phone = {'mike':10,'sue':

Re: newbie: dictionary - howto get key value

2005-03-11 Thread Tim Roberts
"G. Völkl" <[EMAIL PROTECTED]> wrote: > >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 ? If you need to do this a lot, just keep two dictionaries, where the keys in each

Re: newbie: dictionary - howto get key value

2005-03-10 Thread Richard Brodie
<[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > 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

Re: newbie: dictionary - howto get key value

2005-03-10 Thread bruno modulix
[EMAIL PROTECTED] wrote: (top-post corrected) -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] n.org]On Behalf Of G. Völkl Sent: Thursday, March 10, 2005 12:19 PM To: python-list@python.org Subject: newbie: dictionary - howto get key value Hello, I use a dictionary: phon

Re: newbie: dictionary - howto get key value

2005-03-10 Thread Jeremy Jones
G. Völkl wrote: 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 How 'bout a list comprehension: In [1]:phone = {'mike':10,'sue':8,'john':3, 'billy':3} In [

Re: newbie: dictionary - howto get key value

2005-03-10 Thread bruno modulix
G. Völkl wrote: 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' Note that you can have many keys with the same value: phone = {'mike':10,'sue':8,'john':3, 'jack': 3, 'helen' : 10} How to get it in the python way

Re: newbie: dictionary - howto get key value

2005-03-10 Thread Diez B. Roggisch
phone = {'mike':10,'sue':8,'john':3} print [key for key, value in phone.items() if value == 3] -> ['john'] -- Regards, Diez B. Roggisch -- http://mail.python.org/mailman/listinfo/python-list

RE: newbie: dictionary - howto get key value

2005-03-10 Thread Michael . Coll-Barth
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