Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1.  
  2.  
  3. from collections import OrderedDict
  4.  
  5. def main():
  6. ord_dict = OrderedDict()
  7.  
  8. filename = '285.txt'
  9. all_chars = 0
  10.  
  11. with open(filename, encoding='utf8', errors='ignore') as f:
  12. while True:
  13. c = f.read(1).lower()
  14.  
  15. if c != ' ':
  16. counter = ord_dict.get(c, None)
  17. if counter is None:
  18. ord_dict[c] = 1
  19. else:
  20. ord_dict[c] += 1
  21. all_chars += 1
  22. if not c:
  23. break
  24.  
  25. tuples_sorted = sorted(ord_dict.items(), key=lambda x:x[1], reverse=True)
  26. for x, y in tuples_sorted:
  27. print(x, round(y / all_chars * 100, 2), "%")
  28.  
  29. if __name__ == '__main__':
  30. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement