gpt4 book ai didi

python - 如果一个字典的值与第二个字典的键和第二个字典值 Python 相同

转载 作者:行者123 更新时间:2023-12-04 01:05:10 25 4
gpt4 key购买 nike

我想根据第一个字典对第二个字典的值求和。如果我有字典 A 和 B。

A = {"Mark": ["a", "b", "c", "d"], "June": ["e", "a"], "John": ["a", "b", "f"]}
B = {"a": 1, "b": 2, "c": 3, "d": 0, "e": 3, "f": 2}

结果应该是:

Mark=6, June = 4, John = 5

提前致谢

最佳答案

你可以这样做:

for key, value in A.items():
_sum = 0
for val in value:
_sum += B[val]
print(key, '=', _sum)

输出:

Mark = 6
June = 4
John = 5

关于python - 如果一个字典的值与第二个字典的键和第二个字典值 Python 相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66702832/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com