簡體   English   中英

從點列表創建一個 numpy 坐標數組

[英]Create a numpy array of coordinates from a list of points

給定一個點列表,我想創建一個帶有坐標的 numpy 數組。 相關問題在這里這里 有誰知道這樣做的正確或更有效的方法嗎?

import numpy as np

# Please note that we have no control over the point class.
# This code just to generate the example.
class Point:
    x = 0.0
    y = 0.0


points = list()
for i in range(10):
    p = Point()
    p.x = 10.0
    p.y = 5.0
    points.append(p)

# The question starts here.
# The best I could come up with so far:
x = np.fromiter((p.x for p in points), float)
y = np.fromiter((p.y for p in points), float)
arr = np.vstack((x,y)).transpose()

一次同時獲取 x, y,在 listcomp 中的列表中加入,它具有您想要的結構

np.array([[e.x, e.y] for e in points])  

Out[203]: 
array([[ 10.,   5.],
       [ 10.,   5.],
       [ 10.,   5.],
       [ 10.,   5.],
       [ 10.,   5.],
       [ 10.,   5.],
       [ 10.,   5.],
       [ 10.,   5.],
       [ 10.,   5.],
       [ 10.,   5.]])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM