I want to create a 2d numpy array where every element is a tuple of its indices.
Example (4x5):
array([[[0, 0],
[0, 1],
[0, 2],
[0, 3],
[0, 4]],
[[1, 0],
[1, 1],
[1, 2],
[1, 3],
[1, 4]],
[[2, 0],
[2, 1],
[2, 2],
[2, 3],
[2, 4]],
[[3, 0],
[3, 1],
[3, 2],
[3, 3],
[3, 4]]])
I would create an python list
with the following list comprehension:
[[(y,x) for x in range(width)] for y in range(height)]
Is there a faster way to achieve the same, maybe with numpy methods?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…