小编Abd*_*aie的帖子

如何反转字典中键的值分配?解决算法

groups_per_user 函数接收一个字典,其中包含带有用户列表的组名。就像 Linux 系统中的组。用户可以属于多个组。填空以返回一个以用户为键、以用户组列表为值的字典。

基本上我试图将组分配给用户而不是用户分配给组

这就是我迄今为止尝试过的:

def groups_per_user(group_dictionary):
    user_groups = {}
    groups = []
    # Go through group_dictionary
    for group,users in group_dictionary.items():
        # Now go through the users in the group
        for user in users:
        # Now add the group to the list of
          # groups for this user, creating the entry
          # in the dictionary if necessary
          groups.append(group)
          user_groups[user] = group

    return(user_groups)

print(groups_per_user({"local": ["admin", "userA"],
        "public":  ["admin", "userB"],
        "administrator": ["admin"] }))
Run Code Online (Sandbox Code Playgroud)

如何遍历抛出列表,同时尽可能高效地将用户添加到组名中?

请原谅我的语法,这是我的第一个问题。谢谢

python algorithm

1
推荐指数
1
解决办法
2万
查看次数

标签 统计

algorithm ×1

python ×1