l = ['this is my pen','bottle is translucent']
res = []
for i in range(len(l)):
words = l[i].split()
length = len(words)
for c in range(length):
if words[c] == "is":
res.append(words[c+1])
from itertools import dropwhile, islice
lst = ["this is my pen", "bottle is translucent"]
new = [
next(islice(dropwhile(lambda word: word != "is", phrase.split()), 1, None))
for phrase in lst
]
print(new) # ['my', 'translucent']
更简单:
new = []
for phrase in lst:
splt = phrase.split()
new.append(splt[splt.index("is") + 1])
介于itertools overkill和c风格python之间:
在单词(锚定词、短语)后定义单词:
单词=短语。拆分(“”)
enum=枚举(单词)
对于idx,枚举中的项:
如果锚定==项目:
中断
返回下一个(枚举)
结果=单词后面的单词('is',['this is my pen','bottle is translucent'][0])
打印(f“result{result}”)
代码(非常简单的方法):
输出:
一种
itertools
变体。。。有点过分了:更简单:
相关问题 更多 >
编程相关推荐