在python

时间:2018-11-20 18:03:02

标签: python data-structures

我对python非常陌生。我在更新python数据结构时遇到问题,非常感谢您提供一些帮助。这是我的问题:

我的结构如下:

"players": [
    {
       "metadata": {
        "level": 11,
        "guild": "wizard",
        "kingdom" : "westonia"
       },
       "player_id": 34333
    },
    {
       "metadata": {
        "level": 11,
        "guild": "warrior",
        "kingdom": "Glorianth"
       },
       "player_id": 197456
    },
    ...etc...
]

现在,我想根据条件循环并更新其中一些播放器节点。所以我这样做:

for player in players:
   player_id = player[player_id]
   if has_level_bonus(player_id):
      player_level = player['metadata']['level']
      player_level = player_level + 10
      player['metadata']['level'] = player_level

这将正确更新player_level,但仅在提取的“ player”节点内。我的问题是如何将更新后的值“投入”到“玩家”结构中?

我尝试了players[player]['metadata']['level'] = player_level,但它给了我错误。我是python的新手,所以无法包扎。非常感谢您的帮助。

0 个答案:

没有答案