获取 N 维数组的所有索引作为列表
Get all indices of an N-dimensional array as a list
有没有办法在Python中快速高效地获取N维数组中所有索引的列表或数组?
例如,图像我们有以下数组:
import numpy as np
test = np.zeros((4,4))
array([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]])
我想获取所有元素索引如下:
indices = [ [0,0],[0,1],[0,2] ... [3,2],[3,3] ]
你可以试试itertools.product
:
>>> from itertools import product
>>>
>>> [list(i) for i in product(range(4), range(4))]
[[0, 0], [0, 1], [0, 2], [0, 3], [1, 0], [1, 1], [1, 2], [1, 3], [2, 0], [2, 1], [2, 2], [2, 3], [3, 0], [3, 1], [3, 2], [3, 3]]
如果您可以使用列表理解
test = np.zeros((4,4))
indices = [[i, j] for i in range(test.shape[0]) for j in range(test.shape[1])]
print (indices)
[[0, 0], [0, 1], [0, 2], [0, 3], [1, 0], [1, 1], [1, 2], [1, 3], [2, 0], [2, 1], [2, 2], [2, 3], [3, 0], [3, 1], [3, 2], [3, 3]]
我建议使用 np.ones_like
制作一个与 test
数组形状相同的 1
数组,然后使用 np.where
:
>>> np.stack(np.where(np.ones_like(test))).T
# Or np.dstack(np.where(np.ones_like(test)))
array([[0, 0],
[0, 1],
[0, 2],
[0, 3],
[1, 0],
[1, 1],
[1, 2],
[1, 3],
[2, 0],
[2, 1],
[2, 2],
[2, 3],
[3, 0],
[3, 1],
[3, 2],
[3, 3]])
只是列举应该做的:
test = [[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]]
indices = [[i, j] for i, row in enumerate(test) for j, col in enumerate(row)]
print(indices)
>>> [[0, 0], [0, 1], [0, 2], [0, 3], [1, 0], [1, 1], [1, 2], [1, 3], [2, 0], [2, 1], [2, 2], [2, 3], [3, 0], [3, 1], [3, 2], [3, 3], [4, 0], [4, 1], [4, 2], [4, 3]]
使用 np.indices
并进行一些整形:
np.indices(test.shape).reshape(2, -1).T
array([[0, 0],
[0, 1],
[0, 2],
[0, 3],
[1, 0],
[1, 1],
[1, 2],
[1, 3],
[2, 0],
[2, 1],
[2, 2],
[2, 3],
[3, 0],
[3, 1],
[3, 2],
[3, 3]])
有没有办法在Python中快速高效地获取N维数组中所有索引的列表或数组?
例如,图像我们有以下数组:
import numpy as np
test = np.zeros((4,4))
array([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]])
我想获取所有元素索引如下:
indices = [ [0,0],[0,1],[0,2] ... [3,2],[3,3] ]
你可以试试itertools.product
:
>>> from itertools import product
>>>
>>> [list(i) for i in product(range(4), range(4))]
[[0, 0], [0, 1], [0, 2], [0, 3], [1, 0], [1, 1], [1, 2], [1, 3], [2, 0], [2, 1], [2, 2], [2, 3], [3, 0], [3, 1], [3, 2], [3, 3]]
如果您可以使用列表理解
test = np.zeros((4,4))
indices = [[i, j] for i in range(test.shape[0]) for j in range(test.shape[1])]
print (indices)
[[0, 0], [0, 1], [0, 2], [0, 3], [1, 0], [1, 1], [1, 2], [1, 3], [2, 0], [2, 1], [2, 2], [2, 3], [3, 0], [3, 1], [3, 2], [3, 3]]
我建议使用 np.ones_like
制作一个与 test
数组形状相同的 1
数组,然后使用 np.where
:
>>> np.stack(np.where(np.ones_like(test))).T
# Or np.dstack(np.where(np.ones_like(test)))
array([[0, 0],
[0, 1],
[0, 2],
[0, 3],
[1, 0],
[1, 1],
[1, 2],
[1, 3],
[2, 0],
[2, 1],
[2, 2],
[2, 3],
[3, 0],
[3, 1],
[3, 2],
[3, 3]])
只是列举应该做的:
test = [[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]]
indices = [[i, j] for i, row in enumerate(test) for j, col in enumerate(row)]
print(indices)
>>> [[0, 0], [0, 1], [0, 2], [0, 3], [1, 0], [1, 1], [1, 2], [1, 3], [2, 0], [2, 1], [2, 2], [2, 3], [3, 0], [3, 1], [3, 2], [3, 3], [4, 0], [4, 1], [4, 2], [4, 3]]
使用 np.indices
并进行一些整形:
np.indices(test.shape).reshape(2, -1).T
array([[0, 0],
[0, 1],
[0, 2],
[0, 3],
[1, 0],
[1, 1],
[1, 2],
[1, 3],
[2, 0],
[2, 1],
[2, 2],
[2, 3],
[3, 0],
[3, 1],
[3, 2],
[3, 3]])