我試圖反映對稱組的所有排列的平方。 正方形的「座標」用作字典中的鍵。現在假設值 沿着x軸反射。但由於某種原因 .pop()只想接受整數?我認爲它接受任何不包含不可變的類型,並且如果鍵不在字典中,則會引發KeyError。這是我的代碼:Python3類型錯誤dict.pop
def y_reflection(insquare):
"""Function that reflects a square against it's y axis:
<----- reflection
--> x
n n-1 ... 2 1 | 1 2 ... n-1 n
n-1 n-2 ... 1 n | | n 1 ... n-2 n-1
............... | v ................
2 1 ... 4 3 | y 3 4 ... 1 2
1 n ... 3 2 | 2 3 ... n 1"""
for y in range (0, SQUARE_SIZE()):
for x in range (0, SQUARE_SIZE()):
insquare[(y,x)]=insquare.pop((y,(SQUARE_SIZE()-x)))
return insquare
insquare[(x,y)]=insquare.pop((x,(SQUARE_SIZE()-y)))
TypeError: 'tuple' object cannot be interpreted as an integer
任何想法是怎麼回事?
到底是什麼'SQUARE_SIZE'?和'insquare'? – shuttle87
SQUARE_SIZE是n。軸的長度。 Insquare是我描述的字典。它只是座標(x,y)作爲鍵和值0
Elias