我如何循环遍历字典并设置以达到我所需的值?
我的代码具有嵌套词典,我想访问每个参与者的“助攻”价值并将其附加。我该怎么做?
gameData = {
"metadata": {
"dataVersion": "2",
"matchId": "NA1_4263873346",
"participants": [
"Sw_wTB5fzxXvyXeCovgcGhKNw4hLKzgcEvWFKzMqZWtfiJ7HtbxYOK6Nb7nBU5SR-B3bNt4Ay9bvjQ",
"-t2_OfuyZFaCdZJ1lvbbfRFgYS1FWZcGhIsqj-8m-SS9UZ9wFyYeWBiGkcMgNEl_geH5CF9tX4SAzQ",
"A-n0X4QWr8Jr0PISogZK3VpnIqqVbm87jchMYpTrUrhiSfeoxVCl8ImnJxaE_lg9pIAdxNgaFpkT7g"
]
},
"info": {
"gameVersion": "12.6.431.7988",
"mapId": 11,
"participants": [
{
"assists": 9,
"deaths": 5,
"kills": 1
},
{
"assists": 1,
"deaths": 3,
"kills": 1
},
{
"assists": 3,
"deaths": 5,
"kills": 6
}
]
}
我用它来循环通过它们,但它返回错误“ typeError:列表索引必须是整数或切片,而不是str”。
participants = []
for row in gameData['metadata']['participants']:
participants_row = {}
participants_row['assists'] = gameData['info']['participants']['assists']
participants.append(participants_row)
谁能帮助我了解如何循环通过它来获得所需的价值?
I have the following code with a nested dictionary and I want to access the "assists" value for each participant and append it. How do I do that?
gameData = {
"metadata": {
"dataVersion": "2",
"matchId": "NA1_4263873346",
"participants": [
"Sw_wTB5fzxXvyXeCovgcGhKNw4hLKzgcEvWFKzMqZWtfiJ7HtbxYOK6Nb7nBU5SR-B3bNt4Ay9bvjQ",
"-t2_OfuyZFaCdZJ1lvbbfRFgYS1FWZcGhIsqj-8m-SS9UZ9wFyYeWBiGkcMgNEl_geH5CF9tX4SAzQ",
"A-n0X4QWr8Jr0PISogZK3VpnIqqVbm87jchMYpTrUrhiSfeoxVCl8ImnJxaE_lg9pIAdxNgaFpkT7g"
]
},
"info": {
"gameVersion": "12.6.431.7988",
"mapId": 11,
"participants": [
{
"assists": 9,
"deaths": 5,
"kills": 1
},
{
"assists": 1,
"deaths": 3,
"kills": 1
},
{
"assists": 3,
"deaths": 5,
"kills": 6
}
]
}
I use this to loop through them but it returns the error "TypeError: list indices must be integers or slices, not str".
participants = []
for row in gameData['metadata']['participants']:
participants_row = {}
participants_row['assists'] = gameData['info']['participants']['assists']
participants.append(participants_row)
Can anyone help me understand how to loop through it to get the desired values?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
访问
dict
的部分时,您有几个错误。我认为您的意思是:
您的原始内容:
在Gamedata ['Metadata'] ['参与者']中的行:
正在通过错误的参与者
列表进行迭代。您的意思是:在Gamedata ['info'] ['参与者']中的行:
You have several mistakes in accessing parts of the
dict
.I think you meant this:
Your original:
for row in gameData['metadata']['participants']:
is iterating over the wrongparticipants
list. You meant:for row in gameData['info']['participants']:
我认为这条代码会导致错误。要调试此问题,请将其分解为较小的零件:
如果这样做,您会发现第三行会导致错误。因此,添加
现在我们看到
参与者
是一个列表,但是您像词典一样使用它。我认为您需要每个
参与者
的协助
。做到这一点的一种方法是列表理解:旁注:
Python团队正在努力改进错误消息,以使消息指向导致这种错误的特定索引。我不知道它将发布什么版本,但是我很期待它,因为这种类型的嵌套索引非常普遍。
I assume this line of code causes the error. To debug this, break it down into smaller pieces:
If you do this, you will see that the third line causes the error. So add
Now we see that
participants
is a list, but you are using it like a dictionary.I think you want the
assists
from eachparticipant
. One way to do this is with a list comprehension:Side note:
The python team is working on improving error messages so that the message points at the specific index that causes this kind of error. I don't know what version it will be released in, but I'm looking forward to it because this type of nested indexing is very common.