Convert 2d numpy array to list of lists

I use an external module ( libsvm ) that does not support numpy arrays, only tuples, lists, and dicts. But my data is in a 2d numpy array. How can I convert it to the Python path, aka without loops.

>>> import numpy >>> array = numpy.ones((2,4)) >>> data_list = list(array) >>> data_list [array([ 1., 1., 1., 1.]), array([ 1., 1., 1., 1.])] >>> type(data_list[0]) <type 'numpy.ndarray'> # <= what I don't want # non pythonic way using for loop >>> newdata=list() >>> for line in data_list: ... line = list(line) ... newdata.append(line) >>> type(newdata[0]) <type 'list'> # <= what I want 
+57
python arrays list numpy multidimensional-array
Mar 15 2018-12-15T00:
source share
1 answer
 >>> import numpy >>> a = numpy.ones((2,4)) >>> a array([[ 1., 1., 1., 1.], [ 1., 1., 1., 1.]]) >>> a.tolist() [[1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0]] >>> type(a.tolist()) <type 'list'> >>> type(a.tolist()[0]) <type 'list'> 
+85
Mar 15 2018-12-15T00:
source share



All Articles