简体   繁体   中英

Matching strings when comparing two lists in Python

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...

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))

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM