Attempt to convert a 1 * 3 matrix to a list

I am currently getting:

y=[[ 0.16666667] [-0.16666667] [ 0.16666667]] 

This comes from the im function using, and I need to include this in the list in the following format:

 x= [0.16666667,-0.16666667,0.16666667] 

I tried list (y), but this does not work because it returns:

 [array([ 0.16666667]), array([-0.16666667]), array([ 0.16666667])] 

How exactly did I do this?

+6
source share
3 answers
 my_list = [col for row in matrix for col in row] 
+6
source

You can use the numpy .tolist() method:

 array.tolist() 

It has one more advantage ... It works with matrix objects, there is no list in understanding. If you want to remove the dimension first, you can use numpy methods like array.squeeze()

+4
source

Captures the first element from each sublist using list comprehension :

 x = [elt[0] for elt in y] 
+1
source

All Articles