【问题标题】:How to add the word 'and' to the return of string list如何将单词'and'添加到字符串列表的返回中
【发布时间】:2020-10-10 19:55:10
【问题描述】:

我说得对:

需要我的输出看起来像这样:

本周的 7 个视觉词是 new、barn、shark、hold、art、only、 eyes。 本周的 2 个常用词是减法、加法。 本周的 9 个常用词分别是女孩、房子、最好、事情、容易、错误、正确、再次、上面的。 本周唯一的视觉词是问题。 本周没有新的视觉词!

但我想知道需要在返回中添加的最后一个词之前的“和”。这样合适吗?

这是代码:

wordlist = [['new', 'barn', 'shark', 'hold', 'art', 'only', 'eyes'],
            ['subtract', 'add'],
            ['girl', 'house', 'best', 'thing', 'easy', 'wrong', 'right', 'again', 'above'],
            ['question'],
            []]

def createSentence(wordlist):
    if len(wordlist) > 1:
        wordlist[-1] = "and " + wordlist[-1]
        return f'The {len(wordlist)} sight words for this week are {", ".join(wordlist)}.'
    elif len(wordlist) == 1:
        return f'The only sight word for this week is {wordlist[0]}.'
    elif len(wordlist) == 0:
        return 'There are no new sight words for this week!'

for lst in wordlist:
    print(createSentence(lst))

【问题讨论】:

  • 进行了调整。

标签: python list return


【解决方案1】:

您可以将第一个n-1 元素与, 连接起来,然后在末尾添加and nth element

if len(wordlist) > 1:
    return f'The {len(wordlist)} sight words for this week are {", ".join(wordlist[:-1])} and {wordlist[-1]}.'

这样您就无需在输入列表的最后一个单词之前手动添加and

【讨论】:

    【解决方案2】:

    处理多于 1 个列表元素的情况只需将除最小元素之外的所有元素与',' 连接起来,然后添加最后一个元素:

    wordlist = [['new', 'barn', 'shark', 'hold', 'art', 'only', 'and eyes'],
                ['subtract', 'and add'],
                ['girl', 'house', 'best', 'thing', 'easy', 'wrong', 
                 'right', 'again', 'and above'],
                ['question'],
                []]
    
    def createSentence(wordlist):
        if len(wordlist) > 1:
            return (f'The {len(wordlist)} sight words for this week are'
                    f' {", ".join(wordlist[:-1])} {wordlist[-1]}.')
        elif len(wordlist) == 1:
            return f'The only sight word for this week is {wordlist[0]}.'
        elif len(wordlist) == 0:
            return 'There are no new sight words for this week!'
    
    for lst in wordlist:
        print(createSentence(lst))
    

    输出:

    The 7 sight words for this week are new, barn, shark, hold, art, only and eyes.
    The 2 sight words for this week are subtract and add.
    The 9 sight words for this week are girl, house, best, thing, easy, wrong, right, again and above.
    The only sight word for this week is question.
    There are no new sight words for this week!
    

    如果您不想在输入中添加'and ',请将其(包括逗号)放在格式字符串中:

    wordlist = [['new', 'barn', 'shark', 'hold', 'art', 'only','eyes'],
                ['subtract', 'add'],
                ['girl', 'house', 'best', 'thing', 'easy', 'wrong', 'right', 'again','above'],
                ['question'],
                []]
    
    def createSentence(wordlist):
        if len(wordlist) > 1:
            return (f'The {len(wordlist)} sight words for this week are ' 
                    f'{", ".join(wordlist[:-1])}, and {wordlist[-1]}.')
        elif len(wordlist) == 1:
            return f'The only sight word for this week is {wordlist[0]}.'
        elif len(wordlist) == 0:
            return 'There are no new sight words for this week!'
    
    for lst in wordlist:
        print(createSentence(lst))
    

    得到与上面完全相同的输出。


    您为此使用列表切片。更多信息:Understanding slice notation

    【讨论】:

      【解决方案3】:

      听起来您想将 "and " 添加到数组/列表中最后一个元素的开头,但前提是您有超过 1 个单词。

      Python 有一种超级简单的方法来获取数组/列表的最后一个元素。

      wordlist[-1]
      

      所以在前面加上“and”:

      wordlist[-1] = "and " + wordlist[-1]
      

      你的最终代码应该是这样的

      def createSentence(wordlist):
          if len(wordlist) > 1:
              wordlist[-1] = "and " + wordlist[-1]
              return f'The {len(wordlist)} sight words for this week are {", ".join(wordlist)}.'
          elif len(wordlist) == 1:
              return f'The only sight word for this week is {wordlist[0]}.'
          elif len(wordlist) == 0:
              return 'There are no new sight words for this week!'
      

      【讨论】:

      • 这有一个你应该避免的副作用。该函数每次运行时都会修改最后一个元素,所以如果你第二次使用相同的输入运行它,最后一个元素将变为and and element
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-07
      • 2023-01-09
      • 1970-01-01
      • 1970-01-01
      • 2023-04-10
      • 2022-06-12
      • 1970-01-01
      相关资源
      最近更新 更多