Python 将二维numpy数组转换为列表列表
我使用一个外部模块(),它不支持numpy数组,只支持元组、列表和dict。但我的数据是在2d numpy数组中。如何将其转换为pythonic方式,即没有循环Python 将二维numpy数组转换为列表列表,python,arrays,list,numpy,multidimensional-array,Python,Arrays,List,Numpy,Multidimensional Array,我使用一个外部模块(),它不支持numpy数组,只支持元组、列表和dict。但我的数据是在2d numpy数组中。如何将其转换为pythonic方式,即没有循环 >>> import numpy >>> array = numpy.ones((2,4)) >>> data_list = list(array) >>> data_list [array([ 1., 1., 1., 1.]), array([ 1., 1
>>> import numpy
>>> array = numpy.ones((2,4))
>>> data_list = list(array)
>>> data_list
[array([ 1., 1., 1., 1.]), array([ 1., 1., 1., 1.])]
>>> type(data_list[0])
<type 'numpy.ndarray'> # <= what I don't want
# non pythonic way using for loop
>>> newdata=list()
>>> for line in data_list:
... line = list(line)
... newdata.append(line)
>>> type(newdata[0])
<type 'list'> # <= what I want
导入numpy
>>>数组=numpy.one((2,4))
>>>数据列表=列表(数组)
>>>数据表
[数组([1,1,1,1.]),数组([1,1,1,1.])]
>>>类型(数据列表[0])
#您只需使用
matrix.tolist()
将矩阵强制转换为列表,证明:
>>> import numpy
>>> a = numpy.ones((2,4))
>>> a
array([[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.]])
>>> a.tolist()
[[1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0]]
>>> type(a.tolist())
<type 'list'>
>>> type(a.tolist()[0])
<type 'list'>
导入numpy
>>>a=整数((2,4))
>>>a
数组([[1,1,1,1.],
[ 1., 1., 1., 1.]])
>>>a.托利斯特()
[[1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0]]
>>>类型(a.tolist())
>>>类型(a.tolist()[0])
您可能想查看scikit learn,它包含一个LibSVM包装器,可以在本地处理Numpy数组。