2个列表上的字符串比较

时间:2013-02-17 13:10:46

标签: python string list

我有2个清单。

a = ['this;mango', 'is:red', 'test;cat']
b = ['man', 'is', 'can']

我想迭代b的每个元素并检查列表a中是否存在该字符串。我怎样才能做到这一点 ?

以上示例输出将为:

# is

6 个答案:

答案 0 :(得分:2)

a = ['this;mango', 'is:red', 'test;cat']
b = ['man', 'is', 'can']

import re

for elem in b:
    for test in a:
        if re.search(r'\b' + re.escape(elem) + r'\b', test):
            print elem

使用列表理解:

>>> a = ['this;mango', 'is:red', 'test;cat']
>>> b = ['man', 'is', 'can']
>>> 
>>> import re
>>> [elem for elem in b if any(re.search(r'\b' + re.escape(elem) + r'\b', test) for test in a)]
['is']

使用maplambda的另一个版本,以避免explicit循环a

>>> [elem for elem in b if any(map(lambda test: re.search(r'\b' + re.escape(elem) + r'\b', test), a))]
['is']

答案 1 :(得分:1)

In [13]: words = set(reduce(operator.add, (re.split(r'[:;]', s) for s in a)))

In [14]: words
Out[14]: set(['this', 'is', 'cat', 'mango', 'test', 'red'])

In [15]: [w for w in b if w in words]
Out[15]: ['is']

答案 2 :(得分:1)

我认为你必须在a中指定你的分隔符,否则也应该找到“man”。

>>> a = ['this;mango', 'is:red', 'test;cat']
>>> b = ['man', 'is', 'can']
>>> [elem for elem in b if elem in " ".join(a)]
['man', 'is']

答案 3 :(得分:1)

采用@ NPE的解决方案并希望非单词字符\W没问题。

In [221]: a = ['this;mango', 'is:red', 'test;cat']

In [222]: right = set(reduce(operator.add, list(re.split(r'[\W]', s) for s in a)))

In [223]: right
Out[223]: set(['this', 'is', 'cat', 'mango', 'test', 'red'])

In [224]: left = set(['man', 'is', 'can'])

In [225]: left
Out[225]: set(['is', 'can', 'man'])

In [226]: left &  right
Out[226]: set(['is'])

答案 4 :(得分:1)

这个怎么样?

[i for i in b if any(i in re.split(r'\W', j) for j in a)]

答案 5 :(得分:1)

您可以组合b的元素以避免第二个循环:

import re

a = ['this;mango', 'is:red', 'test;cat']
b = ['man', 'is', 'can']


def words_in_strings(words, strings):
    pat = re.compile('|'.join(words))
    for i in strings:
        m = pat.search(i)
        if m:
            yield m.group(0)

for i in words_in_strings(b, a):
    print i