这是我的字典:
main_dict = [
{'link_value':[123,111] , 'id' : 22},
{'link_value':[234] , 'id' : 14}
]
dict2 = [
{'id':123 , value:['xx','yy'],name:'mg1'},
{'id':111 , value:['zz','yy'],name:'mg2'},
{'id':234 , value:['aa','yy'],name:'mg3'}
]
我想用dict2数组中的元素替换link_value
expected_result:
[
{'link_value':[{'id':123 , value:['xx','yy'],name:'mg1'} ,{'id':111 , value:['zz','yy'],name:'mg2'}] , 'id' : 22},
{'link_value':[{'id':234 , value:['aa','yy'],name:'mg3'}] , 'id' : 14}
]
答案 0 :(得分:0)
在dict1值中遍历link_value,并遍历列表并将其与id进行比较(如果匹配),将匹配的dict1值附加到dict2上
答案 1 :(得分:0)
像这样吗?
for i in range(len(main_dict)):
main_dict[i]['link_value'] = [dict for dict in dict2 if dict['id'] in main_dict[i]['link_value']]
# main_dict[i]['link_value'] = list(filter(lambda a: a['id'] in main_dict[i]['link_value'], dict2))