title: What is the fastest way to combine and sort lists of tuples?
body: <p>I have a list of lists of tuples. Each tuple has the form <code>(string,int)</code>, e.g.</p>
  
  <pre><code>lst = list()
  lst.append([('a',5),('c',10),('d',3),('b',1)])
  lst.append([('c',14),('f',4),('b',1)])
  lst.append([('d',22),('f',2)])
  </code></pre>
  
  <p>Think of the <code>int</code>'s as counts of each string in different blocks of text.</p>
  
  <p>What I need to do is produce a list of top-<code>N</code> occurring strings together with their cumulative counts. So in the example above, <code>a</code> appears 5 times, <code>b</code> appears twice, <code>c</code> appears 24 times etc. If <code>N=2</code>, then I would have to produce either a pair of parallel lists <code>['d','c']</code> and <code>[25,24]</code> or a list of tuples <code>[('d',25),('c',24)]</code>. I need to do it as quickly as possible. My machine has lots of RAM so memory is not an issue.</p>
  
  <p>I have this implementation:</p>
  
  <pre><code>import numpy as np
  def getTopN(lst,N):
  
      sOut = []
      cOut = []
  
      for l in lst:
          for tpl in l:
              s = tpl[0]
              c = tpl[1]
  
              try:
                  i = sOut.index(s)
                  cOut[i] += c
              except:
                  sOut.append(s)
                  cOut.append(c)
  
      sIndAsc = np.argsort(cOut).tolist()
      sIndDes = sIndAsc[::-1]
      cOutDes = [cOut[sir] for sir in sIndDes]
      sOutDes = [sOut[sir] for sir in sIndDes]
  
      return sOutDes[0:N],cOutDes[0:N]
  </code></pre>
  
  <p>There's gotta be a better way, but what would it be?</p>