I have 2 dictionaries with identical keys.
d1 = {'Dog':[7,2],'Cat':[5,2]}
d2 = {'Dog':1,'Cat':4}
Is there a good way of combining them so that I can have one dictionary that looks like this?
d = {'Dog':[7,2,1],'Cat':[5,2,4]}
for key, value in d2.iteritems():
if key in d1:
d1[key].append(value)
If one contains lists and the other contains ints, you can do:
d = {key:[d2[key]] + d1[key] for key in d1}
Related
dict1 = {'VE-VOLDA-385': 'L_741405088655871',
'VE-EIRA-313': 'L_741405088655872',
'VE-SUNNFJORD-077': 'L_741405088655873',
'PIER-BUAVAG-117': 'L_74140508865602'}
dict2 = {'EIRA': '9261621',
'VOLDA': '9254898',
'SUNNFJORD': '7710501'}
I have two dictionaries I have got from two APIs. The second dictionary value contains the data I would like to match to the first dictionary. How can I match these two on the key?
Also as there is some items in dict1 that should not be matched to dict2 I can't just sort them alphabetically.
If the keys have the form given in your question, it is easy enough to extract the part of the key that you are interested in:
dict1 = {'VE-VOLDA-385': 'L_741405088655871',
'VE-EIRA-313': 'L_741405088655872',
'VE-SUNNFJORD-077': 'L_741405088655873',
'PIER-BUAVAG-117': 'L_74140508865602'}
dict2 = {'EIRA': '9261621',
'VOLDA': '9254898',
'SUNNFJORD': '7710501'}
keys1 = {k.split('-')[1] for k in dict1}
keys2 = set(dict2)
common_keys = keys1 & keys2
print(common_keys) #{'EIRA', 'SUNNFJORD', 'VOLDA'}
Suppose I have a dictionary:
d = {'a_c':1,'b_c':2,'a_d':3,'b_d':4}
how do I split into two based on the last word/letter of the key ('c','d') like this?
d1 = {'a_c':1,'b_c':2}
d2 = {'a_d':3,'b_d':4}
This is one way:
from collections import defaultdict
d = {'a_c':1,'b_c':2,'a_d':3,'b_d':4}
key = lambda s: s.split('_')[1]
res = defaultdict(dict)
for k, v in d.items():
res[key(k)][k] = v
print(list(res.values()))
Output:
[{'a_c': 1, 'b_c': 2}, {'a_d': 3, 'b_d': 4}]
The result is a list of dictionaries divided on the last letter of the key.
You could try something like this:
func = lambda ending_str: {x: d[x] for x in d.keys() if x.endswith(ending_str)}
d1 = func('_c')
d2 = func('_d')
Also, like Marc mentioned in the comments, you shouldn't have two same name keys in the dictionary. It will only keep the last key/value pair in that case.
I want to create a nested dictionary out of a list that should be the keys and dictionaries that are the values of that nested dict.
Input:
l = [key1, key2, key3]
d1 = {string11:value11, string12:value12, string13:value13}
d2 = {string21:value21, string22:value22, string23:value23}
d3 = {string31:value31, string32:value32, string33:value33}
Output:
{key1 : {string11:value11, string12:value12, string13:value13}, key2 : {string21:value21, string22:value22, string23:value23}, key3 : {string31:value31, string32:value32, string33:value33}}
So far, I tried to zip the list and a dict but this creates another dict, not a nested dict.
ld = dict(zip(l,d))
Do I need to create a list of d1,d2,d3 first? How can I combine l and the list of dicts then?
Yes you'll need to create the list of d first
dict(zip(l, [d1, d2, d3]))
When you pass a dictionary in as an iterator, its values will be iterated over (The keys won't be included). Hence why dict(zip(l, d1)) will produce a single dictionary rather than a nested one.
There are many ways. You could for example use dict comprehension:
ld = {k: d for k, d in zip(l, [d1, d2, d3])}
I'd like two join two dictionaries based on the value of d1 and a substring of the key of d2. The resulting dictionary has the key of d1 with the corresponding value of d2.
d1 = {'web02': '23', 'web01': '50'}
d2 = {'server/dc-50': 's01.local', 'server/dc-23': 's02.local'}
Would result in = {web01:s01.local, web02:s02.local}
I guess this is what you need :
result = {k1:v2 for k1,v1 in d1.items() for k2,v2 in d2.items() if v1 in k2}
Output :
{'web02': 's02.local', 'web01': 's01.local'}
This is done without a nested loop by getting the value using string formatting:
data = {k: d2['server/dc-' + v] for k, v in d1.items()}
Prints:
{'web02': 's02.local', 'web01': 's01.local'}
dict:
d1 = {'b,a':12,'b,c,a':13}
Code:
x = collections.OrderedDict(sorted(d1.items()))
print(x)
Not getting the expected output.
Expected Output:
d1 = {'a,b': 12, 'a,b,c':13}
It looks like you don't actually want to sort the keys, you want to re-arrange the non-comma substrings of your keys such that these substrings are ordered.
>>> d1 = {'b,a':12,'b,c,a':13}
>>> {','.join(sorted(key.split(','))):val for key, val in d1.items()}
{'a,b': 12, 'a,b,c': 13}
d1.items(): returns a list of (key, value) tuples
sorted(d1.items()): simply sorts the above list
If you want to sort the items in your keys, then you need to run sort on your keys.