Python 使用collections统计词频

2018-07-05  本文已影响59人  致Great
dictionary = {}
for word in word_list:
    if not word in dictionary:
        dictionary[word] = 1
    else:
        dictionary[word]+= 1
print(dictionary)
输出
{'I': 2, 'am': 1, 'a': 1, 'student': 1, 'in': 1, 'RUC.': 1, 'Like': 1, 'playing': 1, 'basketball': 1}
from collections import defaultdict # again, collections!
dictionary = defaultdict(int)
for word in word_list:
    dictionary[word] += 1
print(dictionary,dictionary['I'])
defaultdict(<class 'int'>, {'I': 2, 'am': 1, 'a': 1, 'student': 1, 'in': 1, 'RUC.': 1, 'Like': 1, 'playing': 1, 'basketball': 1}) 2
from collections import Counter

print(word_list)
counter = Counter(word_list)
dictionary=dict(counter)
print(dictionary) # 统计词频
print(counter.most_common(2))
输出
['I', 'am', 'a', 'student', 'in', 'RUC.', 'I', 'Like', 'playing', 'basketball']
{'I': 2, 'am': 1, 'a': 1, 'student': 1, 'in': 1, 'RUC.': 1, 'Like': 1, 'playing': 1, 'basketball': 1}
[('I', 2), ('am', 1)]
上一篇下一篇

猜你喜欢

热点阅读