我有一個帶有一對坐標的串列:例如 coord_list = [[1,2]] 對于這個坐標,我想附加到距離 x 和 y 坐標 /- 2 點的串列坐標。該串列最后將包含 25 對坐標,以中心坐標作為第一個元素。
關于如何在 Python 中解決這個問題的任何建議?非常感謝。
uj5u.com熱心網友回復:
解決方案
我們可以從點的x和y坐標構造出圍繞該點的點的數量范圍最多為k = 2。
然后我們構建這些可能值x和y坐標值的所有可能組合。這可以通過組合兩個 for 子句使用串列運算式巧妙地完成。由于您希望將原始點放在開頭,我們添加了一個if [x, y] != point子句以將原始點從 list-expression 串列中排除,但我們將其添加到此串列理解的開頭[point] [ <list-comprehension>]。
這個解決方案的好處是你可以改變k,例如,k=3或者通過設定在原始單元格周圍只有一層單元格k=1。
def vary_around(val, k=2):
return list(range(val - k, val k 1))
def points_around(point, k=2):
x, y = point
xs = vary_around(x, k=k)
ys = vary_around(y, k=k)
return [point] [[x, y] for x in xs for y in ys if [x, y] != point]
測驗一下
你可以試試看:
In [39]: points_around([1, 2], k=2)
Out[39]:
[[1, 2],
[-1, 0],
[-1, 1],
[-1, 2],
[-1, 3],
[-1, 4],
[0, 0],
[0, 1],
[0, 2],
[0, 3],
[0, 4],
[1, 0],
[1, 1],
[1, 3],
[1, 4],
[2, 0],
[2, 1],
[2, 2],
[2, 3],
[2, 4],
[3, 0],
[3, 1],def points_around(point, k=2):
x, y = point
xs = vary_around(x, k=k)
ys = vary_around(y, k=k)
return {tuple(point): [[x, y] for x in xs for y in ys if [x, y] != point]}
# now, you get:
reduce(lambda dct, dct1: dct.update(dct1) and dct, [points_around(point, k=2) for point in coord_list])
[3, 2],
[3, 3],
[3, 4]]
In [40]: len(points_around([1, 2], k=2))
Out[40]: 25
和:
coord_list = [[1, 2], [3, 4]]
您可以使用串列推導再次應用此函式:
point_groups = [points_around(point, k=2) for point in coord_list]
進一步建議
可能您希望開始時的原始點與其余點區分開來。
為此目的使用字典怎么樣?
def merge_dicts(dct, dct1):
dct.update(dct1)
return dct
def points_around(point, k=2):
x, y = point
xs = vary_around(x, k=k)
ys = vary_around(y, k=k)
return {tuple(point): tuple([(x, y) for x in xs for y in ys if [x, y] != point])} # key must be a tuple and not a list
# in a dictionary so probably better to make everything a tuple
from functools import reduce
defun coords_to_points_dict(coord_list, k=2):
return reduce(merge_dicts, [points_around(point, k=k) for point in coord_list])
point2surrounding_points = coords_to_points_dict(coord_list, k=2)
point2surrounding_points
# it outputs:
{(1, 2): ((-1, 0),
(-1, 1),
(-1, 2),
(-1, 3),
(-1, 4),
(0, 0),
(0, 1),
(0, 2),
(0, 3),
(0, 4),
(1, 0),
(1, 1),
(1, 3),
(1, 4),
(2, 0),
(2, 1),
(2, 2),
(2, 3),
(2, 4),
(3, 0),
(3, 1),
(3, 2),
(3, 3),
(3, 4)),
(3, 4): ((1, 2),
(1, 3),
(1, 4),
(1, 5),
(1, 6),
(2, 2),
(2, 3),
(2, 4),
(2, 5),
(2, 6),
(3, 2),
(3, 3),
(3, 5),
(3, 6),
(4, 2),
(4, 3),
(4, 4),
(4, 5),
(4, 6),
(5, 2),
(5, 3),
(5, 4),
(5, 5),
(5, 6))}
這本字典你可以用你的點查詢來獲得他們周圍的點。
uj5u.com熱心網友回復:
您可以使用串列推導生成 24 個附加坐標并將它們附加到原始串列中:
coords = [[1,2]]
coords = [ [x d//5-2,y d%5-2] for x,y in coords
for d in range(25) if d != 12 ]
print(coords)
[[1, 2], [-1, 0], [-1, 1], [-1, 2], [-1, 3],
[-1, 4], [0, 0], [0, 1], [0, 2], [0, 3],
[0, 4], [1, 0], [1, 1], [1, 3], [1, 4],
[2, 0], [2, 1], [2, 2], [2, 3], [2, 4],
[3, 0], [3, 1], [3, 2], [3, 3], [3, 4]]
您可以將其概括為任何距離的函式,如下所示:
def addNeighbors(coords,distance=2):
coords = [ [x dx,y dy] for x,y in coords
for dx in range(-distance,distance 1)
for dy in range(-distance,distance 1)
if dx or dy ]
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/367457.html
