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


Groups > comp.lang.python > #69555

Re: Retrieve item deep in dict tree?

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ethan@stoneleaf.us>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'represents': 0.05; 'tree': 0.05; 'nested': 0.07; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'message-id:@stoneleaf.us': 0.09; 'thus,': 0.09; '~ethan~': 0.09; 'def': 0.12; 'stored': 0.12; 'keys:': 0.16; 'path.': 0.16; 'received:69.93': 0.16; 'received:gateway01.websitewelcome.com': 0.16; 'roy': 0.16; 'subject:item': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'header:User- Agent:1': 0.23; 'helper': 0.24; 'nearly': 0.26; 'header:In-Reply- To:1': 0.27; 'function': 0.29; '(which': 0.31; "d'aprano": 0.31; 'keys': 0.31; 'steven': 0.31; 'no,': 0.35; 'but': 0.35; 'there': 0.35; 'sequence': 0.36; 'charset:us-ascii': 0.36; 'subject:?': 0.36; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'anything': 0.39; 'structure': 0.39; 'to:addr:python.org': 0.39; 'range': 0.61; 'received:173': 0.61; 'skip:* 10': 0.61; 'simple': 0.61; 'different': 0.65; 'smith': 0.68; 'short,': 0.84
Date Wed, 02 Apr 2014 13:25:24 -0700
From Ethan Furman <ethan@stoneleaf.us>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121010 Thunderbird/16.0.1
MIME-Version 1.0
To python-list@python.org
Subject Re: Retrieve item deep in dict tree?
References <mailman.8818.1396461529.18130.python-list@python.org> <533c70a5$0$2909$c3e8da3$76491128@news.astraweb.com>
In-Reply-To <533c70a5$0$2909$c3e8da3$76491128@news.astraweb.com>
Content-Type text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding 7bit
X-AntiAbuse This header was added to track abuse, please include it with any abuse report
X-AntiAbuse Primary Hostname - gator3304.hostgator.com
X-AntiAbuse Original Domain - python.org
X-AntiAbuse Originator/Caller UID/GID - [47 12] / [47 12]
X-AntiAbuse Sender Address Domain - stoneleaf.us
X-BWhitelist no
X-Source-IP 173.12.184.233
X-Source
X-Source-Args
X-Source-Dir
X-Source-Sender ([173.12.184.233]) [173.12.184.233]:40708
X-Source-Auth ethan+stoneleaf.us
X-Email-Count 1
X-Source-Cap dG9idWs7dG9idWs7Z2F0b3IzMzA0Lmhvc3RnYXRvci5jb20=
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.8821.1396471817.18130.python-list@python.org> (permalink)
Lines 33
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1396471817 news.xs4all.nl 2872 [2001:888:2000:d::a6]:40332
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:69555

Show key headers only | View raw


On 04/02/2014 01:18 PM, Steven D'Aprano wrote:
> On Wed, 02 Apr 2014 13:58:16 -0400, Roy Smith wrote:
>
>> 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?
>
> Er, idiomatic one liner? No, not really. But a helper function makes
> nearly anything into a one-liner:
>
> def traverse(tree, *keys):
>      t = tree
>      for k in keys:
>          t = t[k]
>      return t
>
> # one-liner
> leaf = traverse(tree, *list_of_keys)

+1

Short, simple -- good Python.  :)

--
~Ethan~

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


Thread

Retrieve item deep in dict tree? Roy Smith <roy@panix.com> - 2014-04-02 13:58 -0400
  Re: Retrieve item deep in dict tree? John Gordon <gordon@panix.com> - 2014-04-02 18:03 +0000
  Re: Retrieve item deep in dict tree? Steven D'Aprano <steve@pearwood.info> - 2014-04-02 20:18 +0000
    Re: Retrieve item deep in dict tree? Ethan Furman <ethan@stoneleaf.us> - 2014-04-02 13:25 -0700
  Re: Retrieve item deep in dict tree? Rustom Mody <rustompmody@gmail.com> - 2014-04-02 19:41 -0700
    Re: Retrieve item deep in dict tree? Rustom Mody <rustompmody@gmail.com> - 2014-04-02 21:15 -0700
      Re: Retrieve item deep in dict tree? Steven D'Aprano <steve@pearwood.info> - 2014-04-03 05:29 +0000
      Re: Retrieve item deep in dict tree? Ian Kelly <ian.g.kelly@gmail.com> - 2014-04-03 01:00 -0600

csiph-web