社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
私信  •  关注

ThiefMaster sipi09

ThiefMaster sipi09 最近创建的主题
ThiefMaster sipi09 最近回复了
3 年前
回复了 ThiefMaster sipi09 创建的主题 » 如果python不区分大小写,如何用特定模式替换给定句子中的单词?

这是我的解决方案(希望我能很好地理解你的问题):

  1. 你把句子分成了几个词。

  2. 你把所有的单词循环一遍

  3. 你从每个单词的第二个字母开始循环单词的字母。

  4. 检查是否有大写字母

  5. 如果你找到大写字母,你给have_to_replace变量加1。

  6. 你来替换

    txt = 'My team is the BeST team at the tournament'
    x = txt.split()
    
    for w in x:
        have_to_replace = False
        for l in w[1:]:
            if l != l.casefold():
                have_to_replace = True
        if have_to_replace:
            txt = txt.replace(w, 'XXX_', 1)
    print(txt)
    

由于某些原因,前三行(以及最后一行)的代码阻塞无法正常工作。抱歉。