【发布时间】:2019-10-21 11:33:31
【问题描述】:
我有一个句子列表。例如:
x = ['Mary had a little lamb',
'Jack went up the hill',
'Jill followed suit',
'i woke up suddenly',
'I just missed the train',
'it was a really bad dream']
我想选择倒数第二个单词不是“the”的选项。 我怎么能在 python 3 上做到这一点? 我试过这个:
l = []
for i in x:
for k in i:
if i.index(k) != (len(i) -2):
l.append(' '.join(i))
我在小列表上工作,但不在大列表上(几千个元素)
【问题讨论】:
-
请展示您的尝试。
-
您的代码是如何“工作”的?它甚至没有
"the"。将每个字符串的len(i)-1“间隔”副本添加到结果列表似乎是一种非常低效的方法。 -
它的部分代码,我也有一个不应该是倒数第二个单词的列表
标签: python python-3.x loops select