how to modify in dictionaries?
Question:
The groups_per_user function receives a dictionary, which contains group names with the list of users. Users can belong to multiple groups. Fill in the blanks to return a dictionary with the users as keys and a list of their groups as values
def groups_per_user(group_dictionary):
user_groups = {}
# Go through group_dictionary
for ___:
# Now go through the users in the group
for ___:
# Now add the group to 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"] }))
Answers:
def groups_per_user(group_dictionary):
user_groups = {}
# Go through group_dictionary
for group in group_dictionary:
# Now go through the users in the group
for user in group_dictionary[group]:
try:
user_groups[user].append(group)
except KeyError:
user_groups[user] = [group]
# Now add the group to the list of
# groups for this user, creating the entry
# in the dictionary if necessary
return(user_groups)
def groups_per_user(group_dictionary):
user_groups = {}
for group in group_dictionary:
for user in group_dictionary[group]:
if user not in user_groups:
user_groups[user] = []
if group not in user_groups[user]:
user_groups[user].append(group)
return user_groups
mylist = {"local": ["admin", "userA"],
"public": ["admin", "userB"],
"administrator": ["admin"] }
print(groups_per_user(mylist))
# {'admin': ['local', 'public', 'administrator'], 'userA': ['local'], 'userB': ['public']}
Try this:
def groups_per_user(group_dictionary):
user_groups = {}
for grp, users in group_dictionary.items():
for user in users:
if user in user_groups:
user_groups[user].append(grp)
else:
user_groups[user] = [grp]
return (user_groups)
Output of your print
statement:
{'admin': ['local', 'public', 'administrator'], 'userA': ['local'], 'userB': ['public']}
def groups_per_user(group_dictionary):
user_groups = {}
for key,users in group_dictionary.items():
for user in users:
user_groups[user]=[]
for key in user_groups.keys():
for ke,value in group_dictionary.items():
for i in value:
if(key==i):
user_groups[key].append(ke)
return(user_groups)
print(groups_per_user({"local": ["admin", "userA"],"public"["admin","userB"],"administrator": ["admin"] }))
def groups_per_user(group_dictionary):
user_groups = {}
# Go through group_dictionary
for key,value in group_dictionary.items():
# Now go through the users in the group
for letter in value:
if letter in user_groups:
user_groups[letter] += [key]
else :
user_groups[letter] = [key]
return(user_groups)
print(groups_per_user({"local": ["admin", "userA"],
"public": ["admin", "userB"],
"administrator": ["admin"] }))
The groups_per_user function receives a dictionary, which contains group names with the list of users. Users can belong to multiple groups. Fill in the blanks to return a dictionary with the users as keys and a list of their groups as values
def groups_per_user(group_dictionary):
user_groups = {}
# Go through group_dictionary
for ___:
# Now go through the users in the group
for ___:
# Now add the group to 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"] }))
def groups_per_user(group_dictionary):
user_groups = {}
# Go through group_dictionary
for group in group_dictionary:
# Now go through the users in the group
for user in group_dictionary[group]:
try:
user_groups[user].append(group)
except KeyError:
user_groups[user] = [group]
# Now add the group to the list of
# groups for this user, creating the entry
# in the dictionary if necessary
return(user_groups)
def groups_per_user(group_dictionary):
user_groups = {}
for group in group_dictionary:
for user in group_dictionary[group]:
if user not in user_groups:
user_groups[user] = []
if group not in user_groups[user]:
user_groups[user].append(group)
return user_groups
mylist = {"local": ["admin", "userA"],
"public": ["admin", "userB"],
"administrator": ["admin"] }
print(groups_per_user(mylist))
# {'admin': ['local', 'public', 'administrator'], 'userA': ['local'], 'userB': ['public']}
Try this:
def groups_per_user(group_dictionary):
user_groups = {}
for grp, users in group_dictionary.items():
for user in users:
if user in user_groups:
user_groups[user].append(grp)
else:
user_groups[user] = [grp]
return (user_groups)
Output of your print
statement:
{'admin': ['local', 'public', 'administrator'], 'userA': ['local'], 'userB': ['public']}
def groups_per_user(group_dictionary):
user_groups = {}
for key,users in group_dictionary.items():
for user in users:
user_groups[user]=[]
for key in user_groups.keys():
for ke,value in group_dictionary.items():
for i in value:
if(key==i):
user_groups[key].append(ke)
return(user_groups)
print(groups_per_user({"local": ["admin", "userA"],"public"["admin","userB"],"administrator": ["admin"] }))
def groups_per_user(group_dictionary):
user_groups = {}
# Go through group_dictionary
for key,value in group_dictionary.items():
# Now go through the users in the group
for letter in value:
if letter in user_groups:
user_groups[letter] += [key]
else :
user_groups[letter] = [key]
return(user_groups)
print(groups_per_user({"local": ["admin", "userA"],
"public": ["admin", "userB"],
"administrator": ["admin"] }))