Question

In: Computer Science

The groups_per_user function receives a dictionary, which contains group names with the list of users.

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 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"] }))

Solutions

Expert Solution

Code:

Output:

Raw Code:

def groups_per_user(group_dictionary):
user_groups={}
for key in group_dictionary.keys(): #loop through the keys using keys function
for val in group_dictionary[key]: #loop through the list of values associated with the key in dictionary
if val in user_groups.keys(): #if it is already present
user_groups[val].append(key) #appending the key as value to user_groups
else: #if it is not present
user_groups[val]=[key] #adding val as key to user_groups and key of group_dictionary as value list
return(user_groups)
print(groups_per_user({"local": ["admin", "userA"],"public": ["admin", "userB"],"administrator": ["admin"] }))


Related Solutions

Write a function that accepts a dictionary and produces a sorted list of tuples The dictionary...
Write a function that accepts a dictionary and produces a sorted list of tuples The dictionary looks like this: {‘US’: [{'Chicago, IL': ('2/1/2020 19:43', 2, 0, 0)}, {'San Benito, CA': ('2/3/2020 3:53', 2, 0, 0)}, {'Santa Clara, CA': ('2/3/2020 0:43', 2, 0, 0)}, {'Boston, MA': ('2/1/2020 19:43', 1, 0, 0)}, {'Los Angeles, CA': ('2/1/2020 19:53', 1, 0, 0)}, {'Orange, CA': ('2/1/2020 19:53', 1, 0, 0)}, {'Seattle, WA': ('2/1/2020 19:43', 1, 0, 0)}, {'Tempe, AZ': ('2/1/2020 19:43', 1, 0, 0)}], 'Australia'...
Write a function which receives a list and returns a number. In the list, all numbers...
Write a function which receives a list and returns a number. In the list, all numbers have been repeated twice except one number that is repeated once. The function should return the number that is repeated once and return it.write a python program for this question. use main function.
# List at least three functions in the linked list toolbox, including function names and their...
# List at least three functions in the linked list toolbox, including function names and their functionality. # Assuming we have already implemented all functions in the linked list toolbox and we have a pointer head_ptr pointing to the front of a linked list. Write a few lines to insert the number 42 and make it the second item in the list. Note: if the list was originally empty, then it should be the first in the list.
Which ones in the following list are properties of a normal density function ? Group of...
Which ones in the following list are properties of a normal density function ? Group of answer choices
(5)When you pass an array to a function, the function receives Group of answer choices a...
(5)When you pass an array to a function, the function receives Group of answer choices a copy of the array a reference to the array the data type of the array and the number of elements it contains the data type of the array and a pointer to its first element (6)Which of the following statements is not true about a built-in array? Group of answer choices Elements that aren’t initialized will be assigned default values. Its elements must have...
Write code to read a list of song names and durations from input. Input first receives...
Write code to read a list of song names and durations from input. Input first receives a song name, then the duration of that song. Input example: Time 424 Money 383 quit. #include <iostream> #include <string> #include <vector> using namespace std; class Song { public: void SetNameAndDuration(string songName, int songDuration) { name = songName; duration = songDuration; } void PrintSong() const { cout << name << " - " << duration << endl; } string GetName() const { return name;...
*Python* 1.1) Create an empty dictionary called 'addresses'. The dictionary you just created will map names...
*Python* 1.1) Create an empty dictionary called 'addresses'. The dictionary you just created will map names to addresses. A person's name (stored as a string) will be the KEY and that person's address (stored as a string) will be the VALUE. 1.2) Insert into the dictionary 'addresses' the names and addresses of two (possibly imaginary) friends of yours. 1.3) Create a second empty dictionary called 'ages'. The dictionary you just created will map names to ages. A person's name (stored...
Using LIST and FUNCTION Write a program in Python that asks for the names of three...
Using LIST and FUNCTION Write a program in Python that asks for the names of three runners and the time it took each of them to finish a race. The program should display who came in first, second, and third place.
Create a web page using PHP and HTML that contains a list of movie names available...
Create a web page using PHP and HTML that contains a list of movie names available for rent (at least 10 movies), so that each time you visit the home page, you see a different order of movies in the list. The movie names are required. 2) The rental price for each movie ranges between $1.99 to $7.99. • Create an associative array with a code for the movie as the key (e.g., movie1, movie2, etc.) and the associated rental...
You are given a list of all employees. You group the names by department (Logistics, Sales,...
You are given a list of all employees. You group the names by department (Logistics, Sales, IT, Human Resource). Suppose you select all employees in Sales. What type of sampling method did you use to select the employees? Explain your reasoning.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT