Untitled

mail@pastecode.io avatar
unknown
plain_text
3 years ago
596 B
2
Indexable
Never
def neighbors(radius, rowNumber, columnNumber):
    return [[a[i][j] if i >= 0 and i < len(a) and j >= 0 and j < len(a[0]) else 0
             for j in range(columnNumber - radius, columnNumber + radius + 1)]
            for i in range(rowNumber - radius, rowNumber + radius + 1)]


a = [[11, 21, 31, 41, 51, 61, 71],
     [12, 22, 32, 42, 52, 62, 72],
     [13, 23, 33, 43, 53, 63, 73],
     [14, 24, 34, 44, 54, 64, 74],
     [15, 25, 35, 45, 55, 65, 75],
     [16, 26, 36, 46, 56, 66, 76],
     [17, 27, 37, 47, 57, 67, 77]]

res = neighbors(1, 6, 6)
for r in res:
    print(r)