[
тот самый код ]
def count_neighbours(grid, row, col): # [row] - ряд
# [col] - столбец
mass_el_1 = []
grid1 = np.array(grid)
mass8 = [(1,1),(1,2),(1,3),(2,1),(2,2),(2,3),(3,1),(3,2),(3,3)]
mass3 = [(0,4),(4,4),(4,0),(0,0)]
mass5 = [(1,0),(2,0),(3,0),(0,1),(0,2),(0,3),(1,4),(2,4),(3,4),(4,1),(4,2),(4,3)]
xl = []
yl = []
for x in range(grid1.shape[0]):
for y in range(grid1.shape[1]):
if grid1[x, y] != 0:
print (x, y)
xl.append(x)
yl.append(y)
print(xl)
print(yl)
listindex = list(zip(xl,yl))
print(listindex)
xlll = []
ylll = []
neighbours = []
exepts = []
for x in range(grid1.shape[0]):
for y in range(grid1.shape[1]):
if grid1[x, y] != 0:
for i in listindex:
print('i: ',i)
for i1 in mass8:
print('i1: ',i1)
if i == i1:
try:
if grid1[x-1, y-1] == 1:
neighbours.append((x-1, y-1))
elif grid1[x, y-1] == 1:
neighbours.append((x, y-1))
elif grid1[x+1, y-1] == 1:
neighbours.append((x+1, y-1))
elif grid1[x+1, y] == 1:
neighbours.append((x+1, y))
elif grid1[x+1, y+1] == 1:
neighbours.append((x+1, y+1))
elif grid1[x, y+1] == 1:
neighbours.append((x, y+1))
elif grid1[x-1, y+1] == 1:
neighbours.append((x-1, y+1))
elif grid1[x-1, y] == 1:
neighbours.append((x-1, y))
except IndexError :
exepts.append((x,y))
print(neighbours,"len:",len(neighbours))
print(exepts,"len:",len(exepts))
count_neighbours(((1, 0, 0, 1, 0),
(0, 1, 0, 0, 0),
(0, 0, 1, 0, 1),
(1, 0, 0, 0, 0),
(0, 0, 1, 0, 0),),0,0)