[英]How to search for a word in a sentence and give sentence as output
I have two lists:我有两个清单:
list1 = ["Name of company", "Banking", "JP Morgan", "Citibank", "Goldman Sachs", "Bank of America", "Big 4", "KPMG", "Deloitte (US GAAP)","EY (IDT)",
"Software/IT", "Infosys", "FMCG", "P & G", "Pidilite Indutries", "Indigo Paints", "Divi Labs"]
list2 = ["divi", "GAAP", "Morgan"]
Now I want Python to search list2 items in list1 and give the output as the sentence in which the item is found.现在我想让 Python 在 list1 中搜索 list2 项目,并给出 output 作为找到该项目的句子。
For example, the output of the above thing should be ["Divi labs", "Deloitte (US GAAP)", "JP Morgan")比如上面这个东西的output应该是["Divi labs", "Deloitte (US GAAP)", "JP Morgan")
PS: ignore case differences, i can solve it through.lower() PS:忽略大小写差异,我可以通过.lower()解决
The actual list1 contains over 1000 sentences and actual list2 contains over 100 words.实际 list1 包含 1000 多个句子,实际 list2 包含 100 多个单词。 The above is just an example.
以上只是一个例子。
I have tried the following:我尝试了以下方法:
For x in list1:
if x in list2:
print (x)
But you know it won't work.但你知道这行不通。
Iterate over list2
and check if the element in present in each word of list1
.遍历
list2
并检查list1
的每个单词中是否存在元素。
[y for y in list1 for x in list2 if x.lower() in y.lower()]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.