簡體   English   中英

“環繞矩陣”以獲取 Python 中二維數組中單元格的鄰居

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

目前我需要獲取二維數組/矩陣中每個單元格的所有 8 個相鄰單元格

現在,您可能知道,矩陣開頭和結尾的單元格只有 3 個或 5 個相鄰單元格。 但是,我想將矩陣的第一行和最后一行和列中的單元格注冊為矩陣的最后一行和第一行和列的鄰居。 從某種意義上說,我需要“環繞”矩陣來做到這一點。

我的代碼目前獲取所有“可用”的相鄰單元格。 代碼是:

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)

我的 output 是:

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

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

如您所見,在我的 output 中,有些單元格的鄰居只有 3 組或 5 組,但我需要 8 組。

我正在分析的原始字符串如下:

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

實際上需要對您的代碼進行相對較小的修改才能使其工作。 你在談論模運算,所以這就是我們使用的:

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