Guest User

Untitled

a guest
Jun 22nd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. import sys
  2. import os
  3.  
  4. try:
  5. f = open(sys.argv[1])
  6. except:
  7. input_line = input()
  8. if os.path.exists(input_line):
  9. f = open(input_line)
  10. else:
  11. print("File not exists")
  12. sys.exit(1)
  13.  
  14. mydict = {"":0}
  15.  
  16. i=0
  17. for line in f:
  18. words = line.replace(",", " ").replace('"',' ').split()
  19.  
  20. for word in words:
  21. word = word.lower()
  22. i += 1
  23. if(word in mydict.keys()):
  24. value = mydict[word]
  25. value += 1
  26. mydict.pop(word)
  27. else:
  28. value = 1
  29.  
  30. mydict[word] = value
  31.  
  32. print("Number of words: " + str(i))
  33.  
  34. i=0
  35. for k, v in sorted(mydict.items(), key=lambda x: -x[1]):
  36. if(i >= 20):
  37. break
  38. print(str(k) + ": " + str(v))
  39. i += 1
  40.  
  41. f.close()
Add Comment
Please, Sign In to add comment