I am creating a program that takes a dictionary with departments and who is in it, for example this dictionary here:
{"local": ["admin", "userA"],
"public": ["admin", "userB"],
"administrator": ["admin"] }
And then returns a dictionary with all users as keys and a list of departments they are in as the value.
For example, the output for the previously given list is supposed to be:
{"admin" : ["local", "public", "administrator"],
"userA" : ["local"],
"userB" : ["public"]}
Here is my attempt:
def groups_per_user(group_dictionary):
user_groups = {}
# Go through group_dictionary
user_groups["admin"] = []
user_groups["userA"] = []
user_groups["userB"] = []
for x in group_dictionary.values():
# Now go through the users in the group
for user in x:
if user in group_dictionary["local"]:
user_groups[user].append("local")
elif user in group_dictionary["public"]:
user_groups[user].append("public")
elif user in group_dictionary["administrator"]:
user_groups[user].append("administrator")
# Now add the group to the the list of
# groups for this user, creating the entry
# in the dictionary if necessary
return(user_groups)
print(groups_per_user({"local": ["admin", "userA"],
"public": ["admin", "userB"],
"administrator": ["admin"] }))
However, I am getting this output:
{'admin': ['local', 'local', 'local'],
'userA': ['local'],
'userB': ['public']}
In addition, I am defining the users in my function on lines 5-7 of my code. What am I supposed to do to make sure my code works for any given department and any given user?
For example, a user called "owner" and a department called "groupWorkspace" wouldn't work because I didn't define them in my code. Can someone explain how to fix this?