[Tutor] Input is Dictionary1, Output is Dictionary2 (using keys and values of Dictionary1)
David L Neil
PyTutor at DancesWithMice.info
Thu Jan 30 17:51:38 EST 2020
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
More information about the Tutor
mailing list