I am struggling to merge values from a second dictionary to the initial one.
The initial dictionary is:
dictionary1 = {u'user1': [[u'N/A', u'N/A', u'N/A', u'N/A', u'N/A', u'N/A', u'N/A']],
u'user2': [[504, u'user2 Windows', u'date', u'date', u'20', u'', u'1']],
u'user3': [[511, u'user3 phone', u'date1', u'date2', u'name', u'1', u'E'],
[125, u'user3 phone',u'date1', u'date2', u'name', u'', u'8'],
[854, u'user3 phone', u'date1', u'date2', u'name', u'1', u'3'],
[890, u'user3 Windows', u'date1', u'date2', u'name', u'', u'4'],
[506, u'user3 Windows', u'date1', u'date2', u'name', u'', u'7'],
[454, u'user3 Windows', u'date1', u'date2', u'name', u'', u'E']]}
dictionary2 = {u'user1': [[u'N/A', u'N/A']],
u'user2': [[504, u'to_add']],
u'user3': [[890, u'to_add'],
[506, u'to_add'],
[454, u'to_add']]}
Matching values: 504 for user2, and 890, 506 and 454 for user 3. I would need the output to be:
dictionary1 = {u'user1': [[u'N/A', u'N/A', u'N/A', u'N/A', u'N/A', u'N/A', u'N/A', u'N/A']],
u'user2': [[504, u'user2 Windows', u'date', u'date', u'20', u'', u'1', u'to_add']],
u'user3': [[511, u'user3 phone', u'date1', u'date2', u'name', u'1', u'E'],
[125, u'user3 phone',u'date1', u'date2', u'name', u'', u'8'],
[854, u'user3 phone', u'date1', u'date2', u'name', u'1', u'3'],
[890, u'user3 Windows', u'date1', u'date2', u'name', u'', u'4', u'to_add'],
[506, u'user3 Windows', u'date1', u'date2', u'name', u'', u'7', u'to_add'],
[454, u'user3 Windows', u'date1', u'date2', u'name', u'', u'E', u'to_add']]}
I have tried multiple ways to check and match, but I am at a lost. Here is where I am stuck:
inter1 = []
for user1 in dictionary2:
for details1 in dictionary2[user1]:
inter1.append(details1[0])
inter1.append(details1[1])
print inter1
a = [[details1[1] for details1 in dictionary2[user1]] for user1 in dictionary2]
inter2 = {}
for user2 in dictionary1:
for details2 in dictionary1[user2]:
if details2[0] in inter1:
print user2, details2[2], details2[3], details2[4], details2[5], details2[6]
inter2[user2] = [details2[2], details2[3], details2[4], details2[5], details2[6], inter1]
but this way just adds the entire list to the end of each list...:(
CodePudding user response:
you can iterate over a dictionary by using the items()
method:
for k, v in dictionary2.items():
print(k, v)
>>>user1 [['N/A', 'N/A']]
>>>user2 [[504, 'to_add']]
>>>user3 [[890, 'to_add'], [506, 'to_add'], [454, 'to_add']]
this should help you a lot
and second: you're overcomplicating things. why create intermediary dictionaries when you just want to modify dictionary1?
for k,v in dictionary2.items():
for lst in v:
for lst2 in dictionary1[k]:
if lst[0] == lst2[0]:
lst2 = lst[1:]
CodePudding user response:
I have managed to do it - posting here for whom else needs some ideas. It's not the most prettiest, but it works:
interm = {}
for user in dictionary1:
if dictionary2[user]:
interm[user] = []
for element in dictionary1[user]:
for element2 in dictionary2[user]:
if str(element[0]) == str(element2[0]):
l = []
l.append(element[0])
l.append(element2[1])
interm[user].append(l)
for user in dictionary1:
if interm[user]:
for element3 in dictionary1[user]:
for element4 in interm[user]:
if str(element3[0]) == str(element4[0]):
element3.append(element4[1])
and the output becomes:
dictionary1 = {u'user3': [
[511, u'user3 phone', u'date1', u'date2', u'name', u'1', u'E'],
[125, u'user3 phone',u'date1', u'date2', u'name', u'', u'8'],
[854, u'user3 phone', u'date1', u'date2', u'name', u'1', u'3'],
[890, u'user3 Windows', u'date1', u'date2', u'name', u'', u'4', u'to_add'],
[506, u'user3 Windows', u'date1', u'date2', u'name', u'', u'7', u'to_add'],
[454, u'user3 Windows', u'date1', u'date2', u'name', u'', u'E', u'to_add']],
u'user2': [
[504, u'user2 Windows', u'date', u'date', u'20', u'', u'1', u'to_add']],
u'user1': [[u'N/A', u'N/A', u'N/A', u'N/A', u'N/A', u'N/A', u'N/A', u'N/A']]}