Counting word frequency and making a dictionary from it

前端 未结 10 971
南旧
南旧 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:34

    One more function:

    def wcount(filename):
        counts = dict()
        with open(filename) as file:
            a = file.read().split()
            # words = [b.rstrip() for b in a]
        for word in a:
            if word in counts:
                counts[word] += 1
            else:
                counts[word] = 1
        return counts
    

提交回复
热议问题