gpt4 book ai didi

python - 有没有办法从多个字典中访问具有相同名称的键?

转载 作者:太空宇宙 更新时间:2023-11-04 01:55:55 26 4
gpt4 key购买 nike

我有多个具有可预测名称的词典。每个字典包含 3 个元素。我如何才能从每个字典中提取特定键并找到总值?

player_A = {
"kills": 15
"death": 3
"kda": 5
}

player_B = {
"kills": 20
"death": 4
"kda": 5
}
:
player_ZA = {
"kills": 35
"death": 7
"kda": 5
}

假设我想找出所有玩家的总击杀数。我应该如何访问字典中名为“kill”的所有键并找到这些键的总和?

即。预期输出类似于:

sum(15 + 20 + ... + 35)

最佳答案

player_a = {"kills": 15, "death": 3, "kda": 5}
player_b = {"kills": 20, "death": 4, "kda": 5}
player_c = {"kills": 35, "death": 7, "kda": 5}

players = [player_a, player_b, player_c]

total_kills = sum(player["kills"] for player in players)

关于python - 有没有办法从多个字典中访问具有相同名称的键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56823764/

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