6

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??

4

3 回答 3

6
my_list = [col for row in matrix for col in row]
于 2012-12-19T02:56:28.700 回答
4

您可以使用 numpy.tolist()方法:

array.tolist()

它还有一个优点......它适用于matrix对象,列表推导不适用。如果要先删除维度,可以使用 numpy 方法来执行此操作,例如array.squeeze()

于 2012-12-19T11:21:47.710 回答
1

Grabs the first element from each sublist, using a list comprehension:

x = [elt[0] for elt in y]
于 2012-12-19T02:57:03.773 回答