英语词频统计

str = '''A man was going to the house of some rich person. As he went along the road, he saw a box of good apples at the side of the road. He said, 
       I do not want to eat those apples;for the rich man will give me much food; he will give me very nice food to eat.Then he took the apples and 
       threw them away into the dust. He went on and came to a river. The river had become very big; so he could not go over it. He waited for some time; 
       then he said, I cannot go to the rich mans house today, for I cannot get over the river. He began to go home. He had eaten no food that day. 
       e began to want food. He came to the apples, and he was glad to take them out of the dust and eat them. Do not throw good things away; you may be glad to have them at some other time. 
'''

punctuation=['.',',',':',';']
prep=['of','to','a','the','and','some']

for i in range(len(punctuation)): #删除标点符号
    str = str.replace(punctuation[i],' ')

str=str.lower() #将大写转换成小写
str=str.split() #将字符串分成单词列表

d = dict()
for s in str:
    d[s] = d.get(s, 0) + 1                   #计数

for i in prep:
    if(d.get(i)!=None):                   #如果为介词,将其删去
        d.pop(i)

d = sorted(d.items(),key=lambda x:x[1],reverse = True)  #排序

for i in range(10):  #输出前十个
    print(d[i])

运行截图:

 

转载于:https://www.cnblogs.com/wxyplu/p/8622012.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值