从基础字典开始按permutations/combinations生成字典,并根据键值将结果与True/False相关联

Generate dictionaries by permutations/combinations starting from a base dictionary, and associate the result with True/False base on key values

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

c  = {

'usize': { '500': False, '100': True}

'isize':  { '200': False, '800':False, '900':True}

'path':  {'/tmp': False, '/var':True, '/tp12':False;}

}

我需要组合所有值,创建一个新字典数组,并根据键将字典与 Bollean 值相关联。可以使用字典以外的东西。

示例:

c1 = {

'usize': 500,
'isize': 200,
'path': '/tmp' }

Associated value, False and False and False = False

c2 = {

'usize': 100,
'isize': 900,
'path': '/var' }

Associated value, True  and True and True = True

c2 = {

'usize': 500,
'isize': 900,
'path': '/var' }

Associated value, False  and True and True = False

我可能有一个建议是制作一个组合字典,您可以在其中将嵌套键与分隔符链接在一起,(我选择 : 也可以使用任何其他分隔符)

于是原词典改为

{'usize:500': False, 'usize:100': True, 
'isize:200': False, 'isize:800': False, 'isize:900': True, 
'path:/tmp': False, 'path:/var': True, 'path:/tp12': False}

通过代码

res = {'{}:{}'.format(key1,key2):value for key1,value1 in c.items() for key2, value in value1.items() }
print(res)

然后我们可以很容易地使用 res 字典来计算 c1,c2... 的布尔值,在用 c1,c2..

的键值对迭代之后
c1_val = all(res['{}:{}'.format(k,v)] for k,v in c1.items())
print(c1_val)

c2_val = all(res['{}:{}'.format(k,v)] for k,v in c2.items())
print(c2_val)

c3_val = all(res['{}:{}'.format(k,v)] for k,v in c3.items())
print(c3_val)

输出将是

False
True
False

您可以使用 itertools product 和对输出进行一些操作来获得所需的字典来做到这一点

from itertools import product

c  = {

'usize': { '500': False, '100': True},

'isize':  { '200': False, '800':False, '900':True},

'path':  {'/tmp': False, '/var':True, '/tp12':False}

}

result = []
for group in product(*c.values()): #this makes all product groups of values such as (500, 200, '/var') etc.
    temp = dict(zip(c.keys(), group)) #bring back the keys for every group
    #create a new 'value' key with the boolean result
    temp['value'] = all(c[k][v] for k, v in temp.items()) #change to .iteritems() for python 2
    result.append(temp)
print(result)

输出:

[{'usize': '500', 'isize': '200', 'path': '/tmp', 'value': False},
 {'usize': '500', 'isize': '200', 'path': '/var', 'value': False},
 {'usize': '500', 'isize': '200', 'path': '/tp12', 'value': False},
 {'usize': '500', 'isize': '800', 'path': '/tmp', 'value': False},
 {'usize': '500', 'isize': '800', 'path': '/var', 'value': False},
 {'usize': '500', 'isize': '800', 'path': '/tp12', 'value': False},
 {'usize': '500', 'isize': '900', 'path': '/tmp', 'value': False},
 {'usize': '500', 'isize': '900', 'path': '/var', 'value': False},
 {'usize': '500', 'isize': '900', 'path': '/tp12', 'value': False},
 {'usize': '100', 'isize': '200', 'path': '/tmp', 'value': False},
 {'usize': '100', 'isize': '200', 'path': '/var', 'value': False},
 {'usize': '100', 'isize': '200', 'path': '/tp12', 'value': False},
 {'usize': '100', 'isize': '800', 'path': '/tmp', 'value': False},
 {'usize': '100', 'isize': '800', 'path': '/var', 'value': False},
 {'usize': '100', 'isize': '800', 'path': '/tp12', 'value': False},
 {'usize': '100', 'isize': '900', 'path': '/tmp', 'value': False},
 {'usize': '100', 'isize': '900', 'path': '/var', 'value': True},
 {'usize': '100', 'isize': '900', 'path': '/tp12', 'value': False}]