在for循环中创建numpy数组 [英] creating numpy arrays in a for loop
本文介绍了在for循环中创建numpy数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望能够根据可以更改的数字创建numpy数组.
例如,假设我有50个文本文件,其中包含2x2的数字集
I would like to be able to create numpy arrays based on a number that could change.
For example, say I have 50 text files containing a 2x2 set of numbers
我想将这50个文件加载为numpy数组,并在以后的代码中使用它们.代码示例可能类似于:
I would like to load those 50 files as numpy arrays and use them later in the code. A sample of code may look like:
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)
这可能吗? 谢谢
推荐答案
您可以将它们存储在列表中:
You can store them in a list:
data = list()
for i in arange(num):
data.append(np.loadtxt("datafromafile%d.txt" % i))
然后您可以使用以下命令访问每个阵列:
Then you can access each array with:
>>> data[0] + data[1] # sum of the first and second numpy array
这篇关于在for循环中创建numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文