如何将元素追加到numpy数组 [英] How to append elements to a numpy array
本文介绍了如何将元素追加到numpy数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想等效于在Numpy中递归地在python列表中添加元素,如以下代码所示
I want to do the equivalent to adding elements in a python list recursively in Numpy, As in the following code
matrix = open('workfile', 'w')
A = []
for row in matrix:
A.append(row)
print A
我尝试了以下操作:
matrix = open('workfile', 'w')
A = np.array([])
for row in matrix:
A = numpy.append(row)
print A
它不返回所需的输出,如列表中所示.
It does not return the desired output, as in the list.
编辑此为示例代码:
mat = scipy.io.loadmat('file.mat')
var1 = mat['data1']
A = np.array([])
for row in var1:
np.append(A, row)
print A
这只是我想做的最简单的情况,但是循环中有更多的数据处理,我这样写,这样例子很清楚.
This is just the simplest case of what I want to do, but there is more data processing in the loop, I am putting it this way so the example is clear.
推荐答案
您需要将数组A传递给Numpy.
You need to pass the array, A, to Numpy.
matrix = open('workfile', 'w')
A = np.array([])
for row in matrix:
A = numpy.append(A, row)
print A
但是,直接从文件加载可能是一个更好的解决方案.
However, loading from the files directly is probably a nicer solution.
这篇关于如何将元素追加到numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文