简体   繁体   English

尝试将矩阵1 * 3转换为列表

[英]Trying to converting a matrix 1*3 into a list

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: 我尝试了列表(y),但这不起作用,因为它返回:

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

How exactly would I do this?? 我到底怎么做?

my_list = [col for row in matrix for col in row]

You can use the numpy .tolist() method: 您可以使用numpy .tolist()方法:

array.tolist()

There is also one more advantage to it... It works with matrix objects, the list comprehension doesn't. 它还有一个优点......它适用于matrix对象,列表理解不适用。 If you want to remove the dimension first you can use numpy methods to do so, such as array.squeeze() 如果要首先删除维度,可以使用numpy方法执行此操作,例如array.squeeze()

使用列表推导从每个子列表中获取第一个元素:

x = [elt[0] for elt in y]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM