综合练习:词频统计=

str-text='''Today, as the development of technology,
people work with efficiency and some of the traditional hand-made work have been abandoned.
For example, the paper cutting, which is the classical art for Chinese culture. 
It has the long history and foreigners are so impressed by this amazing work. 
While for the young generation, most of them have no idea how this art is made, 
they only know it from the news and some pictures.
The traditional culture is forgetting and we have duty to keep it as part of the national treasure.
Though technology facilitates our life, we can’t abandon our culture.
More activities should be held to let people know more about our culture.'''

#分隔符全部替换为空格,大写转换为小写,以空格划分每个单词
str1=str-text.replace('',' ').lower().split()
str2=str-text.split()

#统计各个单词出现的次数
c={}
for i in str2:
    count=str1.count(i)
    c[i]=count

#去掉没意义的单词
word=''' we can’t abandon our culture'''
str3=word.split()
for i in str3:
    if i in c.keys():
        del (c[i])


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

#输出频率最大12
for i in range(12):
    print(count[i])

C:\Users\Administrator\PycharmProjects\bd\venv\Scripts\python.exe C:/Users/Administrator/PycharmProjects/bd/Count.py
('the', 9)
('of', 4)
('and', 4)
('have', 3)
('is', 3)
('for', 3)
('culture.', 3)
('it', 3)
('as', 2)
('people', 2)
('work', 2)
('some', 2)

Process finished with exit code 0

转载于:https://www.cnblogs.com/1940370572QQ/p/8650082.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值