我正在努力将第二个字典的值合并到初始字典中。
初始字典是:
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']]}
匹配值:user2为504,用户3为890、506、454。我需要输出为:
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']]}
我已经尝试了多种方法来检查和匹配,但我在一个迷路。这里是我卡住的地方:
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]
但是这种方式只是将整个列表添加到每个列表的末尾…
您可以使用items()
方法遍历字典:
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']]
这应该对你很有帮助
第二,你把事情弄得太复杂了。当您只想修改dictionary1时,为什么要创建中间字典呢?
for k,v in dictionary2.items():
for lst in v:
for lst2 in dictionary1[k]:
if lst[0] == lst2[0]:
lst2 += lst[1:]
我已经设法做到了——在这里为其他需要一些想法的人发布。它不是最漂亮的,但它有效:
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])
,输出变成:
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']]}