我如何循环遍历字典并设置以达到我所需的值?

发布于 2025-01-25 21:56:33 字数 1160 浏览 2 评论 0原文

我的代码具有嵌套词典,我想访问每个参与者的“助攻”价值并将其附加。我该怎么做?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝眸 2025-02-01 21:56:34

访问dict的部分时,您有几个错误。

我认为您的意思是:

# gameData = {"metadata": { ...
participants = []
for row in gameData['info']['participants']:
    participants_row = {}
    participants_row['assists'] = row['assists']
    participants.append(participants_row)
print(participants)

您的原始内容:在Gamedata ['Metadata'] ['参与者']中的行:正在通过错误的参与者列表进行迭代。您的意思是:在Gamedata ['info'] ['参与者']中的行:

You have several mistakes in accessing parts of the dict.

I think you meant this:

# gameData = {"metadata": { ...
participants = []
for row in gameData['info']['participants']:
    participants_row = {}
    participants_row['assists'] = row['assists']
    participants.append(participants_row)
print(participants)

Your original: for row in gameData['metadata']['participants']: is iterating over the wrong participants list. You meant: for row in gameData['info']['participants']:

马蹄踏│碎落叶 2025-02-01 21:56:34
    participants_row['assists'] = match_detail['info']['participants']['assists']

我认为这条代码会导致错误。要调试此问题,请将其分解为较小的零件:

info = match_detail['info']
participants = info['participants']
assists = participants['assists']

如果这样做,您会发现第三行会导致错误。因此,添加

print(participants)

现在我们看到参与者是一个列表,但是您像词典一样使用它。

我认为您需要每个参与者协助。做到这一点的一种方法是列表理解:

assists = [p['assists'] for p in participants]

旁注:

Python团队正在努力改进错误消息,以使消息指向导致这种错误的特定索引。我不知道它将发布什么版本,但是我很期待它,因为这种类型的嵌套索引非常普遍。

    participants_row['assists'] = match_detail['info']['participants']['assists']

I assume this line of code causes the error. To debug this, break it down into smaller pieces:

info = match_detail['info']
participants = info['participants']
assists = participants['assists']

If you do this, you will see that the third line causes the error. So add

print(participants)

Now we see that participants is a list, but you are using it like a dictionary.

I think you want the assists from each participant. One way to do this is with a list comprehension:

assists = [p['assists'] for p in participants]

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文