If you are using python 3.5 or greater you can merge your dictionaries using the following syntax:
appointment1 = { 'soccer' : {
'day' : 20,
'month' : 'april'
}
}
appointment2 = { 'soccer' : {
'day' : 20,
'month' : 'april'
},
'gym' : {
'day' : 5,
'month' : 'may'
}
}
appointment = {**appointment1,**appointment2}
Answer from mohamad khajezade on Stack OverflowIf you are using python 3.5 or greater you can merge your dictionaries using the following syntax:
appointment1 = { 'soccer' : {
'day' : 20,
'month' : 'april'
}
}
appointment2 = { 'soccer' : {
'day' : 20,
'month' : 'april'
},
'gym' : {
'day' : 5,
'month' : 'may'
}
}
appointment = {**appointment1,**appointment2}
If the dicts already exist you might look at dict.update().
Short answer is do this:
>>> appointment = { 'soccer' : { 'day': 20, 'month': 'april' } }
>>> appointment2 = { 'gym' : { 'day': 5, 'month': 'may' } }
>>> appointment.update(appointment2)
>>> appointment
{'gym': {'day': 5, 'month': 'may'}, 'soccer': {'day': 20, 'month': 'april'}}
But if you're making these dicts it makes more sense to just add new entries in the usual way (See: Add new keys to a dictionary?):
>>> appointment = { 'soccer' : { 'day': 20, 'month': 'april' } }
>>> appointment['gym'] = {'day': 5, 'month': 'may'}
>>> appointment
{'gym': {'day': 5, 'month': 'may'}, 'soccer': {'day': 20, 'month': 'april'}}
python - Append nested dictionaries - Stack Overflow
python - Append dictionary to dictionary as nested - Stack Overflow
Nested dictionary. Merging common keys and appending values to list. 0 value isn't appending. Code inside.
How do I append values to a nested dictionary in Python? - Stack Overflow
Videos
In python, append is only for lists, not dictionaries.
This should do what you want:
d['A']['b'] = 3
Explanation: When you write d['A'] you are getting another dictionary (the one whose key is A), and you can then use another set of brackets to add or access entries in the second dictionary.
You're looking for the update method:
d['A'].update({'b':3})
You told your code to assign newly created dict to key e[0]. It's always replaced blindly and it does not look at previously stored value.
Instead you need something like:
for e in keyList:
if e[0] not in nested_dict:
nested_dict[e[0]] = {}
nested_dict[e[0]].update({e[2] : e[3:]})
If conditional is required to handle 'first key' case. Alternatively defaultdict can be used.
from collections import defaultdict
nested_dict = defaultdict(dict)
for e in keyList:
nested_dict[e[0]].update({e[2] : e[3:]})
Great answer from @ŁukaszRogalski!
Just as an addition, without defaultdict, you can use setdefault:
keyList = [('A B 10'), ('A D 15')]
nested_dict = {}
for e in keyList:
rootkey = nested_dict.setdefault(e[0], {})
rootkey.update({e[2] : e[4:]})
print(nested_dict) # {'A': {'B': '10', 'D': '15'}}
.update is designed to be used with iterable feed. One of possible ways is providing dict, then value of keys in dict_things would be updated to corresponding values in provided dict. That is you might do:
dict_things = {}
for iteration, i_parameter in enumerate(list_of_things):
dict_things.update({iteration: Function_a(i_parameter, matrix)})
Above example is just for showing how dict.update can be used - this task might be done using dict-comprehension resulting in more readable code.
dict_things = {}
for iteration, i_parameter in enumerate(list_of_things):
dict_things[iteration] = Function_a(i_parameter, matrix)
or using a dict comprehension:
dict_things = { iteration: Function_a(i_parameter, matrix) for iteration, i_parameter in enumerate(list_of_things) }
Code: https://pastebin.com/GJn74YX5
I have a nested dictionary with the data structure:
foo[count] [string variable] = value
foo = { 0 : { "a": 2, "b": 5, "c": 6},
1 : { "a": 3, "b": 8, "d": 9},
2 : { "b": 5, "d": 9, "c": 3}}I want to take common values of these dicts and combine common values to the appropriate key variable. Perhaps doable in a new dict. If the common variable is not available for that specific count, it will add a 0. All values should be the same length in the new dict. To to look like this:
{ "a": [2, 3, 0], "b": [5, 8, 5], "c":[6, 0, 3], "d": [0,9,9] }My approach:
Create newdict a defaultdict(list) so i can check if key exists and append to the list like seen above. This will be the final dict I want
newdict = defaultdict(list)
Create the following for loop to append to the new dict:
for count in foo.keys(): for variable in foo[count].keys(): if variable in foo[count].keys(): newdict[variable].append(foo[count].get(variable)) elif variable not in foo[count].keys(): newdict[variable].append(0) else: newdict[variable] = foo[count].get(variable)
My problem:
Output:
{ "a": [2, 3], "b": [5, 8, 5], "c":[6, 3], "d": [9,9] }The newdict seem to merge all the values but it seems to always go toward the first if statement
The elif block is never reached -- 0 is never appended to the list
The else block is also never reached but it seems to be appending right so might not be a big deal(?)
I spent hours and cant seem to wrap my ahead why 0 isn't appending. Any help is appreciated. Thank you in advance!
You can use recursion with collections.defaultdict:
from collections import defaultdict
import re
def merge(*d):
v = defaultdict(list)
for i in d:
for a, b in i.items():
v[re.sub('^\s+', '', a)].append(b)
return {a:merge(*b) if all(isinstance(j, dict) for j in b)
else [i for j in b for i in (j if isinstance(j, list) else [j])]
for a, b in v.items()}
print(merge(grouped1, grouped2))
Output:
{'LabelStat': {'Carrier': ['1', '1', '2', '2'], 'FormID': ['0201', '0430', '10201', '10430']}, 'McAfee': {'DatDate': ['Not Available', 'Available'], 'DatVersion': ['Not Available', 'Available']}}
You can merge 2 dict with the update() method:
grouped1 = {'LabelStat': {'Carrier': ['1', '1'],
'FormID': ['0201', '0430']},
'McAfee': {'DatDate': 'Not Available',
' DatVersion': 'Not Available'}
}
grouped2 = {'LabelStat': {'Carrier': ['2', '2'],
'FormID': ['10201', '10430']},
'McAfee': {'DatDate': 'Available',
'DatVersion': 'Available', }
}
com_grouped = grouped1
com_grouped.update(grouped2)
Output:
{'LabelStat': {'Carrier': ['2', '2'], 'FormID': ['10201', '10430']}, 'McAfee': {'DatDate': 'Available', 'DatVersion': 'Available'}}
Expanding on idjaw's comment and keksnicoh's answer, I think you can make your life a little bit easier by using a defaultdict.
>>> from collections import defaultdict
>>> d = defaultdict(lambda: defaultdict(list))
>>> d['s1']['port1'].append([0, 0, 0])
>>> d['s1']['port1'].append([1, 1, 1])
>>> d['s1']['port2'].append([2, 2, 2])
>>> d
defaultdict(<function <lambda> at 0x7f5d217e2b90>, {'s1': defaultdict(<type 'list'>, {'port2': [[2, 2, 2]], 'port1': [[0, 0, 0], [1, 1, 1]]})})
You can use it just like a regular dictionary:
>>> d['s1']
defaultdict(<type 'list'>, {'port2': [[2, 2, 2]], 'port1': [[0, 0, 0], [1, 1, 1]]})
>>> d['s1']['port1']
[[0, 0, 0], [1, 1, 1]]
Your dict keys may be some kinda list, so you can append multiple values to it.
switches['s1'] = {}
switches['s1']['port1'] = list()
switches['s1']['port1'].append([0, 0, 0])
switches['s1']['port1'].append([1, 1, 1])
Also if you add single values you may also put them into a list so you can access the dict always by the same way:
switches['s1']['port2'] = list([2,2,2])
Getting the first port would be
print(switches['s1']['portN'][0]
Provided I've understood your requirements correctly:
In [25]: from collections import defaultdict
In [26]: d = defaultdict(lambda: defaultdict(list))
In [30]: for group, name, obj in [('g1','n1','o1'),('g1','n2','o2'),('g1','n1','o3'),('g2','n1','o4')]:
....: d[group][name].append(obj)
try something like this
dizGroup = {}
for obj in mc.ls(type='transform'):
if mc.objExists(obj + ('.matteGroup')):
matteGroup = mc.getAttr(obj + ('.matteGroup'))
matteName = mc.getAttr(obj + ('.matteName'))
if matteGroup not in dizGroup:
dizGroup[matteGroup] = {}
if matteName not in dizGroup[matteGroup]:
dizGroup[matteGroup][matteName] = []
dizGroup[matteGroup][matteName].append(obj)
here's solution without defaultdict:
d = {'Spring Savings 0413' : {1 : 3000, 2: 2000, 4:1000},
'Back to School 0812' : {1: 4000, 3:3000, 4:2000}}
r = {}
for s, l in d.items():
for i in range(1, 5):
if i not in r: r[i] = {}
r[i][s] = l.get(i, 0) + r.get(i - 1, {}).get(s, 0)
{1: {'Back to School 0812': 4000, 'Spring Savings 0413': 3000},
2: {'Back to School 0812': 4000, 'Spring Savings 0413': 5000},
3: {'Back to School 0812': 7000, 'Spring Savings 0413': 5000},
4: {'Back to School 0812': 9000, 'Spring Savings 0413': 6000}}
You are overwriting the entry for each key on every loop using by_day[i] = {}. You should rather check for existence of key, in which case, you should update the existing dict.
Or alternatively, use a collections.defaultdict:
>>> by_sale = {'Spring Savings 0413' : {1 : 3000, 2: 2000, 4:1000}, 'Back to School 0812' : {1: 4000, 3:3000, 4:2000}}
>>>
>>>
>>> from collections import defaultdict
>>>
>>> by_day = defaultdict(dict)
>>> for sale, sale_info in by_sale.iteritems():
... running_total = 0
... for i in range(1,5):
... daily_amount = sale_info.get(i,0)
... running_total += daily_amount
... by_day[i].update({sale:running_total})
...
>>>
>>> dict(by_day)
{1: {'Spring Savings 0413': 3000, 'Back to School 0812': 4000},
2: {'Spring Savings 0413': 5000, 'Back to School 0812': 4000},
3: {'Spring Savings 0413': 5000, 'Back to School 0812': 7000},
4: {'Spring Savings 0413': 6000, 'Back to School 0812': 9000}}
You should use range(1, 5) instead. range(1, 4) gives - [1, 2, 3].