Python:检查两个列表之间的字符串是否部分匹配 [英] Python: Check for partial match of strings between two lists
本文介绍了Python:检查两个列表之间的字符串是否部分匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个列表,如下所示:
I have a two lists as shown below:
c = ['John', 'query 989877 forcast', 'Tamm']
isl = ['My name is Anne Query 989877', 'John', 'Tamm Ju']
我想检查 isl
中的每个项目以及 c
中的每个项目,以便获得所有的部分字符串匹配项.我需要的输出如下所示:
I want to check every item in isl
with every item in c
so that I get all my partial string matches.
The output that I need will look like the below:
out = ["john", "query 989877", "tamm"]
可以看出,我也得到了部分字符串匹配.
As can be seen I have gotten the partial string matches as well.
我尝试了以下方法:
out = []
for word in c:
for w in isl:
if word.lower() in w.lower():
out.append(word)
但这只会给我输出
out = ["John", "Tamm"]
我也尝试了以下方法:
print [word for word in c if word.lower() in (e.lower() for e in isl)]
但是这仅输出"John".我如何得到想要的东西?
But this outputs only "John". How do I get what I want?
推荐答案
也许是这样的:
def get_sub_strings(s):
words = s.split()
for i in xrange(1, len(words)+1): #reverse the order here
for n in xrange(0, len(words)+1-i):
yield ' '.join(words[n:n+i])
...
>>> out = []
>>> for word in c:
for sub in get_sub_strings(word.lower()):
for s in isl:
if sub in s.lower():
out.append(sub)
...
>>> out
['john', 'query', '989877', 'query 989877', 'tamm']
如果只想存储最大的匹配项,则需要以相反的顺序生成子字符串,并在 isl
中找到匹配项后立即中断:
If you want to store only the biggest match only then you need to generate the sub-strings in reverse order and break as soon a match is found in isl
:
def get_sub_strings(s):
words = s.split()
for i in xrange(len(words)+1, 0, -1):
for n in xrange(0, len(words)+1-i):
yield ' '.join(words[n:n+i])
out = []
for word in c:
for sub in get_sub_strings(word.lower()):
if any(sub in s.lower() for s in isl):
out.append(sub)
break
print out
#['john', 'query 989877', 'tamm']
这篇关于Python:检查两个列表之间的字符串是否部分匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文