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


Groups > comp.lang.python > #69598

Re:Retrieve item deep in dict tree?

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 <yimr00@gmail.com>
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 <yimr00@gmail.com>
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 <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.8847.1396547789.18130.python-list@python.org> (permalink)
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

Show key headers only | View raw


[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.

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


Thread

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

csiph-web