Python 如何将数组转换为列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34623191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 15:16:48 来源:igfitidea点击:
How to convert array to list?
提问by kamfulebu
Examples
例子
from array:
从数组:
[ 8.85145037e+02 -1.24294207e+02 -1.42830519e+01 -8.62776672e+01
-8.66725361e+01 -7.96623643e+00 1.23582408e+01 2.84470918e+01 ]
to list:
列出:
[[885.1450370958278],[-124.29420748608811],[-14.283051875003691],[-86.27766716541419],[-86.67253605552324],[-7.966236427390702],[12.358240800052027],[28.447091751843175]]
When using tolist () the result is:
使用 tolist() 时的结果是:
[885.1450370958278, -124.29420748608811, -14.283051875003691, -86.27766716541419, -86.67253605552324, -7.966236427390702, 12.358240800052027, 28.447091751843175]
Thanks in advance
提前致谢
采纳答案by Warren Weckesser
If you really need the list to be nested like that, you can do:
如果你真的需要像这样嵌套列表,你可以这样做:
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]]
but x.tolist()
is also a list.
但x.tolist()
也是一个列表。