Re: Nested dictionaries from a list ?

2014-12-09 Thread Dave Angel
  To: Denis McMahon
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

--- SoupGate-Win32 v1.05
 * Origin: SpaceSST.BBS.Fidonetnntp.gatew...@.piz.noip.me (1:249/999)
--- Synchronet 3.15b-Win32 NewsLink 1.92
SpaceSST BBS Usenet  Fidonet Gateway
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Nested dictionaries from a list ?

2014-12-08 Thread Dave Angel

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
--
https://mail.python.org/mailman/listinfo/python-list


Re: Nested dictionaries from a list ?

2014-12-07 Thread Dave Angel

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
--
https://mail.python.org/mailman/listinfo/python-list


Re: Nested dictionaries from a list ?

2014-12-07 Thread Denis McMahon
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, denismfmcma...@gmail.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Nested dictionaries from a list ?

2014-12-07 Thread Denis McMahon
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, denismfmcma...@gmail.com
-- 
https://mail.python.org/mailman/listinfo/python-list