Python3词频统计和排序

1.方法一:

# -*- coding: utf-8 -*-
from collections import defaultdict
import operator

#词集列表
dataset = [ ['my', 'dog', 'has', 'flea', 'problems', 'help', 'please'],
            ['maybe', 'not', 'take', 'him', 'to', 'dog', 'park', 'stupid'],
            ['my', 'dalmation', 'is', 'so', 'cute', 'I', 'love', 'him'],
          ]

word_frequency=defaultdict(int)
for row in dataset:
    for i in row:
        word_frequency[i]+=1
word_sort=sorted(word_frequency.items(),key=operator.itemgetter(1),reverse=True) #根据词频降序排序
print(word_sort)

2.方法二:

# -*- coding: utf-8 -*-

#词集列表
dataset = [ ['my', 'dog', 'has', 'flea', 'problems', 'help', 'please'],
            ['maybe', 'not', 'take', 'him', 'to', 'dog', 'park', 'stupid'],
            ['my', 'dalmation', 'is', 'so', 'cute', 'I', 'love', 'him'],
          ]

word_frequency={}
for row in dataset:
    for i in row:
        if i in word_frequency:
            word_frequency[i]+=1
        else:
            word_frequency[i]=1
word_sort=sorted(word_frequency.items(),key=lambda x:x[1],reverse=True) #根据词频降序排序
print(word_sort)

最终输出结果:

 

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页