繁体   English   中英

如何突出显示字符串中的单词 [Python]

[英]How to highlight words in a string [Python]

我想制作一个 function ,它将突出显示 [.upper() ] 每次出现给定的任何单词。

  • 我已经成功地让我的 function 工作,但前提是我们给它一个字来突出显示。
  • 如果我们给它超过 1 个单词,function 会打印多个 1 个单词突出显示的句子。

我理解它为什么会这样做,但我不知道任何其他方式甚至可以远程接近使其工作,所以你的帮助真的会帮助我吗? 我尝试将不同的最终结果保存到一个列表中,但是我应该如何将它们连接到一个最终的最终句子中?

def highlight_words(sentence, words):
    final = ""
    k = 0
    for j in range(len(words)):
        for i in range(len(sentence)):
            if k != 0:
                k -= 1
                continue
            changed = ""
            if sentence.lower().startswith(words[j].lower(), i):
                changed = sentence[i:i+len(words[j])].upper()
                final += changed
                k = len(words[j]) - 1
            else:
                final += sentence[i]
    return final

print(highlight_words("Have a nIcE day, you Nice person!!", ["nice"]))
print(highlight_words("Shhh, don't be so loud!", ["loud", "Be"]))
print(highlight_words("Automating with Python is fun", ["fun", "auTomaTiNG"]))

这是程序打印的内容:

有一个美好的一天,你很好的人!

嘘,别那么大声,嘘,别那么大声!

用 Python 自动化很有趣用 Python 自动化很有趣

如果您不在解决方案中使用任何导入的库,我将不胜感激! 提前致谢!

我认为如果你将外循环设为字符串而不是单词会容易得多。 下面是一个选项。

def highlight_words(sentence, words):
    for i in range(len(sentence)):
        for j in range(len(words)):    
            if sentence.lower().startswith(words[j].lower(), i):
                sentence = sentence[:i] + sentence[i:i+len(words[j])].upper() + sentence[i+len(words[j]):]
    return sentence

print(highlight_words("Have a nIcE day, you Nice person!!", ["nice"]))
print(highlight_words("Shhh, don't be so loud!", ["loud", "Be"]))
print(highlight_words("Automating with Python is fun", ["fun", "auTomaTiNG"]))

你可以简单地通过使用splitjoin来做到这一点,如下所示:

def highlight_words(sentence, words):
    for word in words:
        if(word.lower() in sentence.lower()):
            sentence = sentence.split(word)
            sentence = word.upper().join(sentence)
    return sentence

希望能帮助到你:)

使用递归:-

def highlight_words(sentence, words):
    for word in words:
        pos = sentence.lower().find(word.lower())
        sentence = sentence if pos < 0 else sentence[0:pos] + word.upper() + highlight_words(
            sentence[pos + len(word):], [word])
    return sentence

这是我的答案,它在 Coursera Quiz 中运行良好。

def highlight_word(sentence, word):
    ans=""
    parts=sentence.split()#parts = splitted sentence
    for part in parts:
        Cpart=part.strip("!,")#Cpart means Clean part after stripping the ! ans , etc
        if Cpart==word:#if our clean part is equal to word provided
            part=part.upper()#then convert the part to Upper Case
        ans = ans+" "+part#keep adding the parts in ans string
    return(ans)

print(highlight_word("Have a nice day", "nice"))
print(highlight_word("Shhh, don't be so loud!", "loud"))
print(highlight_word("Automating with Python is fun", "fun"))

这个问题可以用string.replace(old, new)方法解决。

这是我的解决方案:

def highlight_word(sentence, word):
    return sentence.replace(word, word.upper())

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM