a = ['http://www.yahoo.com', 'http://www.google.net', 'gorilla', 'house.com', 'i love the net very much']
b = ['com', 'http', 'net']
result = ['http://www.yahoo.com', 'http://www.google.net', 'house.com', 'i love the net very much']
我怎样才能找到b? 匹配a中的任何一个。 a是长句
我的正则表达式
for element in a:
m = re.match("anything match in b right?")
if m:
print (m.group())
我不太确定在re.match里面放什么
答案 0 :(得分:3)
a = ['http://www.yahoo.com', 'http://www.google.net', 'gorilla', 'house.com', 'i love the net very much']
b = ['com', 'http', 'net']
print list(set([i for i in a for j in b if j in i]))
编辑:
对于a中的所有b:
a = ['http://www.yahoo.com', 'http://www.google.net', 'gorilla', 'house.com', 'i love the net very much http and com too']
b = ['com', 'http', 'net']
print set(a)-set([i for i in a for j in b if j not in i])
答案 1 :(得分:2)
不是正则表达式解决方案,但我认为您可以迭代每个字符串,并且可以使用any()
进行检查。示例 -
result = []
for i in a:
if any(x in i for x in b):
result.append(i)
示例/演示 -
>>> a = ['http://www.yahoo.com', 'http://www.google.net', 'gorilla', 'house.com', 'i love the net very much']
>>> b = ['com', 'http', 'net']
>>>
>>> result = []
>>> for i in a:
... if any(x in i for x in b):
... result.append(i)
...
>>> result
['http://www.yahoo.com', 'http://www.google.net', 'house.com', 'i love the net very much']
列表理解解决方案 -
result = [i for i in a if any(x in i for x in b)]
示例/演示 -
>>> a = ['http://www.yahoo.com', 'http://www.google.net', 'gorilla', 'house.com', 'i love the net very much']
>>> b = ['com', 'http', 'net']
>>> result = [i for i in a if any(x in i for x in b)]
>>>
>>> result
['http://www.yahoo.com', 'http://www.google.net', 'house.com', 'i love the net very much']
答案 2 :(得分:0)
使用嵌套循环,使用b。
中的每一个使用find()函数检查每个循环if b[x].find(a[y]) != -1:
此if语句将指示是否在b [x]中找到[y]。