gpt4 book ai didi

python - 在 for 循环中创建 numpy 数组

转载 作者:太空宇宙 更新时间:2023-11-04 10:29:50 24 4
gpt4 key购买 nike

我希望能够基于一个可以改变的数字创建 numpy 数组。
例如,假设我有 50 个包含一组 2x2 数字的文本文件

我想将这 50 个文件加载为 numpy 数组,并在稍后的代码中使用它们。代码示例可能如下所示:

import load numpy as np
num = 50 #this could change based on different conditions

for i in arange(num):
data%d % i = np.loadtxt("datafromafile%d.txt" % i)

这可能吗?谢谢

最佳答案

您可以将它们存储在列表中:

data = list()
for i in arange(num):
data.append(np.loadtxt("datafromafile%d.txt" % i))

然后你可以访问每个数组:

>>> data[0] + data[1] # sum of the first and second numpy array

关于python - 在 for 循环中创建 numpy 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27455008/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com