现有 a , b 两个字典,他们都有一个共同的id 1 , 实现dict的合并
和mysql中 select a.id,a.MUT,b.neighbor from a full join b on a.id = b.id
执行结果很类似
a = {
"id": "1",
"MUT": "1500",
}
b = {
"id": "1",
"neighbor": [2]
}
# result = addfunction(a,b)
result = {
"id": "1",
"MUT": "1500",
"neighbor": [2]
}
如何实现?
考虑复杂情况:
a = [
{
"id": "1",
"MUT": "1500",
},
{
"id": "2",
"MUT": "1500",
}
]
b = [
{
"id": "1",
"neighbor": [2]
},
{
"id": "3",
"neighbor": [2]
}
]
# result = addfunction(a,b)
result = [
{
"id": "1",
"MUT": "1500",
"neighbor": [2]
},
{
"id": "2",
"MUT": "1500",
},
{
"id": "3",
"neighbor": [2]
}
]
雷雷
因为join on 条件可能时多个值,我在stack上找了个代码改了一下。