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


Groups > comp.lang.python > #21065

Re: Help needed: dynamically pull data from different levels of a dict

Path csiph.com!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <chris@rebertia.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.022
X-Spam-Evidence '*H*': 0.96; '*S*': 0.00; '"c"': 0.07; 'dict': 0.09; 'dynamically': 0.09; 'def': 0.13; 'simplified': 0.16; 'val': 0.16; 'worried': 0.16; '\xc2\xa0i': 0.16; "\xc2\xa0i'm": 0.16; 'cc:addr :python-list': 0.16; 'wed,': 0.17; 'subject:Help': 0.17; 'wrote:': 0.18; 'converted': 0.18; 'cheers,': 0.20; 'cc:no real name:2**0': 0.21; "doesn't": 0.22; 'header:In-Reply-To:1': 0.22; 'feb': 0.22; 'received:209.85.212.46': 0.23; 'received:mail- vw0-f46.google.com': 0.23; 'helper': 0.23; 'levels.': 0.23; 'pieces': 0.23; 'generic': 0.24; 'subject:data': 0.25; 'cc:2**0': 0.26; 'code': 0.26; 'figure': 0.26; 'bit': 0.28; "i'm": 0.28; 'message-id:@mail.gmail.com': 0.29; 'mapping': 0.29; 'example': 0.29; 'problem': 0.29; 'print': 0.29; 'cc:addr:python.org': 0.29; 'pm,': 0.29; 'objects.': 0.30; 'chris': 0.30; "i've": 0.32; 'received:209.85.212': 0.33; "can't": 0.33; 'it.': 0.33; 'hi,': 0.34; 'nested': 0.34; 'problem.': 0.35; 'something': 0.35; 'lists,': 0.35; 'like,': 0.37; 'pull': 0.37; 'but': 0.37; 'received:google.com': 0.37; 'received:209.85': 0.38; 'some': 0.38; 'data': 0.38; 'open': 0.38; 'e.g.': 0.39; "i'd": 0.39; 'subject:from': 0.39; 'received:209': 0.39; 'might': 0.40; 'header:Received:6': 0.61; 'your': 0.61; '29,': 0.73; 'introduce': 0.82; "'2',": 0.84; 'querying': 0.84; 'sender:addr:chris': 0.84; 'subject:different': 0.84; 'thin': 0.84; 'to:addr:hotmail.com': 0.89; 'fortunately': 0.91
Received-SPF pass (google.com: domain of chris@rebertia.com designates 10.52.90.20 as permitted sender) client-ip=10.52.90.20;
Authentication-Results mr.google.com; spf=pass (google.com: domain of chris@rebertia.com designates 10.52.90.20 as permitted sender) smtp.mail=chris@rebertia.com; dkim=pass header.i=chris@rebertia.com
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=a878ceygNLUxS+HnmGXwLO0Z5e5b6P85dJCXFIrkwts=; b=CU2COjvchF5S40OXEzi6QrHQx3xqL5qtCxAZ7/Q5bE9YU1H69P85gDRgSCEPm94gwy LDyVXYxZ0uf0hDunhkcKT06hcYXa6ASBisRvVv3f4+7ER9btrHlsVUR++LUjFqduxpSH SC6be31gAATPM/TZsxjGD1+lz6tO2BhP/tcy4=
MIME-Version 1.0
Sender chris@rebertia.com
In-Reply-To <BLU150-W94B1B3F5A35DDFE877D1A9C6C0@phx.gbl>
References <BLU150-W94B1B3F5A35DDFE877D1A9C6C0@phx.gbl>
Date Wed, 29 Feb 2012 20:43:44 -0800
X-Google-Sender-Auth 5XarX7mICwYuzjiAFsRmymlVrfk
Subject Re: Help needed: dynamically pull data from different levels of a dict
From Chris Rebert <clp2@rebertia.com>
To Peter Rubenstein <peter.rubenstein@hotmail.com>
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
X-Gm-Message-State ALoCoQmt5kFG0Bs3CtvtPHxAT1u/8mwsCmSFSVp3PqOJS5QoRoEaY/Qrt44JjsauFD4eXSv8lQpG
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.311.1330577028.3037.python-list@python.org> (permalink)
Lines 57
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1330577028 news.xs4all.nl 6917 [2001:888:2000:d::a6]:38672
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:21065

Show key headers only | View raw


On Wed, Feb 29, 2012 at 7:56 PM, Peter Rubenstein
<peter.rubenstein@hotmail.com> wrote:
> Hi,
>
> I'd appreciate a bit of help on this problem.  I have some data that I've
> converted to a dict and I want to pull out individual pieces of it.
>
> Simplified version--
>
> a={'1':'a', '2':'b', '3':{4:'d'}, '5':{'6': {'7': [ {'8':'e'}, {'9':'f'} ] }
> } }
>
> I'd like to be able to code something like:
>
> data_needed = ['1', '2', '3:4', '5:6:7-8']
> for val in data_needed:
>     answer=extract_from(a,val)
>     print answer
>
> And get:
> a
> b
> c

"c" apparently sprung completely out of thin air...

> d
> e
>
> Problem is I can't figure out what extract_from would look like, such that
> it would proceed dynamically down to the right level.  I'm open to the
> possibility that I'm not approaching this the right way.

data_needed = ['1', '2', ('3', '4'), ('5', '6', '7', 0, '8')]

def extract_from(mapping, key_path):
    current = mapping
    for key in key_path:
        current = current[key]
    return current


I would perhaps try and restructure the data into something less
generic than nested dicts & lists, e.g. objects. You then might be
able to introduce helper querying methods.
I would also be worried about Law of Demeter violations, but
fortunately your concrete example doesn't reach any deeper than 2
levels.

Cheers,
Chris
--
http://chrisrebert.com

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


Thread

Re: Help needed: dynamically pull data from different levels of a dict Chris Rebert <clp2@rebertia.com> - 2012-02-29 20:43 -0800

csiph-web