user3541631
user3541631

Reputation: 4008

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

I have a structure similar to dictionaries:

c  = {

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

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

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

}

I need to combine all the values, create an array of new dictionary and associate a dictionary with a Bollean values based on keys. Can be use something else than dictionaries.

Example:

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

Upvotes: 0

Views: 65

Answers (2)

Paritosh Singh
Paritosh Singh

Reputation: 6246

You can do this using itertools product and some manipulations of the output to get the desired dicts

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)

Output:

[{'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}]

Upvotes: 1

Devesh Kumar Singh
Devesh Kumar Singh

Reputation: 20500

One suggestion I might have is to make a combined dictionary where you chain nested keys together with a delimiter, (I chose : any other delimiter can be used as well)

So the original dictionary changes to

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

via the code

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

We can then easily use the res dictionary to compute the boolean values for c1,c2... after iterating over with key,value pairs of c1,c2.. etc

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)

And the output will be

False
True
False

Upvotes: 0

Related Questions