title: Is there a more efficient way to solve this?
body: <p>Consider the following N × N matrices <strong>A</strong>
  and <strong>B</strong> that contain only positive integers:</p>
  
  <ul>
  <li><strong>B</strong> is created from <strong>A</strong> by summing the odd numbers that
  neighbour each element, at position <code>[i,j]</code> in <strong>A</strong>. </li>
  <li>Each computed sum is then stored in the corresponding position <code>[i,j]</code> in <strong>B</strong>.</li>
  </ul>
  
  <p>For example:</p>
  
  <ul>
  <li>At position <code>[3, 2]</code> there are <code>4</code> odd numbers <code>[3, 1, 5, 3]</code> that
  neighbour <code>A[3,2]</code>.  </li>
  <li>The sum of these neighbouring odd numbers is <code>12</code>,
  which is stored in <code>B[3,2]</code></li>
  </ul>
  
  <p>This code works by brute force, taking each of the 9 regions separately, (Namely: Top, bottom, left, right, 4 corners and inner).</p>
  
  <p>Is there a better way to go about this?</p>
  
  <pre><code>from numpy import array,zeros,shape
  
  def sum_neighbours(A):
      A = array(A,int)
      r,c = shape(A)
      B = zeros(shape(A),int)
  
      for i in range(r):
          for j in range(c):
              N_list_i = [0,-1,-1,-1,0,1,1,1,0] #[l,tl,t,tr,r,br,b,bl]
              N_list_j = [-1,-1,0,1,1,1,0,-1,-1]          
  
              # innerSquares
              if 1 &lt;= i &lt;= (r-2) and 1 &lt;= j &lt;= (r-2):
                  for k in range(len(N_list_j)-1):
                      if A[i+N_list_i[k]][j+N_list_j[k]] %2 != 0:
                          B[i][j] += A[i+N_list_i[k]][j+N_list_j[k]]
  
              #TopLeft            
              elif i==0 and j==0:
                  N_list_i = N_list_i[4:(4+3)]
                  N_list_j = N_list_j[4:(4+3)]
                  for k in range(len(N_list_j)):
                      if A[i+N_list_i[k]][j+N_list_j[k]] %2 != 0:
                          B[i][j] += A[i+N_list_i[k]][j+N_list_j[k]]
  
              #BottomLeft            
              elif j==0 and i==(r-1):
                  N_list_i = N_list_i[2:(2+3)]
                  N_list_j = N_list_j[2:(2+3)]
                  for k in range(len(N_list_j)):
                      if A[i+N_list_i[k]][j+N_list_j[k]] %2 != 0:
                          B[i][j] += A[i+N_list_i[k]][j+N_list_j[k]]
  
              #TopRight            
              elif i==0 and j == (r-1):
                  N_list_i = N_list_i[6:(6+3)]
                  N_list_j = N_list_j[6:(6+3)]
                  for k in range(len(N_list_j)):
                      if A[i+N_list_i[k]][j+N_list_j[k]] %2 != 0:
                          B[i][j] += A[i+N_list_i[k]][j+N_list_j[k]]
  
              #BottomRight            
              elif i == (r-1) and j == (r-1):
                  N_list_i = N_list_i[0:(0+3)]
                  N_list_j = N_list_j[0:(0+3)]
                  for k in range(len(N_list_j)):
                      if A[i+N_list_i[k]][j+N_list_j[k]] %2 != 0:
                          B[i][j] += A[i+N_list_i[k]][j+N_list_j[k]]
  
              #TopBorder            
              elif i==0 and j &lt; (r-1):
                  N_list_i = N_list_i[4:(4+5)]
                  N_list_j = N_list_j[4:(4+5)]
                  for k in range(len(N_list_j)):
                      if A[i+N_list_i[k]][j+N_list_j[k]] %2 != 0:
                          B[i][j] += A[i+N_list_i[k]][j+N_list_j[k]]
  
              #LeftBorder            
              elif j==0 and i &lt; (r-1):
                  N_list_i = N_list_i[2:(2+5)]
                  N_list_j = N_list_j[2:(2+5)]
                  for k in range(len(N_list_j)):
                      if A[i+N_list_i[k]][j+N_list_j[k]] %2 != 0:
                          B[i][j] += A[i+N_list_i[k]][j+N_list_j[k]]
  
              #BottomBorder            
              elif i==(r-1) and j &lt; (r-1):
                  N_list_i = N_list_i[0:(0+5)]
                  N_list_j = N_list_j[0:(0+5)]
                  for k in range(len(N_list_j)):
                      if A[i+N_list_i[k]][j+N_list_j[k]] %2 != 0:
                          B[i][j] += A[i+N_list_i[k]][j+N_list_j[k]]
  
              #RightBorder            
              elif j==(r-1) and i &lt; (r-1):
                  N_list_i = [1,1,0,-1,-1]
                  N_list_j = [0,-1,-1,-1,0]
                  for k in range(len(N_list_j)):
                      if A[i+N_list_i[k]][j+N_list_j[k]] %2 != 0:
                          B[i][j] += A[i+N_list_i[k]][j+N_list_j[k]]
      return B
  
  A = array([[3,2,8,1,1],
             [1,2,4,1,1],
             [7,3,4,1,8],
             [1,8,0,6,4],
             [5,6,5,3,8]])
  </code></pre>