Counting word frequency and making a dictionary from it

前端 未结 10 992
南旧
南旧 2020-12-05 21:08

I want to take every word from a text file, and count the word frequency in a dictionary.

Example: \'this is the textfile, and it is used to take words and co

10条回答
  •  孤城傲影
    2020-12-05 21:30

    sentence = "this is the textfile, and it is used to take words and count"
    
    # split the sentence into words.
    # iterate thorugh every word
    
    counter_dict = {}
    for word in sentence.lower().split():
    # add the word into the counter_dict initalize with 0
      if word not in counter_dict:
        counter_dict[word] = 0
    # increase its count by 1   
      counter_dict[word] =+ 1
    

提交回复
热议问题