I want to generate all config from a dict like this:
dict = {
"a": [1,2,3],
"b":{
"b1":[True, False],
"b2":[0],
}
}
List attributes are needed to be enumerated.
And output is like this:
config = [{
"a": 1,
"b":{
"b1":True,
"b2":0,
},
{
"a": 2,
"b":{
"b1":True,
"b2":0,
},
...
]
How can I reach this?
I think recursion is a good idea, but I don't know how to use it
There is no value in using recursion for this. A straightforward list comprehension will suffice:
_dict = {
"a": [1, 2, 3],
"b": {
"b1": [True, False],
"b2": [0]
}
}
config = [{'a': k, 'b': {'b1': True, 'b2': 0}} for k in _dict['a']]
print(config)
Output:
[{'a': 1, 'b': {'b1': True, 'b2': 0}}, {'a': 2, 'b': {'b1': True, 'b2': 0}}, {'a': 3, 'b': {'b1': True, 'b2': 0}}]
def merge_dicts(*dict_args):
result = {}
for dictionary in dict_args:
result.update(dictionary)
return result
def dict2combinations(d):
for k, v in d.items():
if isinstance(v, dict):
d[k] = dict2combinations(v)
values = [[{k: i} for i in v] for k, v in d.items()]
return [merge_dicts(*i) for i in product(*values)]
print(dict2combinations(config))
Output:
[{'a': 1, 'b': {'b1': True, 'b2': 0}}, {'a': 1, 'b': {'b1': False, 'b2': 0}}, {'a': 2, 'b': {'b1': True, 'b2': 0}}, {'a': 2, 'b': {'b1': False, 'b2': 0}}, {'a': 3, 'b': {'b1': True, 'b2': 0}}, {'a': 3, 'b': {'b1': False, 'b2': 0}}]
I'd like to sum up all values in multiple Python dictionaries with the same key and keep the other values as is if they have a different key.
The input dictionaries would look something like this:
dict1 = {'A':{'a':1, 'b':2, 'c':0}, 'B':{'a':3, 'b':0, 'c':0}}
dict2 = {'A':{'a':3, 'c':1, 'd':5}, 'B':{'a':2, 'b':0, 'c':1}, 'C':{'a':1, 'b':2, 'c':1}}
Output:
dict3 = {'A':{'a':4, 'b':2, 'c':1, 'd':5}, 'B':{'a':5, 'b':0, 'c':1}, 'C':{'a':1, 'b':2, 'c':1}}
Is there any way to achieve this result using only the standard library? This should also work with more than 2 dictionaries.
Very structure dependent, however something like ...
dicts = [
{'A':{'a':1, 'b':2, 'c':0}, 'B':{'a':3, 'b':0, 'c':0}},
{'A':{'a':3, 'c':1, 'd':5}, 'B':{'a':2, 'b':0, 'c':1}, 'C':{'a':1, 'b':2, 'c':1}}
]
odict = {}
for d in dicts:
for dk in d.keys():
if dk not in odict:
odict[dk] = {}
for inner_dk in d[dk].keys():
if inner_dk not in odict[dk]:
odict[dk][inner_dk] = d[dk][inner_dk]
else:
odict[dk][inner_dk] += d[dk][inner_dk]
print(odict)
A function that accepts multiple dicts:
dict1 = {"A": {"a": 1, "b": 2, "c": 0}, "B": {"a": 3, "b": 0, "c": 0}}
dict2 = {
"A": {"a": 3, "c": 1, "d": 5},
"B": {"a": 2, "b": 0, "c": 1},
"C": {"a": 1, "b": 2, "c": 1},
}
def merge_dicts(*dicts):
if not dicts:
return
out = {}
all_keys = set(dicts[0]).union(k for d in dicts[1:] for k in d)
for k in all_keys:
out[k] = {}
for kk in set(dicts[0].get(k, {})).union(
k_ for d in dicts[1:] for k_ in d.get(k, {})
):
out[k][kk] = sum(d.get(k, {}).get(kk, 0) for d in dicts)
return out
print(merge_dicts(dict1, dict2))
Prints:
{'C': {'c': 1, 'a': 1, 'b': 2}, 'A': {'c': 1, 'd': 5, 'a': 4, 'b': 2}, 'B': {'c': 1, 'a': 5, 'b': 0}}
For input:
dict1 = {"A": {"a": 1, "b": 2, "c": 0}, "B": {"a": 3, "b": 0, "c": 0}}
dict2 = {
"A": {"a": 3, "c": 1, "d": 5},
"B": {"a": 2, "b": 0, "c": 1},
"C": {"a": 1, "b": 2, "c": 1},
}
dict3 = {
"A": {"a": 100},
}
print(merge_dicts(dict1, dict2, dict3))
Prints:
{'A': {'a': 104, 'b': 2, 'c': 1, 'd': 5}, 'B': {'a': 5, 'b': 0, 'c': 1}, 'C': {'a': 1, 'b': 2, 'c': 1}}
If I have a dictionary containing lists in one or more of its values:
data = {
'a':0,
'b':1,
'c':[0, 1, 2],
'pair':['one','two']
}
How can I get a list of dict tuples paired by pair and iterating over c, with all else remaining constant? E.g.
output = [
({
'a':0,
'b':1,
'c':0,
'pair':'one'
},
{
'a':0,
'b':1,
'c':0,
'pair':'two'
}),
({
'a':0,
'b':1,
'c':1,
'pair':'one'
},
...
]
Well, this doesn't feel especially elegant, but you might use a nested for loop or list comprehension:
output = []
for i in data['c']:
output.append(tuple({'a': 0, 'b': 1, 'c': i, 'pair': p} for p in data))
or
output = [tuple({'a': 0, 'b': 1, 'c': i, 'pair': p} for p in data['pair']) for i in data['c']]
A cleaner solution might separate out the generation of the component dict into a function, like this:
def gen_output_dict(c, pair):
return {'a': 0, 'b': 1, 'c': c, 'pair': pair}
output = []
for i in data['c']:
output.append(tuple(gen_output_dict(i, p) for p in data['pair']))
You can use itertools.product on list values and keep track of the key from which each element originated. Since the key 'pair' has a special meaning, you should treat it separately.
Code
from itertools import product
def unzip_dict(d):
keys = [k for k, v in d.items() if isinstance(v, list) and k != 'pair']
values = [d[k] for k in keys]
for values in product(*values):
yield tuple({**d, **dict(zip(keys, values)), 'pair': pair} for pair in d['pair'])
Example
data = {
'a': 0,
'c': [1, 2],
'pair': ['one', 'two']
}
print(*unzip_dict(data))
Output
({'a': 0, 'c': 1, 'pair': 'one'}, {'a': 0, 'c': 1, 'pair': 'two'})
({'a': 0, 'c': 2, 'pair': 'one'}, {'a': 0, 'c': 2, 'pair': 'two'})
The following is quite an extended solution:
data = {
'a':0,
'b':1,
'c':[0, 1, 2],
'pair':['one','two']
}
# Get the length of the longest sequence
length = max(map(lambda x: len(x) if isinstance(x, list) else 1, data.values()))
# Loop through the data and change scalars to sequences
# while also making sure that smaller sequences are stretched to match
# or exceed the length of the longest sequence
for k, v in data.items():
if isinstance(v, list):
data[k] = v * int(round(length/len(v), 0))
else:
data[k] = [v] * length
# Create a dictionary to keep track of which outputs
# need to end up in which tuple
seen = dict.fromkeys(data.get('pair'), 0)
output = [tuple()] * len(seen)
# Loop through the data and place dictionaries in their
# corresponding tuples.
for v in zip(*data.values()):
d = dict(zip(data, v))
output[seen[d.get('pair')]] += (d,)
seen[d.get('pair')] += 1
print(output)
The idea is to convert the scalars in your data to sequences whose lengths match that of the longest sequence in the original data. Therefore, the first thing I did was assign to the variable length the size of the longest sequence. Armed with that knowledge, we loop through the original data and extend the already existing sequences to match the size of the longest sequence while converting scalars to sequences.
Once that's done, we move to generating the output variable. But first, we create a dictionary called seen to help us both create a list of tuples and keep track of which group of dictionaries ends up in which tuple.
This, then, allows us to run one final loop to place the groups of dictionaries to their corresponding tuples.
The current output looks like the following:
[({'a': 0, 'b': 1, 'c': 0, 'pair': 'one'},
{'a': 0, 'b': 1, 'c': 1, 'pair': 'two'}),
({'a': 0, 'b': 1, 'c': 2, 'pair': 'one'},)]
Please let me know if you need any more clarifying details. Otherwise, I do hope this serves some purpose.
#r3robertson, You can also try the below code. The code is based on the concept of list comprehension, & deepcopy() operation in Python.
Check Shallow copy vs deepcopy in Python.
import pprint;
import copy;
data = {
'a': 0,
'b': 1,
'c': [0, 1, 2],
'pair': ['one','two'],
};
def get_updated_dict(data, index, pair_name):
d = copy.deepcopy(data);
d.update({'c': index, 'pair': pair_name});
return d;
output = [tuple(get_updated_dict(data, index, pair_name) for pair_name in data['pair']) for index in data['c']];
# Pretty printing the output list.
pprint.pprint(output, indent=4);
Output »
[ ( { 'a': 0, 'b': 1, 'c': 0, 'pair': 'one'},
{ 'a': 0, 'b': 1, 'c': 0, 'pair': 'two'}),
( { 'a': 0, 'b': 1, 'c': 1, 'pair': 'one'},
{ 'a': 0, 'b': 1, 'c': 1, 'pair': 'two'}),
( { 'a': 0, 'b': 1, 'c': 2, 'pair': 'one'},
{ 'a': 0, 'b': 1, 'c': 2, 'pair': 'two'})]
Pretty printing using json module »
Note: Tuple will convert into list here as tuples are not supported inside JSON.
import json;
print(json.dumps(output, indent=4));
Output »
[
[
{
"a": 0,
"c": 0,
"b": 1,
"pair": "one"
},
{
"a": 0,
"c": 0,
"b": 1,
"pair": "two"
}
],
[
{
"a": 0,
"c": 1,
"b": 1,
"pair": "one"
},
{
"a": 0,
"c": 1,
"b": 1,
"pair": "two"
}
],
[
{
"a": 0,
"c": 2,
"b": 1,
"pair": "one"
},
{
"a": 0,
"c": 2,
"b": 1,
"pair": "two"
}
]
]
Not too perfect but here's my solution.
data = { 'a':0, 'b':1, 'c':[0, 1, 2], 'pair':['one','two'] }
a,b = data['pair'], data['c']
for t in range(0, len(b)):
for u in range(0, len(a)):
for h in a:
data['c']=b[t]
data['pair']=a[u]
print(tuple([data]))
If you are sure about defined keys then you can use like below
record_dict = {'id':'0123abc', 'roles': ['abc', 'cda', 'xyz']}
output = []
for index, key in enumerate(record_dict['roles']):
output.append({'id': record_dict.get('id'), 'roles': key})
print(output)
You can use itertools:
import itertools
data = {
'a':0,
'b':1,
'c':[0, 1, 2],
'pair':['one','two']
}
def expand_dict(data):
grouped = [a for a, b in data.items() if isinstance(b, list)]
p = [[a, list(b)] for a, b in itertools.groupby(itertools.product(*[data[i] for i in grouped]), key=lambda x:x[0])]
return [tuple({**data, **dict(zip(grouped, i))} for i in c) for _, c in p]
print(expand_dict(data))
Output:
[({'a': 0, 'b': 1, 'c': 0, 'pair': 'one'}, {'a': 0, 'b': 1, 'c': 0, 'pair': 'two'}),
({'a': 0, 'b': 1, 'c': 1, 'pair': 'one'}, {'a': 0, 'b': 1, 'c': 1, 'pair': 'two'}),
({'a': 0, 'b': 1, 'c': 2, 'pair': 'one'}, {'a': 0, 'b': 1, 'c': 2, 'pair': 'two'})]
This solution will also work on input with many possible lists of values:
data = {'a':[5, 6, 1, 3], 'b':1, 'c':[0, 1, 2], 'pair':['one', 'two']}
print(expand_dict(data))
Output:
[({'a': 5, 'b': 1, 'c': 0, 'pair': 'one'}, {'a': 5, 'b': 1, 'c': 0, 'pair': 'two'}, {'a': 5, 'b': 1, 'c': 1, 'pair': 'one'}, {'a': 5, 'b': 1, 'c': 1, 'pair': 'two'}, {'a': 5, 'b': 1, 'c': 2, 'pair': 'one'}, {'a': 5, 'b': 1, 'c': 2, 'pair': 'two'}), ({'a': 6, 'b': 1, 'c': 0, 'pair': 'one'}, {'a': 6, 'b': 1, 'c': 0, 'pair': 'two'}, {'a': 6, 'b': 1, 'c': 1, 'pair': 'one'}, {'a': 6, 'b': 1, 'c': 1, 'pair': 'two'}, {'a': 6, 'b': 1, 'c': 2, 'pair': 'one'}, {'a': 6, 'b': 1, 'c': 2, 'pair': 'two'}), ({'a': 1, 'b': 1, 'c': 0, 'pair': 'one'}, {'a': 1, 'b': 1, 'c': 0, 'pair': 'two'}, {'a': 1, 'b': 1, 'c': 1, 'pair': 'one'}, {'a': 1, 'b': 1, 'c': 1, 'pair': 'two'}, {'a': 1, 'b': 1, 'c': 2, 'pair': 'one'}, {'a': 1, 'b': 1, 'c': 2, 'pair': 'two'}), ({'a': 3, 'b': 1, 'c': 0, 'pair': 'one'}, {'a': 3, 'b': 1, 'c': 0, 'pair': 'two'}, {'a': 3, 'b': 1, 'c': 1, 'pair': 'one'}, {'a': 3, 'b': 1, 'c': 1, 'pair': 'two'}, {'a': 3, 'b': 1, 'c': 2, 'pair': 'one'}, {'a': 3, 'b': 1, 'c': 2, 'pair': 'two'})]
I have the below json, I want to add one more property:
[ {
"A": 1,
"B": "str"
},
{
"A": 2,
"B": "str2"
},
{
"A": 3,
"B": "str3"
}
]
So I want something like this:
[ {
"A": 1,
"B": "str",
"C": "X"
},
{
"A": 2,
"B": "str2",
"C": "X"
},
{
"A": 3,
"B": "str3",
"C": "X"
}
]
What is the best way to do this?
Loop through each dict obj in the list and add required key value pair that you want:
List Before
list1 = [
{
"A": 1,
"B": "str"
},
{
"A": 2,
"B": "str2"
},
{
"A": 3,
"B": "str3"
}
]
The code
for l in list1:
l['C'] = 'X'
print(list1)
List After i.e Output
[{'A': 1, 'B': 'str', 'C': 'X'}, {'A': 2, 'B': 'str2', 'C': 'X'}, {'A': 3, 'B': 'str3', 'C': 'X'}]
>>> j = [ { "A": 1, "B": "str" }, { "A": 2, "B": "str2" }, { "A": 3, "B": "str3" } ]
>>> [i.update({'C': 'X'}) for i in j]
>>> j
[{'A': 1, 'B': 'str', 'C': 'X'}, {'A': 2, 'B': 'str2', 'C': 'X'}, {'A': 3, 'B': 'str3', 'C': 'X'}]
Or, as per coldspeed's comment:
>>> for item in j:
... item['C'] = 'X'
...
>>> j
[{'A': 1, 'B': 'str', 'C': 'X'}, {'A': 2, 'B': 'str2', 'C': 'X'}, {'A': 3, 'B': 'str3', 'C': 'X'}]