如何统计一个文本中单词频次最高的10个单词?【面试题详解】

今天爱分享给大家带来如何统计一个文本中单词频次最高的10个单词?【面试题详解】,希望能够帮助到大家。



import re

def test(filepath):

    distone = {}
    numTen = []

    with open(filepath,"r",encoding="utf-8") as f:
        for line in f:
            line = re.sub("\W","",line)
            lineone = line.split()
            for keyone in lineone:
                if not distone.get(keyone):
                    distone[keyone]=1
                else:
                    distone[keyone]+=1
    numTen = sorted(distone.items(),key=lambda x:x[1],reverse=True)[:10]
    numTen =[x[0]for x in numTen]
    return numTen

人已赞赏
Python

给定两个列表,怎么找出他们相同的元素和不同的元素?【面试题详解】

2020-12-24 11:46:43

Python

损失函数常用的有几种【图文详解】

2020-12-24 15:15:14

'); })();