0

I am trying to match strings that are similar from two lists and the result to be True if there is a match. So far I am getting only False tried with comprehension and set interesection the result was the same.

What I have right now:

a = ['The weather today is awful', 'Last night I had a bad dream']

b = ['The weather today is awful and tomorow it will be probably the same', 'Last night I had a bad dream about aliens']

match = any([item in a for item in b])
print(match)

So what I am trying to do is to match The weather today is awful from list a with the sentence of list b and Last night I had a bad dream with the sentence of list b and so on...

1
  • Have a look at Levenshtein distance or other "string distance" metrics. There are some libraries for python available. Could you provide some other examples of "strings that are similar"? Commented Sep 11, 2020 at 8:42

4 Answers 4

2

You need something like:

match = any(ia in ib for ia in a for ib in b)

Or, using itertools.product:

from itertools import product

match = any(ia in ib for ia, ib in product(a, b))
Sign up to request clarification or add additional context in comments.

Comments

1

You're still comparing full strings (and backwards), by checking if any item in b, is in the list a, not the strings within a

any(item in x for x in b for item in a)

I presume you want to check if any string in a, is within a string in the list b

Comments

1

If you want to match each item in a against any item in b, then you can do:

[any(item in item2 for item2 in b) for item in a]

If you want to match each item in a against only the item in b at the corresponding index, then you can do:

[item in item2 for item, item2 in zip(a,b)]

Both of these return [True, True] with the current example:

a = ['The weather today is awful', 'Last night I had a bad dream']

b = ['The weather today is awful and tomorow it will be probably the same', 'Last night I had a bad dream about aliens']

but if for example you reversed the ordering of b:

b = b[::-1]

then the first expression wouold still return [True, True], whereas the second one would now return [False, False] -- in other words, the first element of a is now contained in an element of b but not the first one, and similarly the second element of a is now contained in an element of b but not the second one.

If you are just interested in whether any item in a is contained in any item in b, or the corresponding item in b, then use these list comprehensions (or better, the analogous generator expression) as input to any. For example:

any(any(item in item2 for item2 in b) for item in a)

tests if any item in a is contained in any item in b

or

any(item in item2 for item, item2 in zip(a,b))

tests if any item in a is contained in the corresponding item in b

Comments

0

you can use any() with startswith() to achieve the reult without importing anything:

a = ['The weather today is awful', 'Last night I had a bad dream']

b = ['The weather today is awful and tomorow it will be probably the same', 'Last night I had a bad dream about aliens']

print(any([item2.startswith(item1) for item1, item2 in zip(a, b)])) # True

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.