簡體   English   中英

構造結構化數組時,元組和列表有什么區別?

[英]What is the difference between tuples and lists when constructing a structured array?

當我嘗試使用 numpy 的結構化 arrays 時,我注意到當我調用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的結構化arrays時元組和列表有什么區別?

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,))])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM