gpt4 book ai didi

Python - 字典嵌套在列表中,列表嵌套在字典中

转载 作者:太空宇宙 更新时间:2023-11-03 15:46:10 25 4
gpt4 key购买 nike

当我执行 requests.get 时,我会收到如下所示的响应,数据来自 LoL API:

{
"champions": [
{
"id": 111,
"stats": {
"maxChampionsKilled": 2,
"maxNumDeaths": 5,
"mostChampionKillsPerSession": 2,
"mostSpellsCast": 0,
"totalAssists": 25,
"totalChampionKills": 2,
"totalDamageDealt": 40838,
"totalDamageTaken": 27900,
"totalDeathsPerSession": 5,
"totalDoubleKills": 0,
"totalFirstBlood": 0,
"totalGoldEarned": 11070,
"totalMagicDamageDealt": 21083,
"totalMinionKills": 56,
"totalPentaKills": 0,
"totalPhysicalDamageDealt": 12876,
"totalQuadraKills": 0,
"totalSessionsLost": 1,
"totalSessionsPlayed": 1,
"totalSessionsWon": 0,
"totalTripleKills": 0,
"totalTurretsKilled": 1,
"totalUnrealKills": 0
}
},

我正在努力获取“统计数据”中的数据。我真正想做的是类似于以下内容:

champ_data = received['champions']['stats']['totalSessionsPlayed']
print(champ_data)

但是它不起作用,因为“champions”后面有一个“[”,我收到错误:

TypeError: list indices must be integers, not str

最佳答案

你想要这个

received['champions'][0]['stats']

查看第一个冠军的统计数据。或者,这个

received['champions'][n]['stats']

nth 冠军的统计数据。或者,这个

[champion['stats'] for champion in received['champions']]

查看每个冠军的统计数据列表。

关于Python - 字典嵌套在列表中,列表嵌套在字典中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41732313/

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