美文网首页
Python 使用collections统计词频

Python 使用collections统计词频

作者: 致Great | 来源:发表于2018-07-05 11:03 被阅读59次
    • 方法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)]
    

    相关文章

      网友评论

          本文标题:Python 使用collections统计词频

          本文链接:https://www.haomeiwen.com/subject/lgguuftx.html