首页 > 解决方案 > 构造结构化数组时,元组和列表有什么区别?

问题描述

当我在试验 numpy 的结构化数组时,我注意到当我打电话时, np.array([[1, 2], [3, 4], [5, 6], [7, 8]], dtype='i, i')我得到了

[[(1, 1), (2, 2)],
 [(3, 3), (4, 4)],
 [(5, 5), (6, 6)],
 [(7, 7), (8, 8)]]

当我打电话时, np.array(([1, 2], [3, 4], [5, 6], [7, 8]), dtype='i, i')我得到

ValueError: could not assign tuple of length 4 to structure with 2 fields.

在这两种情况下,我都应该得到一个正常的[(1, 2), (3, 4), (5, 6), (7, 8)]数组。构造numpy的结构化数组时,元组和列表有什么区别?

标签: pythonpython-3.xnumpynumpy-ndarray

解决方案


In [36]: dt = np.dtype('i,i')                                                   
In [37]: dt                                                                     
Out[37]: dtype([('f0', '<i4'), ('f1', '<i4')])

使用元组列表正确创建,其中每个元组与 的大小(和类型)匹配dtype

In [38]: np.array([(1, 2), (3, 4), (5, 6), (7, 8)], dt)                         
Out[38]: 
array([(1, 2), (3, 4), (5, 6), (7, 8)],
      dtype=[('f0', '<i4'), ('f1', '<i4')])
In [39]: print(_)                                                               
[(1, 2) (3, 4) (5, 6) (7, 8)]

此列表列表创建一个匹配形状 (4,2) 的数组,并将一个值分配给两个字段:

In [40]: np.array([[1, 2], [3, 4], [5, 6], [7, 8]], dt)                         
Out[40]: 
array([[(1, 1), (2, 2)],
       [(3, 3), (4, 4)],
       [(5, 5), (6, 6)],
       [(7, 7), (8, 8)]], dtype=[('f0', '<i4'), ('f1', '<i4')])
In [41]: _.shape                                                                
Out[41]: (4, 2)

这里()解释为标记一条记录。但它有 4 个元素,而 dtype 只需要 2 个:

In [42]: np.array(([1, 2], [3, 4], [5, 6], [7, 8]), dt)                         
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-42-730c344e4f84> in <module>
----> 1 np.array(([1, 2], [3, 4], [5, 6], [7, 8]), dt)

ValueError: could not assign tuple of length 4 to structure with 2 fields.

我可以将它更改为元组中的 2 个元素,但它们是错误的类型 - 每个 2 个值而不是 1 个:

In [43]: np.array(([1, 2], [3, 4]), dt)                                         
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'

The above exception was the direct cause of the following exception:

ValueError                                Traceback (most recent call last)
<ipython-input-43-976803c7a6c9> in <module>
----> 1 np.array(([1, 2], [3, 4]), dt)

ValueError: setting an array element with a sequence.

元组确实有效的情况 - 制作 0d 结构化数组(1 个元素):

In [44]: np.array((1,2), dt)                                                    
Out[44]: array((1, 2), dtype=[('f0', '<i4'), ('f1', '<i4')])

[43] 可以使用不同的dtype,每个字段需要两个值:

In [46]: np.array(([1, 2], [3, 4]), [('f0','i',2),('f1','f',2)])                
Out[46]: array(([1, 2], [3., 4.]), dtype=[('f0', '<i4', (2,)), ('f1', '<f4', (2,))])

推荐阅读