title: Reading a text file to print frequency of letters in decreasing order - Python 3
body: <p>I am doing python basic challenges this is one of them. What all I needed to do is to read through a file and print out the frequency of letters in decreasing order. I am able to do this but I wanted to enhance the program by also printing out the frequency percentage alongside with the letter - frequency - freq%. Something like this: <code>o - 46 - 10.15%</code></p>
  
  <p>This is what I did so far:</p>
  
  <pre><code>def exercise11():
      import string
      while True:
          try:
              fname = input('Enter the file name -&gt; ')
              fop = open(fname)
              break
          except:
              print('This file does not exists. Please try again!')
              continue
  
      counts = {}
      for line in fop:
          line = line.translate(str.maketrans('', '', string.punctuation))
          line = line.translate(str.maketrans('', '', string.whitespace))
          line = line.translate(str.maketrans('', '', string.digits))
          line = line.lower()
          for ltr in line:
              if ltr in counts:
                  counts[ltr] += 1
              else:
                  counts[ltr] = 1
      lst = []
      countlst = []
      freqlst = []
      for ltrs, c in counts.items():
          lst.append((c, ltrs))
          countlst.append(c)
      totalcount = sum(countlst)
      for ec in countlst:
          efreq = (ec/totalcount) * 100
          freqlst.append(efreq)
      freqlst.sort(reverse=True)
      lst.sort(reverse=True)
      for ltrs, c, in lst:
          print(c, '-', ltrs)
  
  exercise11()
  </code></pre>
  
  <p>As you can see I am able to calculate and sort the freq% on a different list but I am not able to include it in the tuple of the <code>lst[]</code> list alongside with the letter, freq. Is there any way to solve this problem? </p>
  
  <p>Also if you have any other suggestions for my code. Please do mention.
  <a href="https://i.stack.imgur.com/OARBw.jpg" rel="nofollow noreferrer">Output Screen</a> </p>
  
  <hr>
  
  <p><em>Modification</em> </p>
  
  <p>Applying a simple modification as mentioned by @wwii I got the desired output. All I had to do is add one more parameter to the print statement while iterating the <code>lst[]</code> list. Previously I tried to make another list for the freq%, sort and then tried to insert it to the letters-count tuple in a list which didn't work out. </p>
  
  <pre><code> for ltrs, c, in lst:
          print(c, '-', ltrs, '-', round(ltrs/totalcount*100, 2), '%')
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/mnEBu.jpg" rel="nofollow noreferrer">Output Screen</a></p>