如何将numpy 2D数组重新放入python列表

时间:2019-03-28 06:42:31

标签: python numpy

[array([868., 905.]), array([827., 905.]), array([785., 905.]), array([743., 905.]), array([701., 905.]), array([659., 905.]), array([617., 905.]), array([575., 905.]), array([533., 905.]), array([491., 905.]), array([449., 905.]), array([407., 905.]), array([365., 905.]), array([323., 905.]), array([281., 905.]), array([239., 905.]), array([197., 905.]), array([155., 905.])]

我需要一种方法可以将其转换为列表。我正在尝试使用该信息运行此代码:

values= #a is the array in the code above
keys = a
dictionary = dict(zip(keys, values))
#print(dictionary)

a=[]
for i in range(101,173):
    cv=str(i)
    a.append(cv)

print(a)

1 个答案:

答案 0 :(得分:3)

您可以执行简单的列表理解来完成此任务。

from numpy import array

a = [array([868., 905.]), array([827., 905.]), array([785., 905.]), array([743., 905.]), array([701., 905.]), array([659., 905.]), array([617., 905.]), array([575., 905.]), array([533., 905.]), array([491., 905.]), array([449., 905.]), array([407., 905.]), array([365., 905.]), array([323., 905.]), array([281., 905.]), array([239., 905.]), array([197., 905.]), array([155., 905.])]

l = [list(x) for x in a] # alternaltiely, l = [x.tolist() for x in a]

print(l)

# output
[[868.0, 905.0],
 [827.0, 905.0],
 [785.0, 905.0],
 [743.0, 905.0],
 [701.0, 905.0],
 [659.0, 905.0],
 [617.0, 905.0],
 [575.0, 905.0],
 [533.0, 905.0],
 [491.0, 905.0],
 [449.0, 905.0],
 [407.0, 905.0],
 [365.0, 905.0],
 [323.0, 905.0],
 [281.0, 905.0],
 [239.0, 905.0],
 [197.0, 905.0],
 [155.0, 905.0]]