Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to converting a matrix 1*3 into a list

Tags:

python

numpy

I am currently getting:

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

This comes out of a function im using and i need to turn the above into a list in the format below:

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 would I do this??

like image 741
user1819717 Avatar asked Dec 19 '12 02:12

user1819717


2 Answers

my_list = [col for row in matrix for col in row]
like image 92
Kenan Banks Avatar answered Sep 29 '22 01:09

Kenan Banks


You can use the numpy .tolist() method:

array.tolist()

There is also one more advantage to it... It works with matrix objects, the list comprehension doesn't. If you want to remove the dimension first you can use numpy methods to do so, such as array.squeeze()

like image 43
seberg Avatar answered Sep 29 '22 03:09

seberg