创建具有列表理解能力的指定词典列表

时间:2019-04-21 15:45:43

标签: python list dictionary for-loop

我有词典列表:

test_users = [{'user': 'ADMIN0', 'privilege': 'ADMIN', 'auth_protocol': 'SHA_512'},
          {'user': 'ADMIN1', 'privilege': 'ADMIN', 'auth_protocol': 'SHA_256'}, \
          {'user': 'MONITOR12', 'privilege': 'MONITOR', 'auth_protocol': 'SHA_512'}, \
          {'user': 'MONITOR13', 'privilege': 'MONITOR', 'auth_protocol': 'SHA_256'}]

,并希望创建一个包含dict的新列表,这些字典包含“ privilege”和“ auth_protocol”的唯一值。例如:

selected_users= [{'user': 'ADMIN0', 'privilege': 'ADMIN', 'auth_protocol': 'SHA_512'},
          {'user': 'MONITOR13', 'privilege': 'MONITOR', 'auth_protocol': 'SHA_256'}, \

限制:

1。永远不能复制“特权”

2.(可选)'auth_protocol'仅在使用所有可能的情况下才可以重复(在这种情况下,它可以是'SHA_256'或'SHA_512'中具有'auth_protocol'的另一种特权)

我尝试解决的问题:

test_users_copy = test_users
selected_users = []
random_user = random.choice(test_users_copy)
# Add randomly chosen user to the list
selected_users.append(random_user)
test_users_copy.remove(random_user)

for test in selected_users:
    selected_users += [user for user in test_users_copy if (user.get('privilege') not in test.get('privilege')) and (user.get('auth_protocol') not in test.get('auth_protocol'))]

1 个答案:

答案 0 :(得分:0)

如果您只想选择具有不同的privilegeauth_protocol的用户:

>>> test_users = [{'user': 'ADMIN0', 'privilege': 'ADMIN', 'auth_protocol': 'SHA_512'},
... {'user': 'ADMIN1', 'privilege': 'ADMIN', 'auth_protocol': 'SHA_256'},
... {'user': 'MONITOR12', 'privilege': 'MONITOR', 'auth_protocol': 'SHA_512'},
... {'user': 'MONITOR13', 'privilege': 'MONITOR', 'auth_protocol': 'SHA_256'}]

>>> privileges, auth_protocols = set(), set()
>>> [tu for tu in test_users if not (tu["privilege"] in privileges or tu["auth_protocol"] in auth_protocols) and not privileges.add(tu["privilege"]) and not auth_protocols.add(tu["auth_protocol"])]
[{'user': 'ADMIN0', 'privilege': 'ADMIN', 'auth_protocol': 'SHA_512'}, {'user': 'MONITOR13', 'privilege': 'MONITOR', 'auth_protocol': 'SHA_256'}]

列表理解非常简单:如果看到tu["privilege"]tu["auth_protocol"],则跳过该命令;否则,将它们添加到可见的特权和auth_protocols中(not x.add(y)始终为True:这是在列表理解中可接受的极少数情况之一)。

但是,如果您没有其他协议,则不会按特权返回命令。更糟糕的是,选择总是一样。

要满足您的可选要求,您需要更复杂的东西。

让我们建立一个字典privilege -> auth_protocol -> users

>>> users_by_ap_by_privilege = {}
>>> for tu in test_users:
...     users_by_ap_by_privilege.setdefault(tu["privilege"], {}).setdefault(tu["auth_protocol"], []).append(tu)
>>> users_by_ap_by_privilege
{'ADMIN': {'SHA_512': [{'user': 'ADMIN0', 'privilege': 'ADMIN', 'auth_protocol': 'SHA_512'}], 'SHA_256': [{'user': 'ADMIN1', 'privilege': 'ADMIN', 'auth_protocol': 'SHA_256'}]}, 'MONITOR': {'SHA_512': [{'user': 'MONITOR12', 'privilege': 'MONITOR', 'auth_protocol': 'SHA_512'}], 'SHA_256': [{'user': 'MONITOR13', 'privilege': 'MONITOR', 'auth_protocol': 'SHA_256'}]}}

我们希望每个privilege(条件1)容纳一个用户,并且最大可能不同̀ {auth_protocol s(条件2)。

我们可以在auth_protocol之前提供privilege可用的:

>>> ap_by_privilege = {privilege: users_by_ap.keys() for privilege, users_by_ap in users_by_ap_by_privilege.items()}
>>> ap_by_privilege
{'ADMIN': dict_keys(['SHA_512', 'SHA_256']), 'MONITOR': dict_keys(['SHA_512', 'SHA_256'])}

对于Python <3.6,我们需要为privilege s的所有顺序设置一次(在Python> = 3.6中不会造成任何损害):

>>> privileges = list(ap_by_privilege)
>>> privileges
['ADMIN', 'MONITOR']

现在轻松构建auth_protocol的所有组合:

>>> import itertools
>>> prod=list(itertools.product(*[ap_by_privilege[p] for p in privileges]))
>>> prod
[('SHA_512', 'SHA_512'), ('SHA_512', 'SHA_256'), ('SHA_256', 'SHA_512'), ('SHA_256', 'SHA_256')]

(请注意,元组包含与列表auth_protocol中的每个privilege关联的privileges,并且顺序相同)现在我们可以影响每个组合的重复计数:< / p>

>>> import collections
>>> tuples_by_dup_count = {}
>>> for t in prod:
...     tuples_by_dup_count.setdefault(max(collections.Counter(t).values()), []).append(t)
...
>>> tuples_by_dup_count
{2: [('SHA_512', 'SHA_512'), ('SHA_256', 'SHA_256')], 1: [('SHA_512', 'SHA_256'), ('SHA_256', 'SHA_512')]}

现在,我们采用此dict的最小键,即具有最少重复项的元组(条件2):

>>> ts = tuples_by_dup_count[min(tuples_by_dup_count)]
>>> ts
[('SHA_512', 'SHA_256'), ('SHA_256', 'SHA_512')]

然后,我们选择auth_protocol的组合:

>>> import random
>>> t = random.choice(ts)
>>> t
('SHA_256', 'SHA_512')

并通过privilege / auth_protocol使用它来选择随机用户:

>>> [random.choice(users_by_ap_by_privilege[p][t[i]]) for i, p in enumerate(privileges)]
[{'user': 'ADMIN0', 'privilege': 'ADMIN', 'auth_protocol': 'SHA_512'}, {'user': 'MONITOR13', 'privilege': 'MONITOR', 'auth_protocol': 'SHA_256'}]