如何在PYTHON中使用raw_input()初始化2D整数数组

时间:2016-07-16 14:49:22

标签: python arrays input initialization raw-input

我需要在PYTHON中初始化用户输入的2D整数数组[10 * 10]。 这是什么代码?我试过这样做,但它显示错误,因为列表索引超出范围

board = [[]]
for i in range(0,10):
    for j in range(0,10):
        board[i].append(raw_input())

追踪(最近一次通话):   文件" solution.py",第162行,in     板[I] .append(的raw_input()) IndexError:列表索引超出范围

1 个答案:

答案 0 :(得分:0)

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值