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


Groups > comp.lang.python > #69598 > unrolled thread

Re:Retrieve item deep in dict tree?

Started byYimr Zero <yimr00@gmail.com>
First post2014-04-04 00:46 +0800
Last post2014-04-04 00:46 +0800
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python


Contents

  Re:Retrieve item deep in dict tree? Yimr Zero <yimr00@gmail.com> - 2014-04-04 00:46 +0800

#69598 — Re:Retrieve item deep in dict tree?

FromYimr Zero <yimr00@gmail.com>
Date2014-04-04 00:46 +0800
SubjectRe:Retrieve item deep in dict tree?
Message-ID<mailman.8847.1396547789.18130.python-list@python.org>

[Multipart message — attachments visible in raw view] — view raw

Here is a tricky method :

reduce(lambda x,y:x[str(y)],keys,tree)



---------- Forwarded message ----------
From: Roy Smith <roy@panix.com>
To: Python List <python-list@python.org>
Cc:
Date: Wed, 2 Apr 2014 13:58:16 -0400
Subject: Retrieve item deep in dict tree?
I have a big hairy data structure which is a tree of nested dicts.  I have
a sequence of strings which represents a path through the tree.  Different
leaves in the tree will be at different depths (which range from 1 to about
4 or 5 at most).  I want to get the value stored at that path.  Thus, if

keys = ['foo', 'bar', 'baz']

I want to retrieve tree['foo']['bar']['baz'].

Is there some idiomatic, non-cryptic way to write that as a one-liner?

I'm using Python 2.7.

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web