[Tutor] Input is Dictionary1, Output is Dictionary2 (using keys and values of Dictionary1)

Panchanathan Suresh suresh.gm at gmail.com
Thu Jan 30 18:10:59 EST 2020


Thanks a lot Mats and David! I am not yet that familiar with data
structures.

This statement indeed works: user_groups[user].append(group)

But how can we use 'append' keyword for a dictionary? Is not append used
only for appending to a List?
Is it because Group is a already a List in the original dictionary? This is
a bit confusing...

Can we append to the list of dictionary values for a given key, using
Append command?


On Thu, Jan 30, 2020 at 2:52 PM <tutor-request at python.org> wrote:

> Send Tutor mailing list submissions to
>         tutor at python.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
>         https://mail.python.org/mailman/listinfo/tutor
> or, via email, send a message with subject or body 'help' to
>         tutor-request at python.org
>
> You can reach the person managing the list at
>         tutor-owner at python.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Tutor digest..."
> Today's Topics:
>
>    1. Regarding learning python (faslur rahman)
>    2. Input is Dictionary1, Output is Dictionary2 (using keys and
>       values of Dictionary1) (Panchanathan Suresh)
>    3. Re: Regarding learning python (Alan Gauld)
>    4. Re: Input is Dictionary1, Output is Dictionary2 (using keys
>       and values of Dictionary1) (Mats Wichmann)
>    5. Re: Input is Dictionary1, Output is Dictionary2 (using keys
>       and values of Dictionary1) (David L Neil)
>
>
>
> ---------- Forwarded message ----------
> From: faslur rahman <frahmanf89 at gmail.com>
> To: tutor at python.org
> Cc:
> Bcc:
> Date: Thu, 30 Jan 2020 20:22:19 +0300
> Subject: [Tutor] Regarding learning python
> I am faslur rahman from Sri Lanka, last six months i was trying to learn
> python programming by my self, But unfortunately i did not found any good
> resource to learn about python. Even i try from python website, there I got
> some books to refer and this email ID also.
> can you please advise me where to learn python full course?.
>
> Thank you
>
>
>
>
> ---------- Forwarded message ----------
> From: Panchanathan Suresh <suresh.gm at gmail.com>
> To: tutor at python.org
> Cc:
> Bcc:
> Date: Thu, 30 Jan 2020 12:23:47 -0800
> Subject: [Tutor] Input is Dictionary1, Output is Dictionary2 (using keys
> and values of Dictionary1)
> Hi Everyone,
>
> I spent a lot of time on this, trying out many things, but I am unable to
> create a new dictionary with the users as keys and a list of their groups
> as values.
>
> Input Dictionary is {"local": ["admin", "userA"],"public":  ["admin",
> "userB"],"administrator": ["admin"] }
> Expected Output Dictionary is {"userA": ["admin","public"], "userB":
> ["admin"], "administrator": ["admin"]}
>
> --- How to create an unique group for a particular user?
> --- And then how to add this user key and the user's value (which will be
> the list of groups the user belongs to)
>
> *****
> def groups_per_user(group_dictionary):
>         user_groups = {}
>         groups = []
>         # Go through group_dictionary
>         for group,user in group_dictionary.items():
>                 # Now go through the users in the group
>                 for user1 in user:
>                         groups.append(group)
>                         print(user1,groups)
>
>         return(user_groups)
>
> print(groups_per_user({"local": ["admin", "userA"],"public":  ["admin",
> "userB"],"administrator": ["admin"] }))
>
> *****
>
> Thanks so much
>
>
>
>
> ---------- Forwarded message ----------
> From: Alan Gauld <alan.gauld at yahoo.co.uk>
> To: tutor at python.org
> Cc:
> Bcc:
> Date: Thu, 30 Jan 2020 21:32:58 +0000
> Subject: Re: [Tutor] Regarding learning python
> On 30/01/2020 17:22, faslur rahman wrote:
> > I am faslur rahman from Sri Lanka, last six months i was trying to learn
> > python programming by my self, But unfortunately i did not found any good
> > resource to learn about python.
>
> It would help if you can be more specific about what was
> wrong with the resources you tried.
>
> Also what is your background? Can you program in other languages,
> is it just python you ate struggling with? Or are you new to
> programming itself, in which case there are a lot of concepts
> to learn as well as how they are done in python.
>
> >  Even i try from python website, there I got
> > some books to refer and this email ID also.
>
> The official tutorial is very good if you can already program
> in another language. But it is not so good for total beginners.
> Thats where the non programmers page comes into play...
>
> > can you please advise me where to learn python full course?.
>
> Well, obviously I can't resist the opportunity to push my
> tutorial - see my signature below... :-)
>
> And if you get stuck (in any tutorial, not just mine) ask
> specific questions here. Send the code that doesn't work,
> any errors you get, tell us what you expected to happen
> and what did happen./ Then we can help/explain.
>
> Also tell us whether you are using Python 2 or 3.
> I strongly recommend using Python 3 and making sure
> your chosen tutorial also uses it.
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
>
>
>
>
>
>
> ---------- Forwarded message ----------
> From: Mats Wichmann <mats at wichmann.us>
> To: tutor at python.org
> Cc:
> Bcc:
> Date: Thu, 30 Jan 2020 14:47:15 -0700
> Subject: Re: [Tutor] Input is Dictionary1, Output is Dictionary2 (using
> keys and values of Dictionary1)
> On 1/30/20 1:23 PM, Panchanathan Suresh wrote:
> > Hi Everyone,
> >
> > I spent a lot of time on this, trying out many things, but I am unable to
> > create a new dictionary with the users as keys and a list of their groups
> > as values.
> >
> > Input Dictionary is {"local": ["admin", "userA"],"public":  ["admin",
> > "userB"],"administrator": ["admin"] }
> > Expected Output Dictionary is {"userA": ["admin","public"], "userB":
> > ["admin"], "administrator": ["admin"]}
> >
> > --- How to create an unique group for a particular user?
> > --- And then how to add this user key and the user's value (which will be
> > the list of groups the user belongs to)
> >
> > *****
> > def groups_per_user(group_dictionary):
> >         user_groups = {}
> >         groups = []
> >         # Go through group_dictionary
> >         for group,user in group_dictionary.items():
> >                 # Now go through the users in the group
> >                 for user1 in user:
> >                         groups.append(group)
> >                         print(user1,groups)
> >
> >         return(user_groups)
> >
> > print(groups_per_user({"local": ["admin", "userA"],"public":  ["admin",
> > "userB"],"administrator": ["admin"] }))
> >
> > *****
>
> Hopefully this isn't a homework problem :)
>
> You're not adding anything to your new dictionary. What you want to do
> is use the user as a key, and a list of groups as the value; the trick
> is that you may need to add several times to a user's entry, so you need
> to handle both the case of "user not yet in dictionary" and the case of
> "user has a grouplist, add to that list".  As a quick and dirty hack,
> like this (I changed a name a bit for clarity):
>
>     for group, userlist in group_dictionary.items():
>         # Now go through the users in the group
>         for user in userlist:
>             try:
>                 user_groups[user].append(group)
>             except KeyError:
>                 user_groups[user] = [group]
>
>
>
>
>
> ---------- Forwarded message ----------
> From: David L Neil <PyTutor at danceswithmice.info>
> To: tutor at python.org
> Cc:
> Bcc:
> Date: Fri, 31 Jan 2020 11:51:38 +1300
> Subject: Re: [Tutor] Input is Dictionary1, Output is Dictionary2 (using
> keys and values of Dictionary1)
> Greetings Suresh!
> What is your level of Python skill?
> ... skills with data structures?
>
>
> On 31/01/20 9:23 AM, Panchanathan Suresh wrote:
> > Hi Everyone,
> >
> > I spent a lot of time on this, trying out many things, but I am unable to
> > create a new dictionary with the users as keys and a list of their groups
> > as values.
> >
> > Input Dictionary is {"local": ["admin", "userA"],"public":  ["admin",
> > "userB"],"administrator": ["admin"] }
> > Expected Output Dictionary is {"userA": ["admin","public"], "userB":
> > ["admin"], "administrator": ["admin"]}
> >
> > --- How to create an unique group for a particular user?
> > --- And then how to add this user key and the user's value (which will be
> > the list of groups the user belongs to)
> >
> > *****
> > def groups_per_user(group_dictionary):
> >          user_groups = {}
> >          groups = []
> >          # Go through group_dictionary
> >          for group,user in group_dictionary.items():
> >                  # Now go through the users in the group
> >                  for user1 in user:
> >                          groups.append(group)
> >                          print(user1,groups)
> >
> >          return(user_groups)
> >
> > print(groups_per_user({"local": ["admin", "userA"],"public":  ["admin",
> > "userB"],"administrator": ["admin"] }))
> >
> > *****
>
>
> This idea might be a bit more than you really want/are ready to try:
>
> The problem with inheriting from dict and list in Python
>
> https://treyhunner.com/2019/04/why-you-shouldnt-inherit-from-list-and-dict-in-python/
>
> Although the article addresses a completely different set of interests,
> the example used is class TwoWayDict( ... ) which implements a dict
> having not only entries in the form key:value, but adds the
> complementary?inverted value:key.
>
> He calls it "bi-directional": "When a key-value pair is added, the key
> maps to the value but the value also maps to the key.".
>
> This is not *exactly* the spec in this post, in that Trey is using a
> single dict, but there is absolutely no reason why the article's
> solution(s) couldn't be extended so that the TwoWayDict class actually
> manages two dicts - one key:value and the other value:key.
>
> This would then allow extension to the 1:m relationship*:
>         forward_dict ~ key1:value1,value2,value3
>         inverted_dict ~ value1:key1,key2
>                         value2:key2,key5
>                         ...
>
> The philosophical difference in this approach lies in the *assembling*
> of a data structure that it is prepared for future use/'ready for
> action'; rather than using the algorithmic 'translation' currently
> envisaged, in-between assembling the original dict, and being able to
> make use of its inversion!
> ie the complexity is in the 'writing' to the dict (or 'loading' it),
> rather than in the 'reading' (or getting items from it)!
>
> OTOH understanding and coding the above will require some
> reasonably-advanced Python knowledge and skills - hence the opening
> questions!
>
>
> * apologies if this term is unfamiliar: "one to many relationship",
> refers to the idea that (here) one key may yield multiple values, cf
> "one to one", eg your ID/SocialSecurityNR which can only apply to you
> (and vice-versa).
>
> NB PSL's collections.UserDict stores its (single dict) keys and values
> in a dict called "data" (IIRC). Thus adding a parallel, but reversed,
> dict, eg "inverse" should be quite smooth to implement.
> (YMMV ? famous last words)
> --
> Regards =dn
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> https://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list