title: Calculate the row average of selected columns
body: <p>I have a csv file:</p>
  
  <pre><code>#col1 #col2 ... #col253
  33,    123, ...    99
  19,    409, ...    24
  34,    239, ...    60
  ...    ...  ...    ..
  </code></pre>
  
  <p>and a dictionary whose values store the start and end index of selected columns:</p>
  
  <pre><code>d = {
      'win': [(11, 55), (194, 233)],
      'lose': [(72, 111), (133, 172)],
      'neut': [(0, 10), (51, 71), (112, 132), (173, 193)]
      }
  </code></pre>
  
  <p>My goal is to calculate averages per row for all the selected columns of a certain key in the dictionary.</p>
  
  <p>For example, for the <code>'win'</code> block of the first row, select the <code>11, 12, ...55, 194, 195,...233</code> columns and calculate the average.</p>
  
  <p>What I am using now:</p>
  
  <pre><code>x = np.loadtxt('filename.csv', delimiter=',')
  for line in x:
      selected = [line[start:end + 1] for (start, end) in d['win']]
      ...
  </code></pre>
  
  <p><code>selected</code> will be <code>[array([39, 12, 94,...]), array([3, 4, ...])]</code> which can not be passed to <code>np.mean()</code>. </p>
  
  <p>So arrays in the list should be combined to one, which I don't know how to do elegantly, or iteratively add all numbers and then take the average, which I think is ugly.</p>
  
  <hr>
  
  <p><strong>Edited:</strong></p>
  
  <p>An ugly method could be:</p>
  
  <pre><code>average = sum(map(sum, selected)) / sum(map(len, selected))
  </code></pre>