我正在编写一个国际象棋程序和代码检查。我需要从对手移动字典(其中包含国王的位置)的关键是用来找到一块放在检查它的坐标。现在这是给我的错误:
opponentpieceposition=opponentposition.get(piece)
TypeError: unhashable type: 'list'.
注意下面的例子应该打印(1,6)
king=(5,1)
opponentmoves={'ksknight': [(8, 3), (5, 2), (6, 3)],
'ksbishop': [(3, 6), (4, 7), (5, 8), (1, 4), (1, 6), (3, 4), (4, 3), (5, 1), (6, 1)],
'king': [(6, 1), (5, 2), (4, 1)],
'queen': [(4, 5), (2, 4), (1, 3), (2, 6), (1, 7), (4, 4)],
'qsknight': [(3, 3), (1, 3)]}
opponentposition={'ksknight': (1, 3),
'ksbishop': (1, 6),
'king': (6, 1),
'queen': (4, 5),
'qsknight': (3, 3)}
if king in [z for v in opponentmoves.values() for z in v]:
piece=[key for key in opponentmoves if king in opponentmoves[key]]
opponentpieceposition=opponentposition.get(piece)
print(opponentpieceposition)
这就是我要做的。你知道吗
在您的代码段中是一个列表,它不能是字典键。请按照代码中的注释说明如何克服此问题:
希望这有助于解决问题。你知道吗
其他可变类型的列表和对象不能用作字典中的键(或集合中的元素)。你知道吗
这些容器依赖于计算哈希值,哈希值是插入时对象的“内容”的函数。因此,如果对象(如可变对象)在插入后发生更改,则会出现问题。你知道吗
您可以改为使用一个元组,它是一个不可变的序列。你知道吗
duplicate
相关问题 更多 >
编程相关推荐