python – 将numpy多维数组转换为list

我对
Python很新.我查看了其他类似的主题,但他们没有回答我想要做的事情.结果如下:

coslist[1:4]
Out[94]: [array([[ 0.7984719]]), array([[ 0.33609957]]), 0]

这就是我要的:

coslist=[0.7984719,0.33609957,0]

我试过这个:

tolist=list(coslist)
tolist[1:3]
Out[98]: [array([[ 0.7984719]]), array([[ 0.33609957]])]

还有这个:

y=np.array(val).ravel().tolist()
y[1:4]
Out[99]: [array([[ 0.7984719]]), array([[ 0.33609957]]), 0]

如你所见,他们中的任何一个都是我想要的.
任何帮助将不胜感激.

最佳答案 有一个内置的numpy数组方法.

coslist = [numpy.array([[ 0.7984719]]), numpy.array([[ 0.33609957]]), 0]
coslist = [x.tolist()[0][0] if type(x)==numpy.ndarray else x for x in coslist]

type(coslst) # Should print <type 'list'>
lst # Should print a list of lists
点赞