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

标签 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)

最佳答案

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

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]]

关于python - 如何将 numpy 2D 数组重新放入 python 列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55391587/

相关文章:

python - 区域提案标准化的最快算法

python - 继承 ElementTree 解析器以保留注释

python - 使用 Python Selenium 在输入字段中键入时出错

python - 使用 NumPy 广播的 View

python - 我怎样才能确保首先放置更大的数字?

python - 如何对包含 NaN 的大型多维数组中的每个像素应用线性回归?

python - 在Python中匹配以特定扩展名结尾的文件名的简洁方法?

python - 使用 Python 和 PLY 的递归下降解析器

python - 网页抓取 | BeautifulSoup |解析表

python - 可见弃用警告 : using a non-integer number instead of an integer will result in an error in the future