简体   繁体   English

在 Python 中比较两个列表时匹配字符串

[英]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.我试图匹配两个列表中相似的字符串,如果匹配,结果为True So far I am getting only False tried with comprehension and set interesection the result was the same.到目前为止,我只得到了False理解和设置 interesection 结果是一样的。

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...所以我想要做的是将列表a The weather today is awful与列表b的句子相匹配The weather today is awfulLast night I had a bad dream带有列表b句子的b ,依此类推...

You need something like:你需要这样的东西:

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

Or, using itertools.product :或者,使用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您仍在比较完整的字符串(和向后),通过检查 b 中的任何项目是否在列表 a 中,而不是在 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我想您想检查 a 中的任何字符串是否在列表 b 中的字符串内

If you want to match each item in a against any item in b, then you can do:如果你想在每个项目中对B中的任何项目匹配,那么你可以这样做:

[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:如果您只想将 a 中的每个项目与相应索引处的 b 中的项目进行匹配,则可以执行以下操作:

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

Both of these return [True, True] with the current example:在当前示例中[True, True]这两个都返回[True, True]

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 = 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.然后第一个表达式wouold仍然返回[True, True]而第二个现在将返回[False, False] -换句话说,第一元件a现在已包含在元素b ,但不是第一个,类似地, a的第二个元素现在包含在b元素中,但不包含在第二个元素中。

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 .如果你只是有兴趣在任何项目是否a被包含在任何项目b ,或者在相应的项目b ,然后使用这些列表理解(或更好,类似的发电机表达式)作为输入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如果任何项目测试a被包含在任何项目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测试a任何项目是否包含在b中的相应项目中

you can use any() with startswith() to achieve the reult without importing anything:您可以使用any()startswith()来实现结果,而无需导入任何内容:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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