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

Viewed 205

while i was experimenting with numpy's structured arrays i noticed that when i call np.array([[1, 2], [3, 4], [5, 6], [7, 8]], dtype='i, i') i get

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

and when i call np.array(([1, 2], [3, 4], [5, 6], [7, 8]), dtype='i, i') i get

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

When in both cases i should've gotten a normal [(1, 2), (3, 4), (5, 6), (7, 8)] array. What is the difference between tuples and lists when constructing numpy's structured arrays?

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

The proper creation with a list of tuples, where each tuple matches the size (and type) of the 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)]

This list of lists, makes a array of matching shape (4,2), and assigns the one value to both fields:

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)

Here the () is interpreted to mark one record. But it has 4 elements, while the dtype expects only 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.

I can change it to 2 elements in tuple, but they are the wrong type - 2 values each instead of 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.

A case where the tuple does work - making a 0d structured array (1 element):

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

[43] would work with a different dtype, one that expects two values per field:

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