Advertisement
Guest User

Untitled

a guest
Nov 7th, 2022
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.70 KB | None | 0 0
  1. count_dict = {}
  2.  
  3. def read_file():
  4.     with open("./pythoWord.txt", "r", encoding="UTF-8") as fin:
  5.         for line in fin:
  6.             line = line.strip()
  7.             words = line.split()
  8.             for word in words:
  9.                 if word not in count_dict:
  10.                     count_dict[word] = 0
  11.                 count_dict[word] += 1
  12.  
  13.         st = sorted(count_dict.items(),
  14.                     key=lambda x: x[1],
  15.                     reverse=True)[:5]
  16.         return st
  17.  
  18.  
  19. def writr_file(data):
  20.     with open("./outPythoWord.txt", "w", encoding="UTF-8") as fout:
  21.         for line in data:
  22.             fout.write("".join(line[0]) + "\n")
  23.  
  24. data = read_file()
  25. print(data)
  26.  
  27. writr_file(data)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement