将列表元素附加到symarray

时间:2022-10-06 07:41:52

I'm trying add some numpy arrays into a single array, my code looks like:

我正在尝试将一些numpy数组添加到一个数组中,我的代码如下所示:

m1=symarray('', 2)
for i in range(0,len(countersum)):
  if countersum[i]==1:
    m1.append(gmcounter[i])

This give error

这给出了错误

AttributeError: 'numpy.ndarray' object has no attribute 'append'

I have also tried changing append to vstack but it gives the same error

我也试过更改附加到vstack,但它给出了同样的错误

If I modify the last line to have m1=gcounter[i] it works but only selects the first element of gcounter meeting the condition, and disregards everything afterwards.

如果我修改最后一行以使m1 = gcounter [i]它可以工作,但只选择符合条件的gcounter的第一个元素,然后忽略所有内容。

Does anyone know how I can resolve this?

有谁知道我怎么解决这个问题?

I have seen the thread Append a NumPy array to a NumPy array but I am unable to declare what I need to append as a numpy array beforehand.

我已经看到线程将NumPy数组附加到NumPy数组但是我无法声明我需要事先将其作为numpy数组追加。

Many thanks

非常感谢

1 个解决方案

#1


1  

@Bakuriu is correct, you can not extend a numpy array without copying. However, depending on the application, you can just convert the numpy array to a list and manipulate it from there:

@Bakuriu是正确的,你无法在没有复制的情况下扩展numpy数组。但是,根据应用程序,您可以将numpy数组转换为列表并从那里操作它:

m1 = sympy.symarray('', 2)

m2 = list(m1)
x = sympy.symbols('x')
m2.append(x)

print m2

This gives

这给了

>>> [_0, _1, x]

#1


1  

@Bakuriu is correct, you can not extend a numpy array without copying. However, depending on the application, you can just convert the numpy array to a list and manipulate it from there:

@Bakuriu是正确的,你无法在没有复制的情况下扩展numpy数组。但是,根据应用程序,您可以将numpy数组转换为列表并从那里操作它:

m1 = sympy.symarray('', 2)

m2 = list(m1)
x = sympy.symbols('x')
m2.append(x)

print m2

This gives

这给了

>>> [_0, _1, x]