Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #82064 > unrolled thread
| Started by | Wacky <waqar.hafiz@gmail.com> |
|---|---|
| First post | 2014-12-07 08:18 -0800 |
| Last post | 2014-12-07 23:49 +0000 |
| Articles | 5 — 3 participants |
Back to article view | Back to comp.lang.python
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
| From | Wacky <waqar.hafiz@gmail.com> |
|---|---|
| Date | 2014-12-07 08:18 -0800 |
| Subject | Nested dictionaries from a list ? |
| Message-ID | <72066852-b4a1-4820-b52a-be56f68a0d7b@googlegroups.com> |
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
[toc] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2014-12-07 12:01 -0500 |
| Message-ID | <mailman.16678.1417971703.18130.python-list@python.org> |
| In reply to | #82064 |
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
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2014-12-07 23:52 +0000 |
| Message-ID | <m62p8f$fn7$3@dont-email.me> |
| In reply to | #82068 |
On Sun, 07 Dec 2014 12:01:26 -0500, Dave Angel wrote:
> On 12/07/2014 11:18 AM, Wacky wrote:
>> I've a list of users ....
> I haven't run this through the Python, so please forgive any typos.
> users = [ ....
> mess = { ....
users is redundant, as it's mess.keys()
maintaining a separate list of users and having the users as the keys in
mess suggests redundancy, and the potential for errors if the two data
items get out of synch. Better imo to just have the data in one place.
--
Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2014-12-08 08:35 -0500 |
| Message-ID | <mailman.16722.1418045741.18130.python-list@python.org> |
| In reply to | #82083 |
On 12/07/2014 06:52 PM, Denis McMahon wrote:
> On Sun, 07 Dec 2014 12:01:26 -0500, Dave Angel wrote:
>
>> On 12/07/2014 11:18 AM, Wacky wrote:
>
>>> I've a list of users ....
>
>> I haven't run this through the Python, so please forgive any typos.
>
>> users = [ ....
>> mess = { ....
>
> users is redundant, as it's mess.keys()
>
> maintaining a separate list of users and having the users as the keys in
> mess suggests redundancy, and the potential for errors if the two data
> items get out of synch. Better imo to just have the data in one place.
>
Unless there's an order that wants to be retained. But I would change
the list into a list of user objects, rather than of strings. And I'd
know that eventually it would be a sparse list (as users come and go,
and you don't want to reuse the indices). So it would be another
dictionary mapping userid and User instance.
--
DaveA
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2014-12-07 23:49 +0000 |
| Message-ID | <m62p1f$fn7$2@dont-email.me> |
| In reply to | #82064 |
On Sun, 07 Dec 2014 08:18:03 -0800, Wacky wrote:
> New to Python, so please go easy.
> I've a list of users, who have different profiles .....
How are you getting on with this assignment / homework?
I have a solution I could post, but I thought I'd wait to see what your
solution was first.
Here's a hint though, I defined the following functions to work on my
profiles data:
def is_user(user):
def is_machine(machine):
def is_user_of_machine(user, machine):
def is_machine_of_user(user, machine):
def query_machines_of_user(user):
def query_machines():
def query_users_of_machine(machine):
def query_users():
def add_profile(user, machine, profile):
def get_profile(user, machine):
def remove_profile(user, machine):
def remove_user(user):
After defining the functions, I was able to add the following code:
add_profile('Tom', 'computerA', 'Profile101')
add_profile('Tom', 'computerB', 'Profile102')
add_profile('Tom', 'computerC', 'Profile103')
add_profile('Dick', 'computerA', 'Profile389')
add_profile('Dick', 'computerB', 'Profile390')
add_profile('Harry', 'computerA', 'Profile201')
add_profile('Harry', 'computerB', 'Profile202')
add_profile('Harry', 'computerC', 'Profile203')
add_profile('Harry', 'computerD', 'Profile204')
print 'The users are:', query_users()
print 'The machines are:', query_machines()
print 'Users of computerC are:', query_users_of_machine('computerC')
print 'Harry\'s profile on computerB is:', get_profile('Harry',
'computerB')
print 'Tom uses the machines:', query_machines_of_user('Tom')
which generated the following output:
The users are: ['Harry', 'Dick', 'Tom']
The machines are: ['computerA', 'computerB', 'computerC', 'computerD']
Users of computerC are: ['Harry', 'Tom']
Harry's profile on computerB is: Profile202
Tom uses the machines: ['computerA', 'computerB', 'computerC']
Then I added functions to return all of a user's machines and profiles,
and all of a machine's users and profiles.
def query_machine_profiles(user):
def query_user_profiles(machine):
So I could add things like:
print 'The user profiles on computerB are:', query_user_profiles
('computerB')
and get the result:
The user profiles on computerB are: {'Tom': 'Profile102', 'Dick':
'Profile390', 'Harry': 'Profile202'}
By the way, my longest function definition used 7 lines of code, these
are all fairly simple functions, and no list comprehensions.
--
Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web