如何将这样格式的矩阵作为python3中的输入

我知道有类似的问题,但没有人对这种特定类型的格式有答案。对于codeforces问题,我需要这样输入矩阵:


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

我想将它存储为一个矩阵,其中 m[i][j] 与上面的输入完全相同。例如,我想将输入存储为一个矩阵,其中 [4][1] 与 1 相关。如何在 python3 中获取此输入?如果有帮助,我在此处附上了问题:https ://codeforces.com/problemset/problem/263/A 。唯一需要注意的是矩阵总是 5x5。


UYOU
浏览 81回答 1
1回答

MYYA

利用:N = 5 # Shape of the square matrix(NxN)matrix = [list(map(int, input().split())) for _ in range(N)]print(matrix)print("Element at 2 row and 5 column is:", matrix[1][4])输出:[[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]]Element at 2 row and 5 column is: 1
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python