嘗試遍歷Python中的以下2d列表以查找x,y座標爲龜圖形。Python IndexError:列表索引超出範圍 - 2d列表迭代
data_set_01 = [['A', 1, 0, 'N'], ['A', 2, 1, 'E'], ['A', 3, 2, 'S'], ['A', 4, 3, 'W']]
有以下代碼:
def draw_icons(data_set):
for xpos in data_set: #find x co-ordinates
if data_set[[xpos][1]] == 0:
xpos = -450
elif data_set[[0][1]] == 1:
xpos = -300
elif data_set[[xpos][1]] == 2:
xpos = -150
elif data_set[[xpos][1]] == 3:
xpos = 0
elif data_set[[xpos][1]] == 4:
xpos = 150
elif data_set[[xpos][1]] == 5:
xpos = 300
for ypos in data_set: #find y co-ordinates
if data_set[[ypos][2]] == 0:
ypos = -300
elif data_set[[ypos][2]] == 1:
ypos = -150
elif data_set[[ypos][2]] == 2:
ypos = 0
elif data_set[[ypos][2]] == 3:
ypos = 150
goto(xpos,ypos)
pendown()
setheading(90)
commonwealth_logo()
收到以下錯誤:
if data_set[[xpos][1]] == 0:
IndexError: list index out of range
不知道我做錯了這裏。
最好學會使用調試器。例如參見['pdb'](https://docs.python.org/2/library/pdb.html)。 – juanchopanza
你想做什麼? – Cyphase
data_set_01不是data_set,所以你不用共享代碼,我願意打賭你沒有調試。 「爲什麼這個代碼沒有工作」的問題沒有足夠的上下文是沒有用的,如果沒有你研究這個錯誤並首先調試你的代碼,永遠不應該被問到。 –