【问题标题】:Changing keys after loading json file dictionary加载 json 文件字典后更改键
【发布时间】:2022-01-24 07:08:07
【问题描述】:

好的,所以我遇到了 JSON、键和字符串的问题。我在 python 中使用 JSON 转储来保存我的游戏词典,它确实有效。问题是当我加载字典时,我正在制作的游戏使用 int 值作为世界目录中的键,但 JSON 将键存储为字符串。这是我做的随机生成。

worldmap = {
   'Regions': {
1: 'Zelbridge', 2: 'Forest Path', 3: 'Baronbell', 4: 'Old Path', 5: 'Cariva', 6: 'Prairie Path'},

   'Zelbridge': {1: 'Field', 2: 'Prairie Path', 3: 'School', 4: 'Mountain Path', 5: 'Graveyard',
      6: 'Old Path', 7: 'Blacksmith', 8: 'Forest Path', 9: 'Doctor', 0: 'Zelbridge'},

   'Forest Path': {1: 'Trees', 2: 'Bushes', 3: 'Path', 4: 'Cariva', 5: 'Path',
      6: 'Baronbell', 7: 'Path', 8: 'Zelbridge', 9: 'Path', 0: 'Forest Path'}, 

   'Baronbell': {1: 'House', 2: 'Mountain Path', 3: 'Graveyard', 4: 'Old Path', 5: 'Field',
      6: 'Forest Path', 7: 'Church', 8: 'Prairie Path', 9: 'Shop', 0: 'Baronbell'},

   'Old Path': {1: 'Path', 2: 'Trees', 3: 'Bushes', 4: 'Cariva', 5: 'Path', 
     6: 'Zelbridge', 7: 'Trees', 8: 'Baronbell', 9: 'Trees', 0: 'Old Path'}, 

   'Cariva': {1: 'Cellar', 2: 'Old Path', 3: 'Graveyard', 4: 'Mountain Path', 5: 'Town Hall', 
     6: 'Prairie Path', 7: 'School', 8: 'Forest Path', 9: 'Blacksmith', 0: 'Cariva'}, 

   'Prairie Path': {1: 'Bushes', 2: 'Path', 3: 'Path', 4: 'Zelbridge', 5: 'Trees', 
     6: 'Cariva', 7: 'Trees', 8: 'Baronbell', 9: 'Path', 0: 'Prairie Path'}
}

所以当我使用加载函数时,由于 int 被转换为字符串,我得到了关键错误。我尝试了一个 for 循环来迭代键并将它们改回来,但我收到了关于字典更改的错误。这是我尝试加载不同(也是随机)世界的示例。它设置为在每个循环后打印,表明它可以工作

What was your hero's name? #Input hero name
Loading...
{'2': 'Prairie Path', '3': 'Cariva', '4': 'Old Path', '5': 'Baronbell', '6': 'Mountain Path', 1: 'Zelbridge'} #Region number 1 no longer string

{'3': 'Cariva', '4': 'Old Path', '5': 'Baronbell', '6': 'Mountain Path', 1: 'Zelbridge', 2: 'Prairie Path'} #Region numbers 1 and 2 no longer string

{'4': 'Old Path', '5': 'Baronbell', '6': 'Mountain Path', 1: 'Zelbridge', 2: 'Prairie Path', 3: 'Cariva'} #ect

{'5': 'Baronbell', '6': 'Mountain Path', 1: 'Zelbridge', 2: 'Prairie Path', 3: 'Cariva', 4: 'Old Path'} #ect

{'6': 'Mountain Path', 1: 'Zelbridge', 2: 'Prairie Path', 3: 'Cariva', 4: 'Old Path', 5: 'Baronbell'} # Region numbers 1, 2, 3, 4, and 5 no longer string

{'6': 'Mountain Path', 1: 'Zelbridge', 2: 'Prairie Path', 3: 'Cariva', 5: 'Baronbell'}

Traceback (most recent call last):
  File "C:/Users/crazy/PycharmProjects/rpg/main.py", line 581, in load
    for key in worldmap["Regions"]:
RuntimeError: dictionary changed size during iteration

Process finished with exit code 1

我不确定它有什么问题,但遗憾的是,我还必须为区域内的每个位置执行此操作。感谢您提供任何和所有帮助,因为我已经查看了 SO 和谷歌但无济于事。

        with open(world_file) as infile:
            worldmap = json.load(infile)
            copy = worldmap.copy()
            regions = worldmap["Regions"]
            locations = copy.pop("Regions")
            for key in worldmap["Regions"]:
                value = worldmap["Regions"][key]
                new_key = int(key)
                worldmap["Regions"].update({new_key: value})
                worldmap ["Regions"].pop(key)
                print(str(worldmap["Regions"]) + "\n")```

【问题讨论】:

    标签: python json dictionary


    【解决方案1】:

    以这种方式使用 For 循环并更新循环本身的键:

    mydict = {1: 'a', 2: 'b'}
    for index, (key, value) in enumerate(mydict.items()):
        print("index: {}, key: {}, value: {}".format(index, key, value))
        mydict[index] = mydict.pop(key)
    

    或者使用可以使用 List 来强制复制密钥:

    mydict = {1: 'a', 2: 'b'}
    for index, key in enumerate(list(mydict)):
        mydict[index] = mydict.pop(key)
     
     # which will give output like:
     # ---------------------------
     # {0: 'a', 1: 'b'}
    

    【讨论】:

      猜你喜欢
      • 2013-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-22
      • 1970-01-01
      • 2019-12-24
      相关资源
      最近更新 更多