From what I understand, the recommended way to convert a NumPy array into a native Python list is to use ndarray.tolist
.
Alas, this doesn't seem to work recursively when using structured arrays. Indeed, some ndarray
objects are being referenced in the resulting list, unconverted:
>>> dtype = numpy.dtype([('position', numpy.int32, 3)])
>>> values = [([1, 2, 3],)]
>>> a = numpy.array(values, dtype=dtype)
>>> a.tolist()
[(array([1, 2, 3], dtype=int32),)]
I did write a simple function to workaround this issue:
def array_to_list(array):
if isinstance(array, numpy.ndarray):
return array_to_list(array.tolist())
elif isinstance(array, list):
return [array_to_list(item) for item in array]
elif isinstance(array, tuple):
return tuple(array_to_list(item) for item in array)
else:
return array
Which, when used, provides the expected result:
>>> array_to_list(a) == values
True
The problem with this function is that it duplicates the job of ndarray.tolist
by recreating each list/tuple that it outputs. Not optimal.
So the questions are:
- is this behaviour of
ndarray.tolist
to be expected? - is there a better way to make this happen?