Create a structured array from a flat list

Suppose I have a flat list Las follows:

In [97]: L
Out[97]: [2010.5, 1, 2, 3, 4, 5]

... and I want to get such a structured array:

array((2010.5, [1, 2, 3, 4, 5]),
      dtype=[('A', '<f4'), ('B', '<u4', (5,))])

How can I do this conversion most efficiently? I cannot pass the last dtype directly to array(L, ...)or to array(tuple(L)):

In [98]: dtp                                                                                                                                                                                 [9/1451]
Out[98]: [('A', '<f4', 1), ('B', '<u4', 5)]

In [99]: array(L, dtp)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-99-32809e0456a7> in <module>()
----> 1 array(L, dtp)

TypeError: expected an object with a buffer interface
In [101]: array(tuple(L), dtp)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-101-4d0c49a9f01d> in <module>()
----> 1 array(tuple(L), dtp)

ValueError: size of tuple must match number of fields.

What needs to be done to pass in a temporary dtype where each field has one entry and then view it with the dtype that I really want:

In [102]: tdtp
Out[102]: 
[('a', numpy.float32),
 ('b', numpy.uint32),
 ('c', numpy.uint32),
 ('d', numpy.uint32),
 ('e', numpy.uint32),
 ('f', numpy.uint32)]

In [103]: array(tuple(L), tdtp).view(dtp)
Out[103]: 
array((2010.5, [1, 2, 3, 4, 5]), 
      dtype=[('A', '<f4'), ('B', '<u4', (5,))])

But creating this temporary dtype is an extra step that I would like to avoid if possible.

Is it possible to go directly from my flat list to my structured dtype type without using the intermediate dtype shown above?

(: , , , , . )

+4
1

tuple(L) array, , dtype. , , (L[0], L[1:]):

In [28]: L
Out[28]: [2010.5, 1, 2, 3, 4, 5]

In [29]: dtp
Out[29]: [('A', '<f4', 1), ('B', '<u4', 5)]

In [30]: array((L[0], L[1:]), dtype=dtp)
Out[30]: 
array((2010.5, [1L, 2L, 3L, 4L, 5L]), 
      dtype=[('A', '<f4'), ('B', '<u4', (5,))])
+4

Source: https://habr.com/ru/post/1530663/


All Articles