python用方法concatenate()连接numpy数组

时间:2022-01-07 21:23:33

I want to concatenate numpy arrays. The error that I get is : ValueError: 0-d arrays can't be concatenated

我想连接numpy数组。我得到的错误是:ValueError:0-d数组不能连接

The code is the following:

代码如下:

n = len(names)
#print names
print n

pairs = concatenate(array([[(j,i) for j in xrange(i)] for i in xrange(1,n)]))

I just don't now how to solve this. I would be great if someone could help me.

我现在不知道如何解决这个问题。如果有人可以帮助我,我会很棒。

Cheers, M

干杯,M

2 个解决方案

#1


1  

I don't know what you want to do. So see if it is your desired output. If it is not, please comment what is your desired output.

我不知道你想做什么。所以看看它是否是你想要的输出。如果不是,请评论您想要的输出。

>>> n = 5
>>> a = array([[(j,i) for j in xrange(i)] for i in xrange(1,n)])
>>> a
array([
       [(0, 1)],
       [(0, 2), (1, 2)],
       [(0, 3), (1, 3), (2, 3)],
       [(0, 4), (1, 4), (2, 4), (3, 4)]
      ], dtype=object)

>>> import itertools
>>> it = itertools.chain(*a)
>>> list(it)
[array([0, 1]),
 array([0, 2]),
 array([1, 2]),
 array([0, 3]),
 array([1, 3]),
 array([2, 3]),
 array([0, 4]),
 array([1, 4]),
 array([2, 4]),
 array([3, 4])]

#2


0  

Let's see what you are trying to concatenate:

让我们看看你想要连接的内容:

>>> a = np.array([[(j,i) for j in xrange(i)] for i in xrange(1,n)])
>>> a
array([[(0, 1)], [(0, 2), (1, 2)]], dtype=object)
>>> a.shape
(2,)
>>> a[0]
[(0, 1)]
>>> a[1]
[(0, 2), (1, 2)]

You already have a numpy array of an object dtype. If this is what you actually want (which I doubt), just remove concatenate. Otherwise, what is that you actually trying to achieve.

你已经拥有了一个对象dtype的numpy数组。如果这是你真正想要的(我怀疑),只需删除连接。否则,你实际上想要实现的是什么。

#1


1  

I don't know what you want to do. So see if it is your desired output. If it is not, please comment what is your desired output.

我不知道你想做什么。所以看看它是否是你想要的输出。如果不是,请评论您想要的输出。

>>> n = 5
>>> a = array([[(j,i) for j in xrange(i)] for i in xrange(1,n)])
>>> a
array([
       [(0, 1)],
       [(0, 2), (1, 2)],
       [(0, 3), (1, 3), (2, 3)],
       [(0, 4), (1, 4), (2, 4), (3, 4)]
      ], dtype=object)

>>> import itertools
>>> it = itertools.chain(*a)
>>> list(it)
[array([0, 1]),
 array([0, 2]),
 array([1, 2]),
 array([0, 3]),
 array([1, 3]),
 array([2, 3]),
 array([0, 4]),
 array([1, 4]),
 array([2, 4]),
 array([3, 4])]

#2


0  

Let's see what you are trying to concatenate:

让我们看看你想要连接的内容:

>>> a = np.array([[(j,i) for j in xrange(i)] for i in xrange(1,n)])
>>> a
array([[(0, 1)], [(0, 2), (1, 2)]], dtype=object)
>>> a.shape
(2,)
>>> a[0]
[(0, 1)]
>>> a[1]
[(0, 2), (1, 2)]

You already have a numpy array of an object dtype. If this is what you actually want (which I doubt), just remove concatenate. Otherwise, what is that you actually trying to achieve.

你已经拥有了一个对象dtype的numpy数组。如果这是你真正想要的(我怀疑),只需删除连接。否则,你实际上想要实现的是什么。