我要把它放出来:
需要我的输出看起来像这样:
本周的7个视觉词是新的,谷仓,鲨鱼,抓住,艺术,只有,和眼睛。和补充说,本周的两个观察词是减法。本周的9个视觉词是女孩,房子,最好的,东西,容易,错误,对,再次,和上面。本周唯一能看到的词就是疑问。本周没有新的视觉词汇!
但我想知道最后一个字之前的'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:
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))发布于 2020-06-20 17:30:34
您可以将第一个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。
发布于 2020-06-20 17:29:23
处理多于一个list元素的案例只需使用','连接除最小元素之外的所有元素,然后添加最后一个元素:
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
发布于 2020-06-20 17:40:50
听起来,您想要将"and "添加到数组/列表中最后一个元素的开头,但前提是您有超过一个单词。
Python有一种获得数组/列表的最后一个元素的非常简单的方法。
wordlist[-1]所以在这个词前面加上“和”:
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!'https://stackoverflow.com/questions/62489275
复制相似问题