title: How to improve this python vectorial function
body: <p>I have a function <code>A</code> whose input is a numpy vector (numpy.ndarray) called <code>x</code>. This function calculates, for each element of <code>x</code>, the sum of that element itself with other elements of <code>x</code> given by a list of those elements.</p>
  
  <p>The following example should illustrate this better:</p>
  
  <pre><code>x = [[2,3], [3,4], [1,2], [1,3], [1,4]] # my input
  n = [[1,2,3], [0,4,2], [3,0,1], [0,1,4], [3,1,2]] # list with lists of element to be added for each element in x
  </code></pre>
  
  <p>So for the first element of x, which is x[0] = [2,3] I have to add the values given by n[0], so those are 1, 2 and 3. I obtain them by <code>x[n[0][0]],x[n[0][1]] and x[n[0][2]]</code>.</p>
  
  <p>The expected output for the example should be:</p>
  
  <pre><code>l = [[11, 18], [13, 21], [9, 16], [9, 20], [8, 21]]
  </code></pre>
  
  <p>The final sum for a element x[i] should be</p>
  
  <pre><code>(x[i] + x[n[i][0]] + x[i] + x[n[i][1]] + x[i] + x[n[i][2]])
  </code></pre>
  
  <p>The return of the function is the list with each calculated sum.</p>
  
  <p>As this is iterative I move through both lists x and n. The following code achieve this but goes element by element in both lists x and n.</p>
  
  <pre><code>def A(x):
      a = []
      for i, x_i in enumerate(x):
          mysum = np.zeros(2)
          for j, n_j in enumerate(n[i]):
             mysum = mysum + x_i + x[n_j]
          a.append(mysum)
      return np.array(a)
  </code></pre>
  
  <p>I want to make this code more vectorial, but this is my best since some days ago.</p>
  
  <p>Edit: If it is helpful, I always sum 3 values per element, so the sublists of <code>n</code> are always of lenght 3.</p>