谁家的June 2022-05-31 21:07 采纳率: 50%
浏览 205
已结题

Python从给定字符串中提取单词

问题遇到的现象和发生背景

统计如下字符串str 中每个单词出现的次数,结果存入 dict 中,单词为key,次数为 value,
并按照 value 由高到底排序,输出此 dict

问题相关代码,请勿粘贴截图
str  = """The Zen of Python, by Tim Peters
            Beautiful is better thanugly.
            Explicit is better than implicit.
            Simple is better than complex.
            Complex is better than complicated.
            Flat is better than nested.
            Sparseisbetterthandense.
            Readability counts.
            Specialcasesaren'tspecialenoughtobreaktherules.
            Although practicality beats purity.
            Errors should never pass silently.
            Unless explicitly silenced.
            In the face of ambiguity, refuse the temptation to guess.
            Thereshouldbeone--andpreferablyonlyone --obviouswayto do it.
            Although that way may not be obvious at first unless you're Dutch.
            Now is better than never.
            Although never is often better than *right* now.
            If the implementation is hard to explain, it's a bad idea.
            If the implementation is easy to explain, it may be a good idea.
            Namespacesareonehonkinggreatidea--let'sdomoreofthose!"""
我的解答思路和尝试过的方法

尝试用正则表达式解答过 没有空格的句子无法提取单词出来,例如“Specialcasesaren'tspecialenoughtobreaktherules”直接视为一个单词了,还有are'nt无法识别提取成一个单词

我想要达到的结果

其中没有空格区分的句子和are'nt该如何区分提取单词出来

  • 写回答

3条回答 默认 最新

  • Hann Yang 全栈领域优质创作者 2022-05-31 22:08
    关注

    注:str dict 都是内置函数,尽量不要用它们作变量名。

    zen  = """The Zen of Python, by Tim Peters
                Beautiful is better than ugly.
                Explicit is better than implicit.
                Simple is better than complex.
                Complex is better than complicated.
                Flat is better than nested.
                Sparse is better than dense.
                Readability counts.
                Special cases aren't special enough to break the rules.
                Although practicality beats purity.
                Errors should never pass silently.
                Unless explicitly silenced.
                In the face of ambiguity, refuse the temptation to guess.
                There should be one-- and preferably only one --obvious way to do it.
                Although that way may not be obvious at first unless you're Dutch.
                Now is better than never.
                Although never is often better than *right* now.
                If the implementation is hard to explain, it's a bad idea.
                If the implementation is easy to explain, it may be a good idea.
                Namespaces are one honking great idea -- let's do more of those!"""
    
    
    punc = [',','.','-','!','*']
    
    for p in punc:
        zen = zen.replace(p,' ')
    
    lst = zen.lower().split()
    
    dic = {}
    
    for i in lst:
        dic[i] = dic.get(i,0) + 1
    
    for key,value in sorted(dic.items(), key=lambda x:x[1], reverse=True):
        print(f'{key:>15}:{value}')
    


    ↓↓↓如有帮助请点个采纳,谢谢!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 6月9日
  • 已采纳回答 6月1日
  • 创建了问题 5月31日

悬赏问题

  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同