1. 程式人生 > >用python統計檔案中各個單詞出現的次數

用python統計檔案中各個單詞出現的次數

import string

d = {}
def choice(str):
    s = str.lower() #全部轉化為小寫
    for c in range(97,123): #ASCII 97-122
        for i in chr(c): #ASCII 轉化為字元
            d[i] = s.count(i) #統計s中出現'i'的次數
    print(d)

with open('test','r') as f:
    choice(f.read())