首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在返回字符串列表中添加单词‘和’

如何在返回字符串列表中添加单词‘和’
EN

Stack Overflow用户
提问于 2020-06-20 17:24:18
回答 3查看 205关注 0票数 2

我要把它放出来:

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

本周的7个视觉词是新的,谷仓,鲨鱼,抓住,艺术,只有,眼睛。补充说,本周的两个观察词是减法。本周的9个视觉词是女孩,房子,最好的,东西,容易,错误,对,再次,上面。本周唯一能看到的词就是疑问。本周没有新的视觉词汇!

但我想知道最后一个字之前的'and‘,这个词需要在返回中添加。这样合适吗?

这是代码:

代码语言:javascript
复制
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))
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-06-20 17:30:34

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

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

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

票数 1
EN

Stack Overflow用户

发布于 2020-06-20 17:29:23

处理多于一个list元素的案例只需使用','连接除最小元素之外的所有元素,然后添加最后一个元素:

代码语言:javascript
复制
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))

输出:

代码语言:javascript
复制
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 '添加到输入中,请将其(包括逗号)放在格式字符串中:

代码语言:javascript
复制
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

票数 1
EN

Stack Overflow用户

发布于 2020-06-20 17:40:50

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

Python有一种获得数组/列表的最后一个元素的非常简单的方法。

代码语言:javascript
复制
wordlist[-1]

所以在这个词前面加上“和”:

代码语言:javascript
复制
wordlist[-1] = "and " + wordlist[-1]

最后的代码应该是这样的

代码语言:javascript
复制
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!'
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62489275

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档