简体   繁体   English

python在列表列表中找到给定节点的所有邻居

[英]python find all neighbours of a given node in a list of lists

I am trying to find a way to find all neighbours of a given node in a list of lists. 我正在尝试找到一种在列表列表中查找给定节点的所有邻居的方法。 The array looks like this: 该数组如下所示:

0,2,4,1,6,0,0
2,0,0,0,5,0,0
4,0,0,0,5,5,0
1,0,0,0,1,1,0
6,5,0,1,0,5,5
0,0,5,1,5,0,0
0,0,0,0,5,0,0

So far my code is: 到目前为止,我的代码是:

#!/usr/bin/python

#holds all source nodes
source = []

#read in and store the matrix
def create_matrix(file):
    with open('network.txt') as f:
        Alist = []
        for line in f:
            part = []
            for x in line.split(','):
                part.append(int(x))
            Alist.append(part)
    return Alist

def check_neighbours(Alist):
    i = iter(Alist)
    item = i.next()
    source.append(item)
    print source

file = ("F:/media/KINGSTON/Networking/network.txt")
Alist = create_matrix(file)
check_neighbours(Alist)

Obviously this only outputs the first row of the matrix but I am wanting something a little different. 显然,这仅输出矩阵的第一行,但是我想要一些不同的东西。 For example, I would start at the node [0,0] which is 0 and then find both [0,1] and [1,0]. 例如,我将从节点[0,0]开始,该节点为0,然后找到[0,1]和[1,0]。 But I also need to look in a 3x3 radius if I am not on the edge of the matrix. 但是,如果我不在矩阵的边缘,则还需要查看3x3的半径。 I know how to find the next neighbour to the right of the current node but I am really not sure how to find anything next to the node which includes diagonal nodes as well. 我知道如何在当前节点的右边找到下一个邻居,但是我真的不确定如何在节点附近找到任何对象,其中也包括对角节点。

You want a 8-neighbor algorithm, which is really just a selection of indices from a list of lists. 您需要一个8邻居算法,实际上是从列表列表中选择索引。

# i and j are the indices for the node whose neighbors you want to find
def find_neighbors(m, i, j, dist=1):
    return [row[max(0, j-dist):j+dist+1] for row in m[max(0, i-1):i+dist+1]]

Which can then be called by: 然后可以通过以下方式调用:

m = create_matrix(file)
i = some_y_location
j = some_x_location
neighbors = find_neighbors(m, i, j)

The implementation without list compression: 没有列表压缩的实现:

def find_neighbors(m, i, j, dist=1):
    neighbors = []
    i_min = max(0, i-dist)
    i_max = i+dist+1
    j_low = max(0, j-dist)
    j_max = j+dist+1
    for row in m[i_min:i_max]:
        neighbors.append(row[j_min:j_max])
    return neighbors

You need the max call for the i/j_min to avoid negative indices, but the upper values being too large are handled by list slices automatically. 您需要对i / j_min进行max调用,以避免出现负索引,但上限值太大会由列表切片自动处理。

If you want those row lists as a single element list you need to add: 如果要将这些行列表作为单个元素列表,则需要添加:

neighbors = [elem for nlist in neighbors for elem in nlist]

This flattens the list of lists. 这会使列表列表变平。

If you want the indicies of neighbors instead (there are probably cleaner solutions): 如果您想要邻居的索引(可能有更干净的解决方案):

def find_neighbor_indices(m, i, j, dist=1):
    irange = range(max(0, i-dist), min(len(m), i+dist+1))
    if len(m) > 0:
        jrange = range(max(0, j-dist), min(len(m[0]), j+dist+1))
    else:
        jrange = []
    for icheck in irange:
        for jcheck in jrange:
            # Skip when i==icheck and j==jcheck
            if icheck != i or jcheck != j:
                neighbors.append((icheck, jcheck))
    return neighbors

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM