有一句话:['这是我的笔','瓶是半透明的']
需要脚本,每个元素中的下一个单词应返回“ is”
首选输出:['my','translucent']
答案 0 :(得分:0)
itertools
的变体。 ...有点矫kill过正:
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])
答案 1 :(得分:0)
代码(非常简单的方法):
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])
输出:
['my', 'translucent']
答案 2 :(得分:0)
在itertools过度杀伤和c风格的python之间的某个地方:
def word_after_word(锚,词组):
单词=短语.split('')
枚举=枚举(单词)
用于idx,枚举中的项目:
如果锚==项:
休息
返回next(enum)
结果= word_after_word('是',['这是我的笔','瓶是半透明的'] [0])
print(f“ result {result}”)
答案 3 :(得分:0)
list = ["this is my pen", "bottle is translucent"]
lst_of_lst =[i.split(" ")for i in list]
result =[lst[ind+1] for lst in lst_of_lst for ind,ele in enumerate(lst) if ele == "is"]
#result=['my', 'translucent']