我有一个看起来像这样的数组:
[['A0' 'B0' 'C0']
['A1' 'B1' 'C1']
['A2' 'B2' 'C2']]
我想得到B1
的邻居B0 , C1 , B2 , A1
及其索引。
这是我想出的:
import numpy as np
arr = np.array([
['A0','B0','C0'],
['A1','B1','C1'],
['A2','B2','C2'],
])
def get_neighbor_indices(x,y):
neighbors = []
try:
top = arr[y - 1, x]
neighbors.append((top, (y - 1, x)))
except IndexError:
pass
try:
bottom = arr[y + 1, x]
neighbors.append((bottom, (y + 1, x)))
except IndexError:
pass
try:
left = arr[y, x - 1]
neighbors.append((left, (y, x - 1)))
except IndexError:
pass
try:
right = arr[y, x + 1]
neighbors.append((right, (y, x + 1)))
except IndexError:
pass
return neighbors
这将返回元组(value, (y, x))
的列表。
是否有更好的方法可以不依赖try / except?
答案 0 :(得分:5)
因为您知道数组的大小,所以可以直接在numpy中执行此操作,而没有任何例外。 x, y
的直接邻居的索引由
inds = np.array([[x, y]]) + np.array([[1, 0], [-1, 0], [0, 1], [0, -1]])
您可以轻松制作一个掩码,以指示哪些索引有效:
valid = (inds[:, 0] >= 0) & (inds[:, 0] < arr.shape[0]) & \
(inds[:, 1] >= 0) & (inds[:, 1] < arr.shape[1])
现在提取所需的值:
inds = inds[valid, :]
vals = arr[inds[:, 0], inds[:, 1]]
最简单的返回值为inds, vals
,但是如果您坚持保留原始格式,则可以将其转换为
[v, tuple(i) for v, i in zip(vals, inds)]
附录
您可以轻松地对此进行修改以适用于任意尺寸:
def neighbors(arr, *pos):
pos = np.array(pos).reshape(1, -1)
offset = np.zeros((2 * pos.size, pos.size), dtype=np.int)
offset[np.arange(0, offset.shape[0], 2), np.arange(offset.shape[1])] = 1
offset[np.arange(1, offset.shape[0], 2), np.arange(offset.shape[1])] = -1
inds = pos + offset
valid = np.all(inds >= 0, axis=1) & np.all(inds < arr.shape, axis=1)
inds = inds[valid, :]
vals = arr[tuple(inds.T)]
return vals, inds
给定一个N维数组arr
和N个pos
元素,您可以通过仅将每个维顺序设置为1
或-1
来创建偏移量。通过将valid
和inds
一起广播,以及在每个N大小的行上调用arr.shape
而不是为每个行手动进行,大大简化了掩码np.all
的计算尺寸。最后,通过将每一列分配给单独的维度,转换tuple(inds.T)
将inds
转换为实际的花式索引。转置是必要的,因为数组会在行上进行迭代(dim 0)。
答案 1 :(得分:1)
您可以使用此:
def get_neighbours(inds):
places = [(-1, 0), (1, 0), (0, -1), (0, 1)]
return [(arr[x, y], (y, x)) for x, y in [(inds[0] + p[0], inds[1] + p[1]) for p in places] if x >= 0 and y >= 0]
get_neighbours(1, 1)
# OUTPUT [('B0', (1, 0)), ('B2', (1, 2)), ('A1', (0, 1)), ('C1', (2, 1))]
get_neighbours(0, 0)
# OUTPUT [('A1', (0, 1)), ('B0', (1, 0))]
答案 2 :(得分:0)
怎么样?
def get_neighbor_indices(x,y):
return ( [(arr[y-1,x], (y-1, x))] if y>0 else [] ) + \
( [(arr[y+1,x], (y+1, x))] if y<arr.shape[0]-1 else [] ) + \
( [(arr[y,x-1], (y, x-1))] if x>0 else [] ) + \
( [(arr[y,x+1], (y, x+1))] if x<arr.shape[1]-1 else [] )