3

I want a result that would make the print statement print;

{"admin" : ["local", "public", "administrator"], 
"user A" : ["local"], 
"user B" : ["public"]}

This is what I have tried:

def groups_per_user(groups_dictionary):
    user_groups = {}
    for groups, users in groups_dictionary.items():
            for user in users:
                user_groups[user] = groups
                
    return(user_groups)
    
print(groups_per_user({"local":["admin", "user A"], "public":["admin", "user B"], "administrator": ["admin"]}))

output

{'admin': 'administrator', 'user A': 'local', 'user B': 'public'}
Umutambyi Gad
  • 4,082
  • 3
  • 18
  • 39
Bryan Somto
  • 101
  • 6

2 Answers2

2

Just modify your for loop like the following:

for user in users:
    user_groups.setdefault(user, [])
    user_groups[user].append(groups)
Selcuk
  • 57,004
  • 12
  • 102
  • 110
1

Here is solution without any help of any module but not simple but flexible you can use it on whatever no matter how your data is larger the code will still work and give you the desired output

Solution

data = {"local":["admin", "user A"],
        "public":["admin", "user B"],
        "administrator": ["admin"]}

def groups_per_user(data):
    data_keys = [key for key in data.keys()]
    data_values = [value for value in data.values()]

    for inter in range(len(data)):
        intersect = set(data_values[inter]).intersection(set(data_values[inter]))

    indexes = [i.index(''.join(i for i in intersect)) for i in data_values]
    for i, v in enumerate(data_values):
        data_values[i].remove(v[indexes[i]])

    non_empty_list = [e for i in data_values if len(i) != 0 for e in i]
    for_admin = {''.join(i for i in intersect): data_keys}

    for_users = {}
    for users_keys, users in data.items():
        for i in range(len(non_empty_list)):
            if non_empty_list[i] in users:
                for_users[''.join(user for user in users)] =  users_keys

    res = {}
    for a, b in for_users.items():
        for c, d in for_admin.items():
            res[c] = d
            res[a] = [b]

    return res

print(groups_per_user(data))

output

{'admin': ['local', 'public', 'administrator'], 
'user A': ['local'],
'user B': ['public']}

So let me give short hand about my code first of all you determine the keys(data_keys) and values(data_keys) in the data thereafter find the intersection(intersect) to all values in order to get admin dynamically then search for the indexes(indexes) of the admin in every single value in order to get it removed in the keys so when you done to remove them there will be an empty key list and remove it as well then make the dictionary for the admin(for_admin) and make another dictionary for users(for_users) after of all concatenate them to get the final solution.

and also if you got the following crazy input you will get desired output.

input

data = {"local":["admin", "user A"],
        "current":["admin", "user C"],
        "public":["admin", "user B"],
        "private":["admin", "user B"],
        "administrator": ["admin"]}

output

{'admin': ['local', 'current', 'public', 'private', 'administrator'],
'user A': ['local'],
'user C': ['public'],
'user B': ['private']}
Umutambyi Gad
  • 4,082
  • 3
  • 18
  • 39