No numpy
Edit
I think this is the best solution because it interrupts the inner loop as soon as the neighbor is zero
def zero_one(input2d, r, c):
for rr in (r-1, r, r+1):
for cc in (c-1, c, c+1):
if input2d[rr][cc] == 0 : return 1
return 0
Boundaries = [[0,0,0,0,0],
[0,1,1,1,0],
[0,1,1,1,1],
[0,1,1,1,0],
[0,0,1,0,0]]
rows = 5
cols = 5
Output = []
for r in range(rows):
Output.append([])
for c in range(cols):
if (r==0 or r==rows-1) or (c==0 or c==cols-1):
Output[r].append(Boundaries[r][c])
elif Boundaries[r][c] == 0:
Output[r].append(0)
else:
Output[r].append(zero_one(Boundaries, r, c))
for line in Output:
print line
execution of the above code gives
[0, 0, 0, 0, 0]
[0, 1, 1, 1, 0]
[0, 1, 0, 1, 1]
[0, 1, 1, 1, 0]
[0, 0, 1, 0, 0]
My previous code after
End of editing
In [15]: Boundaries = [
[0,0,0,0,0],
[0,1,1,1,0],
[0,1,1,1,1],
[0,1,1,1,0],
[0,0,1,0,0]]
In [16]: Output = [
[0,0,0,0,0],
[0,1,1,1,0],
[0,1,1,1,1],
[0,1,1,1,0],
[0,0,1,0,0]]
In [17]: for i in (1, 2, 3):
for j in (1,2,3):
s = 0
for m in (i-1, i, i+1):
for n in (j-1, j, j+1):
s = s+Boundaries[n][m]
if s == 9 : Output[j][i] = 0
....:
In [18]: Output
Out[18]:
[[0, 0, 0, 0, 0],
[0, 1, 1, 1, 0],
[0, 1, 0, 1, 1],
[0, 1, 1, 1, 0],
[0, 0, 1, 0, 0]]
In [19]: