ivapopova

count_words.py

Apr 17th, 2018
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.74 KB | None | 0 0
  1. text = """
  2. one two three and
  3. one three labala three two
  4. """
  5.  
  6. def split_into_words(text):
  7.     tmp_list = text.split(sep=" ")
  8.     words_list = []
  9.  
  10.     for word in tmp_list:
  11.         tmp = word.split(sep="\n")
  12.         # print(tmp)
  13.         for w in tmp:
  14.             if w:
  15.                 words_list.append(w)
  16.  
  17.  
  18.     return words_list
  19.  
  20. def clean_words(words_list):
  21.     cleaned_list = []
  22.     for word in words_list:
  23.             if word[0] =="\n":
  24.                 new_word = word[1:]
  25.  
  26. def count_words(words_list):
  27.     words_dict = {}
  28.     for word in words_list:
  29.         if word in words_dict.keys():
  30.             words_dict[word] += 1
  31.         else:
  32.             words_dict[word] = 1
  33.  
  34.  
  35.     return words_dict
  36.  
  37.  
  38. words_list = split_into_words(text)
  39. print(words_list)
  40.  
  41. words_dict = count_words(words_list)
  42. print(words_dict)
  43. # clean_words(words_list)
Advertisement
Add Comment
Please, Sign In to add comment