Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting list of Arrays to list of Lists?

After some complex operations, a resultant list is obtained, say list1, which is a list of different arrays.

Following is the list1

In [] : list1
Out [] : 
       [array([ 10.1]),
        array([ 13.26]),
        array([ 11.0 ,  12.5])]

Want to convert this list to simple list of lists and not arrays

Expected list2

      [ [ 10.1],
        [ 13.26],
        [ 11.0 ,  12.5] ]

Please let me know if anything is unclear.

like image 652
Liza Avatar asked Jul 30 '17 20:07

Liza


People also ask

Can you convert an array to a list in Python?

array ] objects can be converted to a list with the tolist() function. The tolist() function doesn't accept any arguments. If the array is one-dimensional, a list with the array elements is returned. For a multi-dimensional array, a nested list is returned.

How do I change Ndarray to list?

To convert a NumPy array (ndarray) to a Python list use ndarray. tolist() function, this doesn't take any parameters and returns a python list for an array. While converting to a list, it converts the items to the nearest compatible built-in Python type.


1 Answers

You can use tolist() in a list comprehension:

>>> [l.tolist() for l in list1]
[[0.0], [0.0], [0.0, 0.5], [0.5], [0.5], [0.5, 0.69], [0.69, 0.88], [0.88], [0.88], [0.88], [0.88, 1.0], [1.0, 1.1], [1.1], [1.1], [1.1], [1.1, 1.5], [1.5, 2.0], [2.0], [2.0]]
like image 186
Vinícius Figueiredo Avatar answered Oct 11 '22 05:10

Vinícius Figueiredo