在Python中读取2d列表作为空格分隔的值

时间:2019-07-31 22:45:13

标签: python arrays python-3.x list

我想从用户那里读取二维列表,以空格分隔的值,例如这种格式:

0 0 0 0 0  
0 0 0 0 1  
0 0 0 0 0  
0 0 0 0 0  
0 0 0 0 0  

这是我的代码:

# Read Matrix of 5X5
mat = [[int(input()) for x in range(5)] for y in range(5)]

1 个答案:

答案 0 :(得分:0)

也许您可以尝试以下方法:

mat = [list(map(int, input().split())) for y in range(5)]

这将需要5行一行的列表来创建矩阵。