问题:将numpy数组转换为元组

注意:这要求与通常的元组到数组的转换相反。

我必须将一个参数传递给(包装的c ++)函数作为嵌套元组。例如,以下作品

X = MyFunction( ((2,2),(2,-2)) )

而以下

X = MyFunction( numpy.array(((2,2),(2,-2))) )
X = MyFunction( [[2,2],[2,-2]] )

不幸的是,我想使用的参数是一个numpy数组。对于某些N,该阵列的尺寸始终为2xN,这可能会很大。

有没有简单的方法可以将其转换为元组?我知道我可以循环遍历,创建一个新的元组,但是更喜欢numpy数组提供的一些访问。

如果不可能如我所愿地做到这一点,那么通过循环执行此操作的最漂亮的方法是什么?

Note: This is asking for the reverse of the usual tuple-to-array conversion.

I have to pass an argument to a (wrapped c++) function as a nested tuple. For example, the following works

X = MyFunction( ((2,2),(2,-2)) )

whereas the following do not

X = MyFunction( numpy.array(((2,2),(2,-2))) )
X = MyFunction( [[2,2],[2,-2]] )

Unfortunately, the argument I would like to use comes to me as a numpy array. That array always has dimensions 2xN for some N, which may be quite large.

Is there an easy way to convert that to a tuple? I know that I could just loop through, creating a new tuple, but would prefer if there’s some nice access the numpy array provides.

If it’s not possible to do this as nicely as I hope, what’s the prettiest way to do it by looping, or whatever?


回答 0

>>> arr = numpy.array(((2,2),(2,-2)))
>>> tuple(map(tuple, arr))
((2, 2), (2, -2))
>>> arr = numpy.array(((2,2),(2,-2)))
>>> tuple(map(tuple, arr))
((2, 2), (2, -2))

回答 1

这是一个可以完成的功能:

def totuple(a):
    try:
        return tuple(totuple(i) for i in a)
    except TypeError:
        return a

还有一个例子:

>>> array = numpy.array(((2,2),(2,-2)))
>>> totuple(array)
((2, 2), (2, -2))

Here’s a function that’ll do it:

def totuple(a):
    try:
        return tuple(totuple(i) for i in a)
    except TypeError:
        return a

And an example:

>>> array = numpy.array(((2,2),(2,-2)))
>>> totuple(array)
((2, 2), (2, -2))

回答 2

我不满意,所以我终于使用了:

>>> a=numpy.array([[1,2,3],[4,5,6]])
>>> a
array([[1, 2, 3],
       [4, 5, 6]])

>>> tuple(a.reshape(1, -1)[0])
(1, 2, 3, 4, 5, 6)

我不知道它是否更快,但看起来更有效;)

I was not satisfied, so I finally used this:

>>> a=numpy.array([[1,2,3],[4,5,6]])
>>> a
array([[1, 2, 3],
       [4, 5, 6]])

>>> tuple(a.reshape(1, -1)[0])
(1, 2, 3, 4, 5, 6)

I don’t know if it’s quicker, but it looks more effective ;)


回答 3

另外的选择

tuple([tuple(row) for row in myarray])

如果要将NumPy数组传递给C ++函数,则还可能希望使用Cython或SWIG。

Another option

tuple([tuple(row) for row in myarray])

If you are passing NumPy arrays to C++ functions, you may also wish to look at using Cython or SWIG.


回答 4

如果您喜欢大刀阔斧,这是另一种方法tuple(tuple(tuple(a_m.tolist())表示a_m中的a)

from numpy import array
a = array([[1, 2],
           [3, 4]])
tuple(tuple(a_m.tolist()) for a_m in a )

输出为((1,2),(3,4))

注意只是(a中a_m的tuple(a_m.tolist())会给出一个生成器表达。受到@ norok2对Greg von Winckel的回答的启发

If you like long cuts, here is another way tuple(tuple(a_m.tolist()) for a_m in a )

from numpy import array
a = array([[1, 2],
           [3, 4]])
tuple(tuple(a_m.tolist()) for a_m in a )

The output is ((1, 2), (3, 4))

Note just (tuple(a_m.tolist()) for a_m in a ) will give a generator expresssion. Sort of inspired by @norok2’s comment to Greg von Winckel’s answer


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。