- 方法1
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}
- 方法 2
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
- 方法 3
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)]