我在这样的列表中有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
答案 0 :(得分:3)
您可以通过以下方式更有效地进行迭代:”
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section class="filter driedelig mw1">
<div class="filteritem">
<div class="filterselect">
<ul>
<li class="active">Categorie 1</li>
<li>Caetgorie 2</li>
<li>Categorie 3</li>
</ul>
<i class="fas fa-angle-down"></i>
</div>
</div>
</section>
答案 1 :(得分:1)
languages = [...]
sentences = [...]
for sentence in sentences:
for language in languages:
if language in sentence:
...