Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.kamp.net!newsfeed.kamp.net!feeder1.cambriumusenet.nl!feed.tweaknews.nl!85.12.40.138.MISMATCH!xlned.com!feeder5.xlned.com!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.025 X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; 'represents': 0.05; 'tree': 0.05; 'nested': 0.07; 'thus,': 0.09; 'python': 0.11; 'stored': 0.12; 'dict': 0.16; 'path.': 0.16; 'roy': 0.16; 'subject:item': 0.16; 'wed,': 0.18; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; '----------': 0.26; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; '(which': 0.31; 'keys': 0.31; 'date:': 0.34; 'received:google.com': 0.35; 'there': 0.35; 'sequence': 0.36; 'method': 0.36; 'subject:?': 0.36; 'list': 0.37; 'to:addr:python- list': 0.38; 'skip:- 10': 0.38; 'forwarded': 0.39; 'subject:': 0.39; 'structure': 0.39; 'to:addr:python.org': 0.39; 'range': 0.61; 'different': 0.65; 'here': 0.66; 'smith': 0.68; '2.7.': 0.84; 'tricky': 0.84; 'subject::': 0.85 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:cc:content-type; bh=WtlXuiIFcZ8m3iUowHzv8hY8lYhkOePNp3nPOKCjz6g=; b=cphNrrKbffzx7a7Rt4rJxeXCeYAxyrz15RZCSQK+qp36dDGPlN/VIjN2OT15nRdtKC bob6zXZkXPHBqzH/DmLC3W9J62FI3/cn/Vf/Z3MeGvr6rHGUYim1RxsGpLpkyfvT7HdV HS1FpBlzk9Gr5w2Hwzv+hc2uUZRt7hM0jQXJ2+EHDVH3jFEKNPBeGIFl0Ndle6PnRpY9 iPwF46VRoO5qUbo7aFmo3vVGUDwm2XWzlLaIZ+6Ean0CfnCxu3JWuP8VocJBOUQlBGEm X7a03/d7UKDksRsCK99ImgBCPUETWFaIaiSnikpfGpzC4KwZWpIsEo5CDumDk++ES18j vCjQ== MIME-Version: 1.0 X-Received: by 10.224.114.209 with SMTP id f17mr8586157qaq.40.1396543598920; Thu, 03 Apr 2014 09:46:38 -0700 (PDT) Date: Fri, 4 Apr 2014 00:46:38 +0800 Subject: Re:Retrieve item deep in dict tree? From: Yimr Zero To: python-list@python.org Content-Type: multipart/alternative; boundary=047d7bdc19ac2087f304f6262530 X-Mailman-Approved-At: Thu, 03 Apr 2014 19:56:28 +0200 Cc: roy@panix.com X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 61 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1396547789 news.xs4all.nl 2955 [2001:888:2000:d::a6]:36016 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:69598 --047d7bdc19ac2087f304f6262530 Content-Type: text/plain; charset=ISO-8859-1 Here is a tricky method : reduce(lambda x,y:x[str(y)],keys,tree) ---------- Forwarded message ---------- From: Roy Smith To: Python List 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. --047d7bdc19ac2087f304f6262530 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
Here= is a tricky method :

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


<= br>
---------- Forwarded message ----------=
From: Roy Smith <roy@panix.com>
To: Python List <python-list@python.org>
Cc:=A0
Date: Wed, 2 Apr 2014 13:58:16 -04= 00
Subject: Retrieve item deep in dict tree?
<= div class=3D"gmail_extra">I have a big hairy data structure which is a tree= of nested dicts. =A0I have a sequence of strings which represents a path t= hrough the tree. =A0Different leaves in the tree will be at different depth= s (which range from 1 to about 4 or 5 at most). =A0I want to get the value = stored at that path. =A0Thus, if

keys =3D [&= #39;foo', 'bar', 'baz']

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

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

I'm using Python = 2.7.

--047d7bdc19ac2087f304f6262530--