创建在每个方向上偏移1的坐标列表

  • 本文关键字:坐标 列表 方向 创建 python
  • 更新时间 :
  • 英文 :


我在位置(x,y,z(有一个对象,我想要它的相邻坐标列表。相邻的对象6在每个方向上都位于正1或负1处。我的坐标列表看起来像:

[[x+1, y, z], [x-1, y, z], [x, y+1, z], [x, y-1, z], [x, y, z+1], [x, y, z-1]]

在python中有什么聪明(巧妙(的方法可以做到这一点?

这可能不是最漂亮的方法,但如果重复此操作的函数需要它,那么它应该很好。

from copy import copy
point = [1,4,7]
neighbours = list()
for dim in range(len(point)):
for shift in range(-1,2,2):
neighbour = copy(point)
neighbour[dim] += shift
neighbours.append(neighbour)

我尝试使用numpy:

import numpy as np
x=2.3
y=-4.5
z=1.9
my_points = np.repeat(np.array([[x,y,z]]),6,axis=0)
res = my_points + np.vstack((np.eye(3),-np.eye(3)))
print(res)

输出:

[[ 3.3 -4.5  1.9]
[ 2.3 -3.5  1.9]
[ 2.3 -4.5  2.9]
[ 1.3 -4.5  1.9]
[ 2.3 -5.5  1.9]
[ 2.3 -4.5  0.9]]

您可以使用res.tolist()来获取列表。

输出:

[[3.3, -4.5, 1.9], [2.3, -3.5, 1.9], [2.3, -4.5, 2.9], [1.2999999999999998, -4.5, 1.9], [2.3, -5.5, 1.9], [2.3, -4.5, 0.8999999999999999]]

最新更新