【问题标题】:Why does it say unhashable type: 'list' even though it is a string? [duplicate]为什么它说 unhashable type: 'list' 即使它是一个字符串? [复制]
【发布时间】:2020-08-16 02:09:20
【问题描述】:
game_locs = ['a1', 'a2', 'a3', 'a4', 'b1', 'b2', 'b3', 'b4']
ZONENAME = ['Town Market', 'Town Entrance', 'Town Square', 'Town Hall', "", 'Home', "", "", ""]
DESCRIPTION = 'description'
EXAMINATION = 'examine'
SOLVED = False
UP = 'up', 'north'
DOWN = 'down', 'south'
LEFT = 'left', 'west'
RIGHT = 'right', 'east'

zonemap = {}
for i in range(len(game_locs)):
    x = game_locs[i]
    if x[0] == 'a':
        if x[1] == '1':
            zonemap.update({'a1': {
                ZONENAME: ZONENAME[i],
                DESCRIPTION: "description",
                EXAMINATION: "examine",
                SOLVED: False,
                UP: "You cannot move up from here",
                DOWN: game_locs[i + 4],
                LEFT: "You cannot move left from here",
                RIGHT: game_locs[i + 1]
            }})
        elif x[1] == '4':
            zonemap.update({'a4': {
                ZONENAME: ZONENAME[i],
                DESCRIPTION: "description",
                EXAMINATION: "examine",
                SOLVED: False,
                UP: "You cannot move up from here",
                DOWN: game_locs[i + 4],
                LEFT: game_locs[i - 1],
                RIGHT: "You cannot move right from here"}
            })
        else:
            zonemap.update({f'a{indx}': {
                ZONENAME: ZONENAME[i],
                DESCRIPTION: "description",
                EXAMINATION: "examine",
                SOLVED: False,
                UP: "You cannot move up from here",
                DOWN: game_locs[i + 4],
                LEFT: game_locs[i - 1],
                RIGHT: game_locs[i + 1]}
            })
    elif x[0] == 'b':
        if x[1] == '1':
            zonemap.update({'b1': {
                ZONENAME: ZONENAME[i],
                DESCRIPTION: "description",
                EXAMINATION: "examine",
                SOLVED: False,
                UP: game_locs[i - 4],
                DOWN: game_locs[i + 4],
                LEFT: "You cannot move left from here",
                RIGHT: game_locs[i + 1]}
            })
        elif x[1] == '4':
            zonemap.update({'b4': {
                ZONENAME: ZONENAME[i],
                DESCRIPTION: "description",
                EXAMINATION: "examine",
                SOLVED: False,
                UP: game_locs[i - 4],
                DOWN: game_locs[i + 4],
                LEFT: game_locs[i - 1],
                RIGHT: "You cannot move right from here"}
            })
        else:
            zonemap.update({f'b{indx}': {
                ZONENAME: ZONENAME[i],
                DESCRIPTION: "description",
                EXAMINATION: "examine",
                SOLVED: False,
                UP: game_locs[i - 4],
                DOWN: game_locs[i + 4],
                LEFT: game_locs[i - 1],
                RIGHT: game_locs[i + 1]}
            })

但我收到错误消息

    zonemap.update({'a1': {
TypeError: unhashable type: 'list'

这是什么原因? 'a1' 显然是一个字符串,键是字典而不是列表,那么我收到此错误的原因是什么?我正在使用 PyCharm 来测试代码并使用 Python 3.8。字典绝对是可散列的,对吗?所以我不明白为什么会这样。背景:基于txt的RPG游戏

【问题讨论】:

    标签: python


    【解决方案1】:
    ZONENAME = ['Town Market', 'Town Entrance', 'Town Square', 'Town Hall', "", 'Home', "", "", ""]
    
    zonemap.update({'a1': {
        ZONENAME: ZONENAME[i],
        ...
    })
    

    错误给出了错误的大致位置,但由于它是多行语句,因此错误实际上位于嵌套字典的下一行。 ZONENAME 是一个列表,列表不能用作键。

    要解决这个问题,不要使用常量作为键。使用他们的名字作为键:引用名字。

    zonemap.update({'a1': {
        'ZONENAME': ZONENAME[i],
        'DESCRIPTION': "description",
        'EXAMINATION': "examine",
        'SOLVED': False,
        'UP': "You cannot move up from here",
        'DOWN': game_locs[i + 4],
        'LEFT': "You cannot move left from here",
        'RIGHT': game_locs[i + 1]
    }})
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-03
      • 2021-03-14
      • 1970-01-01
      • 2020-08-22
      • 2020-04-07
      • 2013-05-28
      • 2020-05-03
      • 2011-01-13
      相关资源
      最近更新 更多