簡體   English   中英

python 某個點相鄰的點數

[英]python number of points a certain point is adjacent to

我有一個坐標列表,例如[[1, 0], [1, 1], [0, 1]] ,對於每個點,我想找到該點水平或垂直相鄰的點數,然后打印出該號碼。 因此,對於示例,我的編程將打印:

1 #1, 0 is vertically adjacent to 1, 1
2 #1, 1 is vertically adjacent to 1, 0 and horizontally adjacent to 0, 1
1 #0, 1 is horizontally adjacent to 1, 1

謝謝你的幫助!

PS有沒有辦法可以在O(N)中完成?

另一個解決方案可以解決您的確切問題,但點可以具有任意值的更通用解決方案是:

def adjacent(a, b):
    '''
    Returns True if a and b are adjacent points
    '''
    if a[0] == b[0] and abs(a[1] - b[1]) == 1:
        return True 
    elif a[1] == b[1] and abs(a[0] - b[0]) == 1:
        return True
    else:
        return None 
coords = [[1, 0], [1, 1], [0, 1]] 
coords = [tuple(point) for point in coords]
result = {tuple(coord): [point for point in coords if adjacent(coord, point) and coord != point] for coord in coords}
print(result)
# {(1, 0): [(1, 1)], (1, 1): [(1, 0), (0, 1)], (0, 1): [(1, 1)]}

雖然這省略了寫is horizontally adjacent tois vertically adjacent to過程,但它提供了一個字典,將每個坐標映射到所有相鄰對的列表。

我們可以重寫它以省略重復計算:

def adjacent(a, b):
    '''
    Returns True if a and b are adjacent points
    '''
    if a[0] == b[0] and abs(a[1] - b[1]) == 1:
        return True 
    elif a[1] == b[1] and abs(a[0] - b[0]) == 1:
        return True
    else:
        return None 
coords = [[1, 0], [1, 1], [0, 1]] 
coords = [tuple(point) for point in coords]
result = {coords[i]: [point for point in coords[i+1:] if adjacent(coords[i], point)] for i in range(len(coords)- 1)}
print(result)
# {(1, 0): [(1, 1)], (1, 1): [(0, 1)]}

請注意,這是我們可以實現的絕對最小比較次數:我們必須至少將第一個元素與下一個 n-1 進行比較,然后將第二個元素與下一個 n-2 進行比較,然后將第三個元素與下一個 n 進行比較-3, ... 然后最后一個元素到最后一個 0(我們這里不做比較)。 在這種情況下,您正在進行 0 + 1 + 2 +... n-2 + n-1 比較。 這等於 (1/2) * (n-1) * n,簡稱為 O(n^2),所以不,這不能在 O(n) 中完成。

可以在 O(N) 中計算如下。

代碼

def neighbors(point, set_pts = set((x,y) for x, y in data)):
    '''
        Computes neighbors of point in O(1) time
        Default argument set_pts is O(N) be is calculated only once i.e.
           the first time the function is read by the Pyton interpreter
    '''
    # Starting Message
    x, y = point
    start = f'#{x}, {y} is '
    
    # Check horizontal and vertical neighbor of point O(1)
    adjacents = []
    if y == 0 and (x, 1) in set_pts:
        adjacents.append(f' is vertically  adjacent to {x}, {1}')
    if y == 1 and (x, 0) in set_pts:
        adjacents.append(f' is vertically  adjacent to {x}, {0}')
    if x == 0 and (1, y) in set_pts:
        adjacents.append(f' is horizontally adjacent to {1}, {y}')
    if x == 1 and (0, y) in set_pts:
        adjacents.append(f' is horizontally adjacent to {0}, {y}')
        
    return start + ' and '.join(adjacents)

for pt in data:
    # N Points in loop, neigbors call takes O(1) so O(N) loop overall
    print(neighbors(pt))

Output

#1, 0 is  is vertically  adjacent to 1, 1
#1, 1 is  is vertically  adjacent to 1, 0 and  is horizontally adjacent to 0, 1
#0, 1 is  is horizontally adjacent to 1, 1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM