【问题标题】:Capitalize a word based on a matching word in a list根据列表中的匹配单词将单词大写
【发布时间】:2020-05-09 05:11:57
【问题描述】:

完成“Coursera Python”课程,我遇到了很多麻烦。

highlight_word 函数将句子中的给定单词更改为其大写形式。例如,highlight_word("Have a nice day", "nice") 返回"Have a NICE day"。我需要帮助只用一行代码重写这个函数吗?

def highlight_word(sentence, word):
    return(___)

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"))

我想我可以在更大的语句中做到这一点,但有人知道如何在一行中正确返回吗?我猜这将涉及列表理解。

【问题讨论】:

  • 您可以通过简单的正则表达式替换来做到这一点。
  • 您可以使用split()join() 和列表理解来完成,但这会很长且令人困惑。
  • @AndrejKesely 这不会尊重单词边界。
  • 对于这个简单的案例(忽略单词边界),您可能可以这样做return sentence.replace(word, word.upper())

标签: python list list-comprehension uppercase


【解决方案1】:

re.sub 有效,但它仍然是不正确的答案并且过于复杂 - @C。 Leconte 使用简单替换是正确的。

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

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"))

谢谢

【讨论】:

    【解决方案2】:

    可以通过regular expression using re.sub 完成

    def highlight_word(sentence, word):
      return re.sub(r'\b' + word + r'\b', word.upper(), sentence)
    

    【讨论】:

      【解决方案3】:

      一行中的部分答案,就像@Barmar 暗示的那样:

      def highlight_word(sentence, word): return " ".join([x.upper() if x.lower() == word.lower() else x for x in sentence.split()])
      

      基本上 - 将句子拆分为单词,并使用列表推导 up() 匹配的单词。然后使用 join() 将句子重新组合在一起。

      编辑:sentence.split() 只会在空格上拆分,因此它不会将第二个示例大写为“大声!” !=“大声”。在这种情况下,您可以使用regex library 进行替换。

      是的,它有效:

      【讨论】:

      • 可能应该使用if x.lower() == word,所以它适用于Python之类的东西。
      • 不要这样做:highlight = lambda
      • 我无法让第二个示例正常工作 - 我假设他们在提出问题时搞砸了,因为尚未涵盖任何正则表达式。有没有办法检查 .isalpha() 或此语句中的某些内容以超越 !随附的?您能否告诉我使用正则表达式的示例可能是什么样的?
      • 你为什么不在字符串上使用一个简单的 replace() 方法将 word 更改为 word.upper(),这对你来说是禁止的吗?
      • 我有一个问题。有谁知道为什么我的类似于上面的代码不起作用?我认为 if 条件必须在列表理解语句的末尾。 return " ".join([x.upper() for x in sentence.split() if x == word else x])
      【解决方案4】:

      你可以试试这个,它对我有用!

      def highlight_word(sentence, word):
        return sentence[0:sentence.index(word)] + word.upper()+ sentence[sentence.index(word)+len(word):] 
      

      【讨论】:

        【解决方案5】:

        根据课程所教的内容,以下表达式适用于我: def highlight_word(sentence, word): return(sentence[:sentence.find(word)]+word.upper()+sentence[sentence.find(word)+len(wor d):])

        这里我只使用了字符串切片

        【讨论】:

          【解决方案6】:

          你可以用re.sub()函数来做到这一点

          import re
          
          def highlight_word(sentence, word):
               return [(re.sub(y, y.upper(), x)) for x in sentence for y in word if y in x]
          
          
          words = ["have a nice day", "Shhh, don't be so loud!","Automating with Python is fun"]
          lowers = ['nice','loud','fun']
          
          print(highlight_word(words,lowers))
          

          输出:

          ['have a NICE day', "Shhh, don't be so LOUD!", 'Automating with Python is FUN']
          

          【讨论】:

            【解决方案7】:

            您可以使用 'replace(string,replaced_string)' 在一行中完成此操作,如图所示:

            def highlight_word(sentence, word):
            return(sentence.replace(sentence[sentence.find(word):sentence.find(word)+len(word)],sentence[sentence.find(word):sentence.find(word)+len(word)].upper()))
            

            sentence.replace(sentence[sentence.find(word):sentence.find(word)+len(word)]

            它适用于以下输入:

            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"))
            

            你得到以下输出

             Have a NICE day
             Shhh, don't be so LOUD!
             Automating with Python is FUN
            

            【讨论】:

              猜你喜欢
              • 2022-06-10
              • 2020-02-02
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多