If your list of lists contains lists with a different number of elements, then the answer of Ignacio Vazquez-Abrams will not work. Instead, there are at least 3 options:
1) Create an array of arrays:
x=[[1,2],[1,2,3],[1]] y=numpy.array([numpy.array(xi) for xi in x]) type(y) >>><type 'numpy.ndarray'> type(y[0]) >>><type 'numpy.ndarray'>
2) Create an array of lists:
x=[[1,2],[1,2,3],[1]] y=numpy.array(x) type(y) >>><type 'numpy.ndarray'> type(y[0]) >>><type 'list'>
3) First make the lists equal in length:
x=[[1,2],[1,2,3],[1]] length = max(map(len, x)) y=numpy.array([xi+[None]*(length-len(xi)) for xi in x]) y >>>array([[1, 2, None], >>> [1, 2, 3], >>> [1, None, None]], dtype=object)
Bastiaan Oct. 06 '14 at 20:47 2014-10-06 20:47
source share