英文:
Find different values between 2 lists of dictionaries with different keys
问题
Here's the translated code portion:
我有2个列表:
list1 = [
{
"address": "1000",
"amount": 0
},
{
"address": "2000",
"amount": 0
},
{
"address": "3000",
"amount": 0
},
{
"address": "4000",
"amount": 20
}
]
list2 = [
{
"account": "1000",
"balance": 100
},
{
"account": "2000",
"balance": 200
},
{
"account": "3000",
"balance": 300
}
]
我想要搜索list2,看看来自list1键“address”的值是否已经作为与键“account”关联的值存在于list2中。
如果没有,将“address”和“amount”都追加到list2中,作为“account”和“balance”。
示例:
list1的“address”为4000在list2中不存在“account: 4000”,所以我希望将整个字典添加到list2中:
{
"account": "4000",
"balance": 20
}
Let me know if you need any further assistance with this code.
英文:
I have 2 lists:
list1 = [
{
"address": "1000",
"amount": 0
},
{
"address": "2000",
"amount": 0
},
{
"address": "3000",
"amount": 0
},
{
"address": "4000",
"amount": 20
}
]
list2 = [
{
"account": "1000",
"balance": 100
},
{
"account": "2000",
"balance": 200
},
{
"account": "3000",
"balance": 300
}
]
I want to search list2
and see if the value from list1
key "address" is already present in list2
as a value tied to "account" key.
If it is not, append both "address" & "amount" to list2
as "account" & "balance".
Example:
list1
"address" of 4000 is not in list2
as account: 4000
, so I want to add that entire dict to list2
as:
{
"account": "4000",
"balance": 20
}
What I have tried already based on other SO questions:
def unique_values_from_list(dict_list):
all_values = set()
for dictionary in dict_list:
all_values.update(dictionary.values())
return all_values
unique_1 = unique_values_from_list(list1)
unique_2 = unique_values_from_list(list2)
intersection = unique_1.intersection(unique_2)
Which gives the values of keys: address
/account
that are in both lists of dicts.
But I don't know where to go from there...
答案1
得分: 2
你可能想要构建一个list2
内部字典的值集(对于“account”键),然后循环遍历list1
:
keys2 = {d['account'] for d in list2 if 'account' in d}
# {'1000', '2000', '3000'}
out = [{'account': d['address'], 'balance': d['amount']}
for d in list1
if (a:=d.get('address', None)) and a not in keys2]
print(out)
输出:
[{'account': '4000', 'balance': 20}]
如果你更愿意更新list2
:
keys2 = {d['account'] for d in list2 if 'account' in d}
# {'1000', '2000', '3000'}
for d in list1:
if (a:=d.get('address', None)) and a not in keys2:
list2.append({'account': d['address'], 'balance': d['amount']})
print(list2)
输出:
[{'account': '1000', 'balance': 100},
{'account': '2000', 'balance': 200},
{'account': '3000', 'balance': 300},
{'account': '4000', 'balance': 20}]
英文:
You might want to build a set of list2
inner dictionaries values (for the "account" key), then loop over list1:
keys2 = {d['account'] for d in list2 if 'account' in d}
# {'1000', '2000', '3000'}
out = [{'account': d['address'], 'balance': d['amount']}
for d in list1
if (a:=d.get('address', None)) and a not in keys2]
print(out)
Output:
[{'account': '4000', 'balance': 20}]
If you rather want to update list2
:
keys2 = {d['account'] for d in list2 if 'account' in d}
# {'1000', '2000', '3000'}
for d in list1:
if (a:=d.get('address', None)) and a not in keys2:
list2.append({'account': d['address'], 'balance': d['amount']})
print(list2)
Output:
[{'account': '1000', 'balance': 100},
{'account': '2000', 'balance': 200},
{'account': '3000', 'balance': 300},
{'account': '4000', 'balance': 20}]
答案2
得分: 0
Another working method would be to convert list1 and list 2 to dicts, and update the first dict with the 2nd:
dict1 = {'1000': 100, '2000': 200, '3000': 300, '4000': 20}
Then you can turn dict1 back to a list if you wish (though it's probably easier to use by keeping it as is):
list3 = [{'account': key, 'balance': val} for key, val in dict1.items()]
list3 = [{'account': '1000', 'balance': 100},
{'account': '2000', 'balance': 200},
{'account': '3000', 'balance': 300},
{'account': '4000', 'balance': 20}]
英文:
Another working method would be to convert list1 and list 2 to dicts, and update the first dict with the 2nd:
dict1 = {e['address']:e['amount'] for e in list1}
dict2 = {e['account']:e['balance'] for e in list2}
dict1.update(dict2)
# {'1000': 100, '2000': 200, '3000': 300, '4000': 20}
Then you can turn dict1 back to a list if you wish (though it's probably easier to use by keeping it as is):
list3 = [{'account':key, 'balance':val} for key, val in dict1.items()]
# [{'account': '1000', 'balance': 100},
# {'account': '2000', 'balance': 200},
# {'account': '3000', 'balance': 300},
# {'account': '4000', 'balance': 20}]
答案3
得分: 0
你可以搜索list1中地址的值,这些值不在list2中,并将它们添加到list2中。以下是代码的翻译部分:
for i in list1:
if i.get("address") not in [value['account'] for value in list2]:
list2.append({"account": i.get("address"), "balance": i.get("amount")})
print(list2)
输出:
[{'account': '1000', 'balance': 100}, {'account': '2000', 'balance': 200}, {'account': '3000', 'balance': 300}, {'account': '4000', 'balance': 20}]
英文:
You can search for the values of address in list1 that are not in list2 and append them to the list2.
for i in list1:
if i.get("address") not in list(value['account'] for value in list2):
list2.append({"account": i.get("address"), "balance": i.get("amount")})
print(list2)
Output:
[{'account': '1000', 'balance': 100}, {'account': '2000', 'balance': 200}, {'account': '3000', 'balance': 300}, {'account': '4000', 'balance': 20}]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论