【问题标题】:How to update a dict value with Python如何使用 Python 更新 dict 值
【发布时间】:2015-04-20 14:38:30
【问题描述】:

我有一本像下面这样的字典:

sample_dict = {
      "matchings": [
        {
          "indices": [
            0,
            1,
            2
          ],
          "geometry": "hemo_Ab}gueC????",
          "matched_points": [
            [
              -33.823845,
              -70.619614
            ],
            [
              -33.823845,
              -70.619614
            ],
            [
              -33.823845,
              -70.619614
            ]
          ]
        }
      ]
    }

如何更新几何值?

【问题讨论】:

  • sample_json["matchings"][0]["geometry"] = "whatever"
  • 这是 .json 文件还是 .py 文件?
  • 货号不详怎么办?我将如何遍历它?
  • 那不是“json”,是Python dict
  • 谢谢吉文。我从 json 创建了字典。你是对的,现在它是一个字典。

标签: python json


【解决方案1】:

您可以使用以下语法遍历您的 matchings

for i, matching in enumerate(sample_json['matchings']):
    # i is your matching's index
    # matching is your current matching object
    my_new_value = 'something'
    sample_json["matchings"][i]["geometry"] = my_new_value

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-23
    • 1970-01-01
    • 2020-03-01
    • 2018-03-31
    • 2017-09-29
    • 2023-03-29
    • 2021-12-25
    • 2018-09-03
    相关资源
    最近更新 更多