简体   繁体   English

“环绕矩阵”以获取 Python 中二维数组中单元格的邻居

[英]"Wrapping around a matrix" to get the neighbors of a cell in a 2D array in Python

Currently I am needing to grab all 8 neighbor cells of each cell in a 2D array/matrix目前我需要获取二维数组/矩阵中每个单元格的所有 8 个相邻单元格

Now, as you may know, cells at the begginings and ends of a matrix only have either 3 or 5 neighbor cells.现在,您可能知道,矩阵开头和结尾的单元格只有 3 个或 5 个相邻单元格。 However, I want to register cells from the first and last rows and columns of the matrix as neighbors of the last and first rows and columnns of the matrix.但是,我想将矩阵的第一行和最后一行和列中的单元格注册为矩阵的最后一行和第一行和列的邻居。 In a sense, I need to "wrap around" the matrix to do this.从某种意义上说,我需要“环绕”矩阵来做到这一点。

My code currently grabs all "available" neighbor cells.我的代码目前获取所有“可用”的相邻单元格。 Code is:代码是:

def getNeighbours(matrix): #function to get and store nighbor cells in a new matrix called neighbourMatrix
    m , n  = len(matrix), len(matrix[0])#generate size of neighbourMatrix from size of rows and columns of original matrix
    neighbourMatrix = [['' for j in range(n)] for i in range(m)]

    def idx_gen(y, x , m, n):#generate indeces of neighbour matrix based around which cell we are viewing in the originla matrix
        v = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1),(1, -1), (1, 0), (1, 1)]
        for val in v:
            if (0 <= y + val[0] < m) and (0 <= x + val[1] < n): 
                yield y + val[0] , x + val[1]

    for i in range(m):
        for j in range(n):#looping through matrix
            for idx in idx_gen(i, j, m, n):
                neighbourMatrix[i][j] += matrix[idx[0]][idx[1]] #initialize and store neighbor values
    return neighbourMatrix#return nighbors in matrix
       
       
#call function to get neighbouring cells and store it in a matrix called "neighbourMatrix"
neighbourMatrix = getNeighbours(matrix)
print("Neighbor matrix is:: ", neighbourMatrix)

and my output is:我的 output 是:

The starting matrix is::  [['-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-'], ['-', '-', '-', '+', '-', '-', '-', '+', '-', '-', '-', '+', '-', '-', '+', '-', '-', '-', '-', '-'], ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '+', '-', '-', '-', '-', '-', '-'], ['+', '+', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '+', '-', '-', '-', '-', '-', '-'], ['-', '+', '-', '+', '-', '-', '-', '-', '+', '-', '-', '-', '-', '-', '-', '+', '-', '-', '-', '-'], ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-'], ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '+', '-', '-', '-', '-', '-', '-', '-', '+'], ['-', '-', '-', '-', '-', '-', '+', '-', '-', '-', '-', '+', '-', '-', '-', '-', '-', '-', '-', '+'], ['+', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-'], ['-', '-', '+', '-', '-', '-', '-', '-', '-', '-', '-', '+', '-', '-', '-', '-', '-', '-', '+', '-']]

Neighbor matrix is::  [['---', '-----', '----+', '---+-', '--+--', '-----', '----+', '---+-', '--+--', '-----', '----+', '---+-', '--+--', '----+', '---+-', '--+--', '-----', '-----', '-----', '---'], ['-----', '--------', '----+---', '--------', '---+----', '--------', '----+---', '--------', '---+----', '--------', '----+---', '--------', '---+---+', '----+-+-', '-----+--', '---+----', '--------', '--------', '--------', '-----'], ['---++', '-----++-', '--+--+--', '-+------', '+-------', '--------', '--+-----', '-+------', '+-------', '--------', '--+-----', '-+------', '+---+--+', '--+---+-', '-+-+-+--', '+-------', '--------', '--------', '--------', '-----'], ['--+-+', '---+--+-', '---+-+-+', '------+-', '-----+--', '--------', '--------', '-------+', '------+-', '-----+--', '--------', '--------', '--+-+---', '-+------', '+--+---+', '------+-', '-----+--', '--------', '--------', '-----'], ['+++--', '++------', '+--++---', '--------', '---+----', '--------', '--------', '----+---', '--------', '---+----', '--------', '--------', '--+-----', '-+------', '+---+---', '--------', '---+----', '--------', '--------', '-----'], ['-+---', '-+------', '+-+-----', '-+------', '+-------', '--------', '--------', '--+-----', '-+------', '+-------', '-------+', '------+-', '-----+--', '--------', '--+-----', '-+------', '+-------', '--------', '-------+', '----+'], ['-----', '--------', '--------', '--------', '--------', '-------+', '------+-', '-----+--', '--------', '--------', '----+--+', '------+-', '---+-+--', '--------', '--------', '--------', '--------', '--------', '----+--+', '----+'], ['---+-', '-----+--', '--------', '--------', '--------', '----+---', '--------', '---+----', '--------', '--------', '--+-+---', '-+------', '+--+----', '--------', '--------', '--------', '--------', '--------', '--+-+---', '-+---'], ['-----', '---+---+', '------+-', '-----+--', '--------', '--+-----', '-+------', '+-------', '--------', '--------', '--+----+', '-+----+-', '+----+--', '--------', '--------', '--------', '--------', '-------+', '--+---+-', '-+-+-'], ['+--', '+---+', '-----', '---+-', '-----', '-----', '-----', '-----', '-----', '-----', '----+', '-----', '---+-', '-----', '-----', '-----', '-----', '----+', '-----', '--+']] 

As you can see, in my output, some cells have neighbors of only sets of 3 or 5, but I need 8.如您所见,在我的 output 中,有些单元格的邻居只有 3 组或 5 组,但我需要 8 组。

the original string that I am analyzing is as follows:我正在分析的原始字符串如下:

--------------------
---+---+---+--+-----
-------------+------
++-----------+------
-+-+----+------+----
--------------------
-----------+-------+
------+----+-------+
+-------------------
--+--------+------+-

It actually takes a relatively minor modification to your code to make this work.实际上需要对您的代码进行相对较小的修改才能使其工作。 You're talking about a modulo operation, so that's what we use:你在谈论模运算,所以这就是我们使用的:

def getNeighbours(matrix):
    h , w  = len(matrix), len(matrix[0])

    def idx_gen(y, x , w, h):
        v = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1),(1, -1), (1, 0), (1, 1)]
        for dx,dy in v:
            x0 = (x+dx+w)%w
            y0 = (y+dy+h)%h
            yield y0, x0

    neighbourMatrix = []
    for i in range(h):
        row = []
        for j in range(w):
            row.append( ''.join( matrix[y][x] for y,x in idx_gen(i, j, w, h)))
        neighbourMatrix.append(row)
    return neighbourMatrix

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

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