简体   繁体   English

运算符+将一个元组添加到另一个存储在元组多维数组中的元组中

[英]Operator + to add a tuple to another tuple stored inside a multidimensional array of tuples

I recently found out how to use tuples thanks to great contributions from SO users(see here ). 由于SO用户的巨大贡献,我最近发现了如何使用元组(请参见此处 )。 However I encounter the problem that I can't add a tuple to another tuple stored inside an array of tuples. 但是,我遇到了无法将元组添加到存储在元组数组中的另一个元组的问题。 For instance if I define: 例如,如果我定义:

arrtup=empty((2,2),dtype=('int,int'))
arrtup[0,1]=(3,4)

Then if I try to add another tuple to the existing tupe to come up with a multidimensional index: 然后,如果我尝试向现有的元组添加另一个元组以提出多维索引:

arrtup[0,1]+(4,4)

I obtain this error: 我收到此错误:

TypeError: unsupported operand type(s) for +: 'numpy.void' and 'tuple'

Instead of the expected (3,4,4,4) tuple, which I can obtain by: 代替预期的(3,4,4,4)元组,我可以通过以下方式获得:

(3,4)+(4,4)

Any ideas? 有任何想法吗? Thanks! 谢谢!

You are mixing different concepts, I'm afraid. 恐怕您正在混合使用不同的概念。

Your arrtup array is not an array of tuples, it's a structured ndarray , that is, an array of elements that look like tuples but in fact are records ( numpy.void objects, to be exact). 您的arrtup数组不是元组的数组,而是结构化的ndarray ,即看起来像元组但实际上是记录的元素数组( numpy.voidnumpy.void对象)。 In your case, you defined these records to consist in 2 integers. 在您的情况下,您将这些记录定义为由2个整数组成。 Internally, NumPy creates your array as a 2x2 array of blocks, each block taking a given space defined by your dtype : here, a block consists of 2 consecutive blocks of size int (that is, each sub-block takes the space an int takes on your machine). 在内部,NumPy将您的数组创建为2x2的块数组,每个块占用dtype定义的给定空间:在这里,一个块由2个大小为int连续块组成(也就是说,每个子块都占用int占用的空间)在您的计算机上)。

When you retrieve an element with arrtup[0,1] , you get the corresponding block. 当使用arrtup[0,1]检索元素时,将获得相应的块。 Because this block is structured as two-subblocks, NumPy returns a numpy.void (the generic object representing structured blocks), which has the same dtype as your array. 由于此块被构造为两个子块,因此NumPy返回numpy.void (代表结构化块的通用对象),其dtype与数组相同。

Because you set the size of those blocks at the creation of the array, you're no longer able to modify it. 因为您在创建数组时设置了这些块的大小,所以您将无法再对其进行修改。 That means that you cannot transform your 2-int records into 4-int ones as you want. 这意味着您无法将2 int记录转换为4 int记录。

However, you can transform you structured array into an array of objects : 但是,您可以将结构化数组转换为对象数组:

new_arr = arrtup.astype(object)

Lo and behold, your elements are no longer np.void but tuples, that you can modify as you want: 瞧,您的元素不再是np.void而是元组,可以根据需要进行修改:

new_arr[0,1] = (3,4) # That's a tuple
new_arr[0,1] += (4,4) # Adding another tuple to the element

Your new_arr is a different beast from your arrtup : it has the same size, true, but it's no longer a structured array, it's an array of objects, as illustrated by new_arr是从一个不同的野兽arrtup :它具有相同的尺寸,真实的,但它不再是一个结构化的阵列,它是对象的数组,通过如图所示

>>> new_arr.dtype
dtype("object")

In practice, the memory layout is quite different between arrtup and newarr . 实际上, arrtupnewarr之间的内存布局是完全不同的。 newarr doesn't have the same constraints as arrtup , as the individual elements can have different sizes, but object arrays are not as efficient as structured arrays. newarr没有与arrtup相同的约束,因为各个元素可以具有不同的大小,但是对象数组的效率不如结构化数组。

The traceback is pretty clear here. 追溯在这里很清楚。 arrtup[0,1] is not a tuple. arrtup[0,1]不是一个元组。 It's of type `numpy.void'. 类型为“ numpy.void”。

You can convert it to a tuple quite easily however: 您可以很容易地将其转换为元组:

tuple(arrtup[0,1])

which can be concatenated with other tuples: 可以与其他元组串联:

tuple(arrtup[0,1]) + (4,4)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM