如何在 PYTHON 中使用 raw_input() 初始化二维整数数组
How to initialize a 2D integer array using raw_input() in PYTHON
我需要初始化一个 2D 整数数组 [10*10],以获取 PYTHON 中用户的输入。
这是什么代码?我试过这样做,但它显示错误,因为列表索引超出范围
board = [[]]
for i in range(0,10):
for j in range(0,10):
board[i].append(raw_input())
回溯(最后一次调用):
文件 "solution.py",第 162 行,位于
board[i].append(raw_input())
IndexError: 列表索引超出范围
board = []
for i in range(10):
row = []
for j in range(10):
row.append(j)
# row.append(raw_input())
board.append(row)
>>> board
[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]
>>>
出于测试目的,我插入了计数器而不是 raw_input 值
我需要初始化一个 2D 整数数组 [10*10],以获取 PYTHON 中用户的输入。
这是什么代码?我试过这样做,但它显示错误,因为列表索引超出范围
board = [[]]
for i in range(0,10):
for j in range(0,10):
board[i].append(raw_input())
回溯(最后一次调用): 文件 "solution.py",第 162 行,位于 board[i].append(raw_input()) IndexError: 列表索引超出范围
board = []
for i in range(10):
row = []
for j in range(10):
row.append(j)
# row.append(raw_input())
board.append(row)
>>> board
[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]
>>>
出于测试目的,我插入了计数器而不是 raw_input 值