Advertisement
biswasrohit20

freq

Apr 25th, 2021
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. myfile = open('secret.txt')
  2. letters = list('ABCDEFGHIJKLMNOPQRSTUVWXYZ')
  3. freq = []
  4. for i in range(26):
  5. freq.append(0)
  6.  
  7. for line in myfile:
  8. for ch in line:
  9. if ch.upper() in letters:
  10. freq[letters.index(ch.upper())] += 1
  11.  
  12.  
  13. f = open("letterFreqs.txt", "w")
  14. f.write(('{0:7}{1:7}{2:7}{2:7}\n'.format(f'{letters[0]}:{freq[0]}',f'{letters[1]}:{freq[1]}',f'{letters[2]}:{freq[2]}',f'{letters[3]}:{freq[3]}')))
  15. f.write(('{0:7}{1:7}{2:7}{2:7}\n'.format(f'{letters[4]}:{freq[4]}',f'{letters[5]}:{freq[5]}',f'{letters[6]}:{freq[6]}',f'{letters[7]}:{freq[7]}')))
  16. f.write(('{0:7}{1:7}{2:7}{2:7}\n'.format(f'{letters[8]}:{freq[8]}',f'{letters[9]}:{freq[9]}',f'{letters[10]}:{freq[10]}',f'{letters[11]}:{freq[11]}')))
  17. f.write(('{0:7}{1:7}{2:7}{2:7}\n'.format(f'{letters[12]}:{freq[12]}',f'{letters[13]}:{freq[13]}',f'{letters[14]}:{freq[14]}',f'{letters[15]}:{freq[15]}')))
  18. f.write(('{0:7}{1:7}{2:7}{2:7}\n'.format(f'{letters[16]}:{freq[16]}',f'{letters[17]}:{freq[17]}',f'{letters[18]}:{freq[18]}',f'{letters[19]}:{freq[19]}')))
  19. f.write(('{0:7}{1:7}{2:7}{2:7}\n'.format(f'{letters[20]}:{freq[20]}',f'{letters[21]}:{freq[21]}',f'{letters[22]}:{freq[22]}',f'{letters[23]}:{freq[23]}')))
  20. f.write(('{0:7}{1:7}'.format(f'{letters[24]}:{freq[24]}',f'{letters[25]}:{freq[25]}')))
  21. f.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement