从字典键(组合)生成一个字符串并根据值分配一个布尔值

Generate a string from a dictionary keys (combinations) and assing a boolean value based on values

我有一个类似字典的结构:

cont  = {

'perm': { 'r': False, 'rw': True}

'prig':  { 'sq': False, 'rot':False, 'rq':True}

'anon':  {'100': False, '500':True, '99':False; '400':False}

}

根据这个结构,我需要从键生成一个字符串,并根据值知道该字符串是 False 还是 True:

示例:

'perm', 'prig', 'anon' will become: 'r,sq,100' or 'rw,sq,100' or 'r,rq,100'.
  1. 我需要生成二级密钥的所有排列。
  2. 对于每个字符串,我需要使用 'AND' 关联一个布尔值是真还是假。 对于上面的例子:
False,False,False -> False;  True,False,False -> True,False,True, False -> False;

您可以使用 itertools.product:

import itertools
cont = {'perm': {'r': False, 'rw': True}, 'prig': {'sq': False, 'rot': False, 'rq': True}, 'anon': {'100': False, '500': True, '99': False, '400': False}}
keys = {a:list(b) for a, b in cont.items()}
p = list(itertools.product(*_keys.values()))
result = [[cont[[a for a, b in keys.items() if c in b][0]][c] for c in i] for i in p]
new_result = [any(i) for i in result]

输出:

#result:
[[False, False, False], [False, False, True], [False, False, False], [False, False, False], [False, False, False], [False, False, True], [False, False, False], [False, False, False], [False, True, False], [False, True, True], [False, True, False], [False, True, False], [True, False, False], [True, False, True], [True, False, False], [True, False, False], [True, False, False], [True, False, True], [True, False, False], [True, False, False], [True, True, False], [True, True, True], [True, True, False], [True, True, False]]
#new_result
[False, True, False, False, False, True, False, False, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True]
#list(map(','.join, prod))
['r,sq,100', 'r,sq,500', 'r,sq,99', 'r,sq,400', 'r,rot,100', 'r,rot,500', 'r,rot,99', 'r,rot,400', 'r,rq,100', 'r,rq,500', 'r,rq,99', 'r,rq,400', 'rw,sq,100', 'rw,sq,500', 'rw,sq,99', 'rw,sq,400', 'rw,rot,100', 'rw,rot,500', 'rw,rot,99', 'rw,rot,400', 'rw,rq,100', 'rw,rq,500', 'rw,rq,99', 'rw,rq,400']

这是我对作业的看法。它的代码比@Ajax1234 的答案多得多,但我希望对某些人来说 understand/read 更容易。

import itertools
data = {
    'test': {'a': False, 'b': True},
    'perm': {'r': False, 'rw': True},
    'prig': {'sq': False, 'rq': True},
    'anon': {'100': False, '500': True},
}

# define the order of the keys
key_order = ('perm', 'prig', 'anon', 'test')

# build a list of iterables for the 'product()' function
iterables = [
    sorted(data[k].keys())
    for k in key_order]
print(iterables)

print()
print('{:3s} {:20s} {:30s} {}'.format('i', 'elements', 'value of elements', 'AND'))

for i, elements in enumerate(itertools.product(*iterables)):
    # get the values for all the elements, in the correct order
    values = [
        data[k][sub_k]
        for k, sub_k in zip(key_order, elements)]

    print('{:3d} {:20s} {:30s} {}'.format(
        i+1,
        ','.join(elements),
        str(values),
        all(values)))

此代码给出以下输出:

[['r', 'rw'], ['rq', 'sq'], ['100', '500'], ['a', 'b']]

i   elements             value of elements              AND
  1 r,rq,100,a           [False, True, False, False]    False
  2 r,rq,100,b           [False, True, False, True]     False
  3 r,rq,500,a           [False, True, True, False]     False
  4 r,rq,500,b           [False, True, True, True]      False
  5 r,sq,100,a           [False, False, False, False]   False
  6 r,sq,100,b           [False, False, False, True]    False
  7 r,sq,500,a           [False, False, True, False]    False
  8 r,sq,500,b           [False, False, True, True]     False
  9 rw,rq,100,a          [True, True, False, False]     False
 10 rw,rq,100,b          [True, True, False, True]      False
 11 rw,rq,500,a          [True, True, True, False]      False
 12 rw,rq,500,b          [True, True, True, True]       True
 13 rw,sq,100,a          [True, False, False, False]    False
 14 rw,sq,100,b          [True, False, False, True]     False
 15 rw,sq,500,a          [True, False, True, False]     False
 16 rw,sq,500,b          [True, False, True, True]      False