List of lists in numpy array

How to convert a simple list of lists to a numpy array? Rows are separate sublists, and each row contains items in the sublist.

+151
python list numpy
Apr 27 '12 at 7:15
source share
7 answers

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) 
+160
Oct. 06 '14 at 20:47
source share
β€” -
 >>> numpy.array([[1, 2], [3, 4]]) array([[1, 2], [3, 4]]) 
+92
Apr 27 '12 at 7:17
source share

Since this is the best Google search for converting a list of lists to a Numpy array, I suggest the following, despite the question, which is 4 years old:

 >>> x = [[1, 2], [1, 2, 3], [1]] >>> y = numpy.hstack(x) >>> print(y) [1 2 1 2 3 1] 

When I first thought of doing it this way, I was very pleased with myself because it is simple. However, after picking a time with a large list of lists, it’s actually faster to do this:

 >>> y = numpy.concatenate([numpy.array(i) for i in x]) >>> print(y) [1 2 1 2 3 1] 

Please note that @Bastiaan # 1 answer does not make a single continuous list, so I added concatenate .

Anyway ... I prefer the hstack approach for its elegant use of Numpy.

+31
Feb 28 '17 at 2:31 on
source share

It's simple:

 >>> lists = [[1, 2], [3, 4]] >>> np.array(lists) array([[1, 2], [3, 4]]) 
+23
Apr 27 '12 at 7:21
source share

Again, after finding the problem of converting nested lists with N levels to an N-dimensional array, I did not find anything, so here is my way around this:

 import numpy as np new_array=np.array([[[coord for coord in xk] for xk in xj] for xj in xi], ndmin=3) #this case for N=3 
+5
Jan 18 '18 at 10:35
source share

I had a list of lists of equal length. Even then, the answer of Ignacio Vazquez-Abrams did not work for me. I got a two-dimensional NumPy array whose elements are lists. If you encounter the same problem, you can use the method below

Use numpy.vstack

 import numpy as np np_array = np.empty((0,4), dtype='float') for i in range(10) row_data = ... # get row_data as list np_array = np.vstack((np_array, np.array(row_data))) 
-one
Jul 17 '19 at 14:45
source share

Just use pandas

 list(pd.DataFrame(listofstuff).melt().values) 

this only works for a list of lists

if you have a list of lists of lists you can try something like

 lists(pd.DataFrame(listofstuff).melt().apply(pd.Series).melt().values) 
-one
Aug 16 '19 at 9:16
source share



All Articles