我有关键字列表,让我们在这样的列表中说 6...
keywords = ['python', 'c plus plus', 'java', 'javascript', 'c#', 'assembly']
并有成千上万这样的句子......
sentence = [
'my first python program was a scraping script',
'i want to learn c plus plus in future',
'i want to learn java in future too',
'i love javascript very much',
'my friend very knowledgeble in c# programming',
'i heard of assembly but never tried yet'
]
我想要做的是从关键字列表中获取一个关键字,让我们说“python”然后搜索所有句子,如果找到则执行某些操作或否则通过。我尝试过一些东西,但如果我添加更多关键字,那么代码会很长。我在 SO 上进行了搜索,但无法获得我想要完成的任务。有没有更好的方法...提前致谢。
这是我尝试过的一些代码
keywords = ['python', 'c plus plus', 'java', 'javascript', 'c#', 'assembly']
sentence = [
'my first python program was a scraping script',
'i want to learn c plus plus in future',
'i want to learn java in future too',
'i love javascript very much',
'my friend very knowledgeble in c# programming',
'i heard of assembly but never tried yet'
]
for i in sentence:
if 'python' in i:
lang = 'python'
begining = i.split('python')[-1]
ending = i.split('python')[0]
elif 'c plus plus' in i:
lang = 'c plus plus'
begining = i.split('c plus plus')[-1]
ending = i.split('c plus plus')[0]
elif 'java' in i:
lang = 'java'
begining = i.split('java')[-1]
ending = i.split('java')[0]
elif 'javascript' in i:
lang = 'javascript'
begining = i.split('javascript')[-1]
ending = i.split('javascript')[0]
elif 'c#' in i:
lang = 'c#'
begining = i.split('c#')[-1]
ending = i.split('c#')[0]
elif 'assembly' in i:
lang = 'assembly'
begining = i.split('assembly')[-1]
ending = i.split('assembly')[0]
else:
pass
print lang, begining, ending
慕田峪9158850
阿晨1998
相关分类