Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #15598

Re: Get keys from a dicionary

From John Gordon <gordon@panix.com>
Newsgroups comp.lang.python
Subject Re: Get keys from a dicionary
Date 2011-11-11 17:28 +0000
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <j9jm0k$e6n$1@reader1.panix.com> (permalink)
References <8f5215a8-d08f-4355-a5a2-77fcaa32c92d@j10g2000vbe.googlegroups.com> <1e00ab59-8fc5-4bd7-b52c-f98f3b0b4473@x8g2000yql.googlegroups.com> <aac0b123-673b-4d8f-bc05-1f639515a951@c18g2000yqj.googlegroups.com>

Show all headers | View raw


In <aac0b123-673b-4d8f-bc05-1f639515a951@c18g2000yqj.googlegroups.com> macm <moura.mario@gmail.com> writes:

> >>> myDict = {}
> >>> myDict['foo'] = {}
> >>> myDict['foo']['bar'] = 'works'

> -----

> >>> def myFunction( MyObj ):
> ...	# MyObj is a nested dicionary (normaly 2 steps like myDict['foo']
> ['bar'])
> ...	# I want inspect this MyObj
> ...	# what keys was pass
> ...	print MyObj.keys() ## WRONG
> ...	# So What I want is :
> ...	# return foo bar

> ----------------

> >>> result = myFunction( myDict['foo']['bar'] )
> >>> result

> Should print :

> ... foo bar

I don't think there's a simple way to do what you want.

You could inspect the whole dictionary to find the keys that map to a
given value, like so:

def MyFunction(mydict, x):
  for k1 in mydict:
    for k2 in mydict[k1]:
      if mydict[k1][k2] == x:
        return "%s %s" % (k1, k2)

>>> print MyFunction(myDict, 'works')
>>> foo bar

-- 
John Gordon                   A is for Amy, who fell down the stairs
gordon@panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Get keys from a dicionary macm <moura.mario@gmail.com> - 2011-11-11 05:31 -0800
  Re: Get keys from a dicionary Jon Clements <joncle@googlemail.com> - 2011-11-11 08:09 -0800
    Re: Get keys from a dicionary macm <moura.mario@gmail.com> - 2011-11-11 08:33 -0800
      Re: Get keys from a dicionary macm <moura.mario@gmail.com> - 2011-11-11 08:38 -0800
      Re: Get keys from a dicionary Dave Angel <d@davea.name> - 2011-11-11 11:47 -0500
      Re: Get keys from a dicionary John Gordon <gordon@panix.com> - 2011-11-11 17:28 +0000
  Re: Get keys from a dicionary John Gordon <gordon@panix.com> - 2011-11-11 16:25 +0000
    Re: Get keys from a dicionary macm <moura.mario@gmail.com> - 2011-11-11 08:36 -0800
  Re: Get keys from a dicionary Gelonida N <gelonida@gmail.com> - 2011-11-11 18:29 +0100
  Re: Get keys from a dicionary Gelonida N <gelonida@gmail.com> - 2011-11-11 18:45 +0100
    Re: Get keys from a dicionary John Gordon <gordon@panix.com> - 2011-11-11 17:51 +0000
  Re: Get keys from a dicionary alex23 <wuwei23@gmail.com> - 2011-11-13 21:42 -0800
  Re: Get keys from a dicionary alex23 <wuwei23@gmail.com> - 2011-11-13 21:44 -0800
  Multilevel dicts/arrays v. tuples as keys? [Was: Re: Get keys from a dicionary] Matej Cepl <mcepl@redhat.com> - 2011-11-14 11:05 +0100
    Re: Multilevel dicts/arrays v. tuples as keys? [Was: Re: Get keys from a dicionary] Tim Golden <mail@timgolden.me.uk> - 2011-11-14 10:42 +0000
    Re: Multilevel dicts/arrays v. tuples as keys? Peter Otten <__peter__@web.de> - 2011-11-14 11:47 +0100
      Re: Multilevel dicts/arrays v. tuples as keys? alex23 <wuwei23@gmail.com> - 2011-11-14 19:07 -0800

csiph-web