• 综合练习:词频统计=


    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

  • 相关阅读:
    Java多线程之 对同一个值操作
    Java 之 应用多线程计算1+2+...+100之多种方法比较(二)
    Java 之 应用多线程计算1+2+...+100之多种方法比较(一)
    SQL语句优化
    第六章:shiro Realm相关对象
    第五章:shiro密码加密
    第四章:shiro的INI配置
    第三章:shiro授权认证
    第二章:shiro身份验证
    第一章:Shiro简介
  • 原文地址:https://www.cnblogs.com/1940370572QQ/p/8650082.html
Copyright © 2020-2023  润新知