综合练习:词频统计=

时间:2021-10-12 11:10:36

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