python IndexError:列表索引超出范围
问题描述:
我想从下面的板创建一个矩阵,与第一个项目坐标(0,0)与下面的代码,但我不断收到列表索引超出范围错误...有人可以帮我解决或纠正我应该先前的方式吗?python IndexError:列表索引超出范围
board_5x = ['orange', 'green', 'blue', 'orange', 'red',
'orange', 'blue', 'orange', 'orange', 'yellow',
'blue', 'orange', 'blue', 'red', 'green',
'yellow', 'orange', 'green', 'orange', 'red',
'orange', 'blue', 'yellow', 'red', 'green',
'red', 'yellow', 'blue', 'blue', 'yellow',
'green', 'orange', 'orange', 'green', 'green',
'orange', 'green', 'red', 'red', 'green',
'orange', 'yellow', 'yellow', 'red', 'orange',
'red', 'green', 'blue', 'blue', 'orange']
board=[]
for row in range(10):
for col in range(5):
board[row][col]=board_5x[row*5+col]
print board
答
您还没有放置值board
,所以你还不如继续构建其与新值在一个通与理解:
board = [[board_5x[row*5+col] for col in range(5)] for row in range(10)]