如何将数组转换为列表?

时间:2016-01-05 23:35:25

标签: python numpy

实施例

来自数组:

[  8.85145037e+02  -1.24294207e+02  -1.42830519e+01  -8.62776672e+01
  -8.66725361e+01  -7.96623643e+00   1.23582408e+01   2.84470918e+01 ]

列出:

[[885.1450370958278],[-124.29420748608811],[-14.283051875003691],[-86.27766716541419],[-86.67253605552324],[-7.966236427390702],[12.358240800052027],[28.447091751843175]]

使用tolist()时,结果为:

 [885.1450370958278, -124.29420748608811, -14.283051875003691, -86.27766716541419, -86.67253605552324, -7.966236427390702, 12.358240800052027, 28.447091751843175]

提前致谢

1 个答案:

答案 0 :(得分:4)

如果你真的需要这样的嵌套列表,你可以这样做:

In [2]: x
Out[2]: array([ 0. ,  0.5,  1. ,  1.5,  2. ,  2.5,  3. ,  3.5,  4. ,  4.5,  5. ])

In [3]: x.reshape(-1, 1).tolist()
Out[3]: [[0.0], [0.5], [1.0], [1.5], [2.0], [2.5], [3.0], [3.5], [4.0], [4.5], [5.0]]

x.tolist()也是一个列表。