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

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日

悬赏问题

  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法