Python: Check for partial match of strings between two lists

list, python, string

Solution

Perhaps something like this:

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']

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']

Problem

I have a two lists as shown below: ``` c = ['John', 'query 989877 forcast', 'Tamm'] isl = ['My name is Anne Query 989877', 'John', 'Tamm Ju'] ``` 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. I have tried the below: ``` out = [] for word in c: for w in isl: if word.lower() in w.lower(): out.append(word) ``` But this only gives me the output as ``` out = ["John", "Tamm"] ``` I have also tried the below: ``` print [word for word in c if word.lower() in (e.lower() for e in isl)] ``` But this outputs only "John". How do I get what I want?

Original source