Numpy 项目 m X 坐标数组和 n Y 坐标数组到 mxn (X,Y) 坐标列表
Numpy project array of m X coords and array of n Y coords into mxn list of (X,Y) coords
实际上我想要两个嵌套的 for 循环遍历 xs 和 ys,其中每个 x 都与每个 y 配对:
xs = [0,1,2]
ys = [2,3]
desiredResult = [[0,2],[1,2],[2,2],[0,3],[1,3],[2,3]]
想知道是否有一个很好的 numpy 解决方案?
Python 的理解对于这一个可能已经足够好了:
coords = [(x,y) for x in xs for y in ys]
itertools.product
对您的情况很有用:
from itertools import product
xs = [0,1,2]
ys = [2,3]
print(list(product(xs, ys)))
输出
[(0, 2), (0, 3), (1, 2), (1, 3), (2, 2), (2, 3)]
如果您只对获取列表而不是元组感兴趣,可以尝试以下代码:
[list(x) for x in product(xs, ys)]
输出
[[0, 2], [0, 3], [1, 2], [1, 3], [2, 2], [2, 3]]
实际上我想要两个嵌套的 for 循环遍历 xs 和 ys,其中每个 x 都与每个 y 配对:
xs = [0,1,2]
ys = [2,3]
desiredResult = [[0,2],[1,2],[2,2],[0,3],[1,3],[2,3]]
想知道是否有一个很好的 numpy 解决方案?
Python 的理解对于这一个可能已经足够好了:
coords = [(x,y) for x in xs for y in ys]
itertools.product
对您的情况很有用:
from itertools import product
xs = [0,1,2]
ys = [2,3]
print(list(product(xs, ys)))
输出
[(0, 2), (0, 3), (1, 2), (1, 3), (2, 2), (2, 3)]
如果您只对获取列表而不是元组感兴趣,可以尝试以下代码:
[list(x) for x in product(xs, ys)]
输出
[[0, 2], [0, 3], [1, 2], [1, 3], [2, 2], [2, 3]]