現有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上找了個代碼改了一下。