首页 > 解决方案 > 用户提供 MXN 矩阵,我想转换为 numpy 矩阵

问题描述

输入

2 3 **Here 2,3 are size of mateix m,n**
2 3 4
5 6 7

输出

[
  [2 3 4]
  [5 6 7]
]

注意:-使用 Numpy 包

标签: pythonpython-3.xnumpymatrixuser-input

解决方案


将mxn输入读取为 numpy 矩阵:

import numpy as np
m,n = map(int, input().split())
arr = np.zeros((m,n))
for i in range(m):
    arr[i] = np.array(list(map(int, input().split())))

输出 :

array([[1., 2., 3.],
       [4., 5., 6.]])

推荐阅读