title: return the top n most frequently occurring chars and their respective counts in python
body: <p>how to return the top n most frequently occurring chars and their respective counts # e.g <code>'aaaaaabbbbcccc'</code>, <code>2</code> should return <code>[('a', 6), ('b', 4)]</code> in python</p>
  
  <p>I tried this</p>
  
  <pre><code>def top_chars(input, n):
      list1=list(input)
      list3=[]
      list2=[]
      list4=[]
      set1=set(list1)
      list2=list(set1)
      def count(item):
          count=0
          for x in input:
              if x in input:
                  count+=item.count(x)
          list3.append(count)
          return count
  
      list2.sort(key=count)
  
      list3.sort()
      list4=list(zip(list2,list3))
      list4.reverse()
      list4.sort(key=lambda list4: ((list4[1]),(list4[0])), reverse=True)
      return list4[0:n]
      pass
  </code></pre>
  
  <p>but it doesn't work for the input ("aabc",2)
  The output it should give is </p>
  
  <pre><code>[('a', 2), ('b', 1)]
  </code></pre>
  
  <p>but the output I get is</p>
  
  <pre><code>[('a', 2), ('c', 1)]
  </code></pre>