Counting unique words in python

前端 未结 3 905
深忆病人
深忆病人 2020-12-19 10:15

In direct, my code so far is this :

from glob import glob
pattern = \"D:\\\\report\\\\shakeall\\\\*.txt\"
filelist = glob(pattern)
def countwords(fp):
    w         


        
3条回答
  •  -上瘾入骨i
    2020-12-19 10:28

    If you want to get count of each unique word, then use dicts:

    words = ['Hello', 'world', 'world']
    count = {}
    for word in words :
       if word in count :
          count[word] += 1
       else:
          count[word] = 1
    

    And you will get dict

    {'Hello': 1, 'world': 2}
    

提交回复
热议问题