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


Groups > comp.lang.python > #82068

Re: Nested dictionaries from a list ?

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin1!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <davea@davea.name>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.015
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; 'python,': 0.02; 'forgive': 0.05; 'nested': 0.07; 'users,': 0.07; 'advance': 0.07; 'attributes': 0.09; 'mess': 0.09; 'tackle': 0.09; 'braces,': 0.16; 'partly': 0.16; 'subject:dictionaries': 0.16; 'subject: ?': 0.16; 'fix': 0.17; 'wrote:': 0.18; 'header:User-Agent:1': 0.23; 'example.': 0.24; "haven't": 0.24; "i've": 0.25; 'class.': 0.26; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'subject:list': 0.30; "skip:' 10": 0.31; 'keys': 0.31; "user's": 0.31; 'class': 0.32; 'lists': 0.32; 'run': 0.32; 'minimal': 0.33; "i'd": 0.34; 'subject:from': 0.34; 'something': 0.35; 'but': 0.35; 'thanks': 0.36; 'list': 0.37; 'whatever': 0.38; 'to:addr:python- list': 0.38; 'structure': 0.39; 'to:addr:python.org': 0.39; 'users': 0.40; 'how': 0.40; 'new': 0.61; "you'll": 0.62; 'making': 0.63; 'soon': 0.63; 'more': 0.64; 'different': 0.65; 'here': 0.66; 'received:74.208': 0.68; 'user,': 0.69; 'computers.': 0.84; 'correction,': 0.84; 'tolerant': 0.84; 'profiles': 0.91
Date Sun, 07 Dec 2014 12:01:26 -0500
From Dave Angel <davea@davea.name>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.4.0
MIME-Version 1.0
To python-list@python.org
Subject Re: Nested dictionaries from a list ?
References <72066852-b4a1-4820-b52a-be56f68a0d7b@googlegroups.com>
In-Reply-To <72066852-b4a1-4820-b52a-be56f68a0d7b@googlegroups.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Provags-ID V02:K0:sC6svito6HcHo51P9+k8dhyko5cNgn7ReUE1qrZv/Dy Ruacd4n7MZrvdKMy+36ao6jh+JfKYoTINLETugWXNJvTvU08uP 4axhvXQxduIqk6UPYKKhvgtpExwWHVrVMc/Nalx4Bb03sAoouS TEXGAvRsinlXWe+bsrZKH7kWXelYerauXNnltQuPORJ5oE40Ol 1ihMFaMNfA6m2S8h+tpdRU6wcNt+6h6o+HJYFuHm25Z2f2u1l7 lf5mKUJ7X5DDiPykRX7eiMNkEHNQ4fRWjbZqXFL/cE8kSTLgFw PcVeJ8/a/cQJY8mptTaxDWnvXo64mqDT9oR0RKWt25WhRZRAaH kAGUzv18GhXojq0Y641s=
X-UI-Out-Filterresults notjunk:1;
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.16678.1417971703.18130.python-list@python.org> (permalink)
Lines 57
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1417971703 news.xs4all.nl 2956 [2001:888:2000:d::a6]:54065
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:82068

Show key headers only | View raw


On 12/07/2014 11:18 AM, Wacky wrote:
> New to Python, so please go easy.
> I've a list of users, who have different profiles on different computers. How to tackle this through lists and dictionaries? Here is the data example. More interested in learning how to declare this structure and add/delete/extract values from whatever data structure is proposed.
>
> users = [ 'Tom', 'Dick', 'Harry' ]
>
> { 'Tom': { 'computerA: 'Profile101'
>             'computerB: 'Profile102'
>             'computerC: 'Profile103' }
>
> { 'Dick': { 'computerA: 'Profile389'
>              'computerB: 'Profile390' }
>
> { 'Harry': { 'computerA: 'Profile201'
>               'computerB: 'Profile202'
>               'computerC: 'Profile203'
>               'computerD: 'Profile204' }
>
> Thanks in advance
>

I haven't run this through the Python, so please forgive any typos.

Minimal correction, partly to fix missing commas and extra left curlies:

users = [ 'Tom', 'Dick', 'Harry' ]

mess = { 'Tom': { 'computerA: 'Profile101',
            'computerB: 'Profile102',
            'computerC: 'Profile103' },
  'Dick': { 'computerA: 'Profile389',
             'computerB: 'Profile390' },
  'Harry': { 'computerA: 'Profile201',
              'computerB: 'Profile202',
              'computerC: 'Profile203',
              'computerD: 'Profile204' }
}

And now to get a particular profile, you'd do something like:

mess["Tom"]["computerA"]

Or to get the nth user's profile, use

mess[users[n]]["computerC"]

Note that making this tolerant of missing keys is much trickier.

If it were myproblem, I'd not try to use triply nested braces, but make 
a class for the user, which has methods to look up profiles. Chances are 
that you'll soon have other attributes for those users, and you can then 
tack them into the same class.



-- 
DaveA

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


Thread

Nested dictionaries from a list ? Wacky <waqar.hafiz@gmail.com> - 2014-12-07 08:18 -0800
  Re: Nested dictionaries from a list ? Dave Angel <davea@davea.name> - 2014-12-07 12:01 -0500
    Re: Nested dictionaries from a list ? Denis McMahon <denismfmcmahon@gmail.com> - 2014-12-07 23:52 +0000
      Re: Nested dictionaries from a list ? Dave Angel <davea@davea.name> - 2014-12-08 08:35 -0500
  Re: Nested dictionaries from a list ? Denis McMahon <denismfmcmahon@gmail.com> - 2014-12-07 23:49 +0000

csiph-web