[Tutor] Re: How to get a key from dictionary?
Matthew Sherborne
miracle@paradise.net.nz
Wed, 27 Mar 2002 08:28:38 +1200
This is a multi-part message in MIME format.
------=_NextPart_000_002A_01C1D569.652C29C0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Sorry to be a show off, but I reckon that this way may be faster. Sorry =
again. The other replies are very helpful. :)
def findKey(val):
for key, value in dict.items():
if value =3D=3D val: return key
return None
=20
GBU
Matthew Sherborne
----- Original Message -----=20
From: "Don Arnold" <darnold02@sprynet.com>
To: <printers@sendme.cz>; <python-list@python.org>; <tutor@python.org>; =
<activepython@listserv.activestate.com>; <python-help@python.org>
Sent: Tuesday, March 26, 2002 11:51 AM
Subject: Re: How to get a key from dictionary?
> >>> targetval =3D 2
> >>> dict =3D {'aa':1, 'bb':2, 'cc': 3, 'dd': 1}
> >>> targetval =3D 2
> >>> for key in dict.keys():
> if dict[key] =3D=3D targetval:
> print "found", targetval, "at key", key
>=20
> found 2 at key bb
> >>> targetval =3D 1
> >>> for key in dict.keys():
> if dict[key] =3D=3D targetval:
> print "found", targetval, "at key", key
>=20
> found 1 at key aa
> found 1 at key dd
>=20
> ----- Original Message -----
> From: "A" <printers@sendme.cz>
> To: <python-list@python.org>; <tutor@python.org>;
> <activepython@listserv.activestate.com>; <python-help@python.org>
> Sent: Monday, March 25, 2002 4:16 PM
> Subject: How to get a key from dictionary?
>=20
>=20
> > Hi,
> > Is there a possibility to get, from a dictionary, a key according to =
a
> > value ?
> > For example
> > I have a dictionary
> >
> > dict=3D{'aa':1,'bb':2}
> >
> > and
> > dict['aa']
> > is 1
> >
> > But how can I for value 1 find out key? (That is here 'aa')
> >
> > Thank you for help
> > Ladislav
> >
> >
> > _______________________________________________
> > ActivePython mailing list
> > ActivePython@listserv.ActiveState.com
> > To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
>=20
> _______________________________________________
> ActivePython mailing list
> ActivePython@listserv.ActiveState.com
> To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
>=20
------=_NextPart_000_002A_01C1D569.652C29C0
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2712.300" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY>
<DIV><FONT face=3DArial size=3D2>Sorry to be a show off, but I reckon =
that this way=20
may be faster. Sorry again. The other replies are very helpful. =20
:)</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT><FONT face=3DArial =
size=3D2></FONT> </DIV>
<DIV><FONT face=3DCourier size=3D2>def findKey(val):</FONT></DIV>
<DIV><FONT face=3DCourier size=3D2> for key, value in=20
dict.items():</FONT></DIV>
<DIV><FONT face=3DCourier size=3D2> if value =3D=3D =
val: return=20
key</FONT></DIV>
<DIV><FONT face=3DCourier size=3D2> return None</FONT></DIV>
<DIV><FONT face=3DArial size=3D2> </FONT></DIV>
<DIV><FONT face=3DArial size=3D2>GBU</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>Matthew Sherborne</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>----- Original Message ----- =
</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>From: "Don Arnold" <</FONT><A=20
href=3D"mailto:darnold02@sprynet.com"><FONT face=3DArial=20
size=3D2>darnold02@sprynet.com</FONT></A><FONT face=3DArial =
size=3D2>></FONT></DIV>
<DIV><FONT face=3DArial size=3D2>To: <</FONT><A=20
href=3D"mailto:printers@sendme.cz"><FONT face=3DArial=20
size=3D2>printers@sendme.cz</FONT></A><FONT face=3DArial size=3D2>>; =
<</FONT><A=20
href=3D"mailto:python-list@python.org"><FONT face=3DArial=20
size=3D2>python-list@python.org</FONT></A><FONT face=3DArial =
size=3D2>>;=20
<</FONT><A href=3D"mailto:tutor@python.org"><FONT face=3DArial=20
size=3D2>tutor@python.org</FONT></A><FONT face=3DArial size=3D2>>; =
<</FONT><A=20
href=3D"mailto:activepython@listserv.activestate.com"><FONT face=3DArial =
size=3D2>activepython@listserv.activestate.com</FONT></A><FONT =
face=3DArial=20
size=3D2>>; <</FONT><A =
href=3D"mailto:python-help@python.org"><FONT face=3DArial=20
size=3D2>python-help@python.org</FONT></A><FONT face=3DArial=20
size=3D2>></FONT></DIV>
<DIV><FONT face=3DArial size=3D2>Sent: Tuesday, March 26, 2002 11:51 =
AM</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>Subject: Re: How to get a key from=20
dictionary?</FONT></DIV>
<DIV><FONT face=3DArial><BR><FONT size=3D2></FONT></FONT></DIV><FONT =
face=3DArial=20
size=3D2>> >>> targetval =3D 2<BR>> >>> dict =3D =
{'aa':1,=20
'bb':2, 'cc': 3, 'dd': 1}<BR>> >>> targetval =3D 2<BR>>=20
>>> for key in dict.keys():<BR>>=20
if dict[key] =
=3D=3D=20
targetval:<BR>>=20
print =
"found", targetval, "at key", key<BR>> <BR>> found 2 at key =
bb<BR>>=20
>>> targetval =3D 1<BR>> >>> for key in =
dict.keys():<BR>>=20
if dict[key] =
=3D=3D=20
targetval:<BR>>=20
print =
"found", targetval, "at key", key<BR>> <BR>> found 1 at key =
aa<BR>>=20
found 1 at key dd<BR>> <BR>> ----- Original Message -----<BR>> =
From:=20
"A" <</FONT><A href=3D"mailto:printers@sendme.cz"><FONT face=3DArial=20
size=3D2>printers@sendme.cz</FONT></A><FONT face=3DArial =
size=3D2>><BR>> To:=20
<</FONT><A href=3D"mailto:python-list@python.org"><FONT face=3DArial=20
size=3D2>python-list@python.org</FONT></A><FONT face=3DArial =
size=3D2>>;=20
<</FONT><A href=3D"mailto:tutor@python.org"><FONT face=3DArial=20
size=3D2>tutor@python.org</FONT></A><FONT face=3DArial =
size=3D2>>;<BR>>=20
<</FONT><A =
href=3D"mailto:activepython@listserv.activestate.com"><FONT=20
face=3DArial =
size=3D2>activepython@listserv.activestate.com</FONT></A><FONT=20
face=3DArial size=3D2>>; <</FONT><A =
href=3D"mailto:python-help@python.org"><FONT=20
face=3DArial size=3D2>python-help@python.org</FONT></A><FONT =
face=3DArial=20
size=3D2>><BR>> Sent: Monday, March 25, 2002 4:16 PM<BR>> =
Subject: How to=20
get a key from dictionary?<BR>> <BR>> <BR>> > Hi,<BR>> =
> Is=20
there a possibility to get, from a dictionary, a key according to =
a<BR>> >=20
value ?<BR>> > For example<BR>> > I have a =
dictionary<BR>>=20
><BR>> > dict=3D{'aa':1,'bb':2}<BR>> ><BR>> > =
and<BR>>=20
> dict['aa']<BR>> > is 1<BR>> ><BR>> > But how can =
I for=20
value 1 find out key? (That is here 'aa')<BR>> =
><BR>> >=20
Thank you for help<BR>> > Ladislav<BR>> ><BR>> =
><BR>> >=20
_______________________________________________<BR>> > =
ActivePython=20
mailing list<BR>> > </FONT><A=20
href=3D"mailto:ActivePython@listserv.ActiveState.com"><FONT face=3DArial =
size=3D2>ActivePython@listserv.ActiveState.com</FONT></A><BR><FONT =
face=3DArial=20
size=3D2>> > To unsubscribe: </FONT><A=20
href=3D"http://listserv.ActiveState.com/mailman/mysubs"><FONT =
face=3DArial=20
size=3D2>http://listserv.ActiveState.com/mailman/mysubs</FONT></A><BR><FO=
NT=20
face=3DArial size=3D2>> <BR>>=20
_______________________________________________<BR>> ActivePython =
mailing=20
list<BR>> </FONT><A =
href=3D"mailto:ActivePython@listserv.ActiveState.com"><FONT=20
face=3DArial =
size=3D2>ActivePython@listserv.ActiveState.com</FONT></A><BR><FONT=20
face=3DArial size=3D2>> To unsubscribe: </FONT><A=20
href=3D"http://listserv.ActiveState.com/mailman/mysubs"><FONT =
face=3DArial=20
size=3D2>http://listserv.ActiveState.com/mailman/mysubs</FONT></A><BR><FO=
NT=20
face=3DArial size=3D2>> </FONT></BODY></HTML>
------=_NextPart_000_002A_01C1D569.652C29C0--