python - 如何创建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,所以可以根据用户输入自动创建它们吗?
解决方案
我看到您需要动态创建变量名。使用全局变量()
# 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
推荐阅读
- java - 如何在我的主要活动中使用片段中的 ImageViews
- c# - 如何将文件扩展名与在文本框中输入的内容进行比较,而不先使用点?
- javascript - 来自网络浏览器的 Youtube/Facebook 直播如何工作
- javascript - 经过一定时间后消失
- python - QMenu mousePressEvent 阻碍了 QAction 切换
- java - Java JNA获取桌面项目位置
- mysql - 使用 select 和 where 更新表
- azure - 是否可以通过编程方式在 Azure Bot 服务上创建 Cortana 频道
- c# - 从同步 API 迁移到异步 API
- javascript - 如何从可序列化的 JavaScript 对象中递归地修剪字符串值?