首页 > 解决方案 > 如何创建n个矩阵

问题描述

我不使用python但是我试图运行矩阵代码,是否可以根据用户输入创建n个矩阵?用户将输入行数,例如 3 ,所以我需要生成 3 个矩阵

这就是我所拥有的,但是我手动创建了矩阵

  
R = int(input("Enter the number of rows:"))
C = R+1
  
  
print("Enter the entries in a single line (separated by space): ")
  
# User input of entries in a 
# single line separated by space
entries = list(map(int, input().split()))
  
# For printing the matrix
matrix = np.array(entries).reshape(R, C)
print(matrix)

de = matrix[:,0:R]
print(de)

    
de0 = np.copy(de)
de0[:,0] = matrix[:,R]
de1 = np.copy(de)
de1[:,1] = matrix[:,R]
de2 = np.copy(de)
de2[:,2] = matrix[:,R]

在这里我假设用户输入为 3,我做了 de0,de1,de2,但如果它是 10,那么我需要 de0,de1...de9,所以可以根据用户输入自动创建它们吗?

标签: pythonarraysmatrix

解决方案


我看到您需要动态创建变量名。使用全局变量()


# This is going to create 10 variables de0, de1,.., de9 and assign None to those
for ind in range(10):
    globals()[f'de{ind}'] = None # Replace None with whatever you need to assign

推荐阅读