pythonarraysnumpy

Convert a pure Python list of lists to a numpy array of pure python lists (of 1 dimension)


I have a list of a certain Python object (let's call it MyClass) that can be interpreted as a multi-dimensional Numpy array. However, I'd like to convert that list to a numpy array of MyClass, and not try to convert MyClass to an inner Numpy array. Just for the sake of the question, you can use a simple list instead of MyClass:

a = [1, 2, 3]
b = [4, 5, 6]

data = [a, b]

You can achieve what I want with:

import numpy

arr = np.empty(len(data), dtype=object)
for i,v in enumerate(data):
    arr[i] = v
assert arr.shape == (len(data),) # Works

But I'm surprised as to why this doesn't work:

arr = np.array(data, dtype=object)
print(arr.shape) # prints (2 ,3), and I want (2,)

Is there a way I can limit Numpy to not delve into the inner lists and instantiate them as arrays? Why isn't dtype=object argument implies that? I really hope to avoid that i,v enumerating loop.


Solution

  • Another option is to use fromiter:

    In [13]: data
    Out[13]: [[1, 2, 3], [4, 5, 6]]
    
    In [14]: arr = np.fromiter(data, dtype=object, count=len(data))
    
    In [15]: arr
    Out[15]: array([list([1, 2, 3]), list([4, 5, 6])], dtype=object)
    
    In [16]: arr.shape
    Out[16]: (2,)