私信  •  关注

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)
    

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