How to check of the existence of any value in the list in python
我有一张单子
1 2 3 | l = ['dd','rr','abcde'] l2 = ['ddf','fdfd','123'] |
我想要一个函数,如果
现在也可以部分匹配了。我的意思是绳子应该出现在
编辑:
输出应为"真"或"假"
在我的例子中,它应该返回true,因为
如果
1 | any(l_value in l2_value for l_value in l for l2_value in l2) |
嵌套循环:
1 | print any(sub in full for sub in l for full in l2) |
高效嵌套循环
1 2 | from itertools import product print any(sub in full for sub, full in product(l, l2)) |
没有循环:
1 2 | import re print re.match('|'.join(l), ' '.join(l2)) |
1 2 3 4 5 6 7 | def match(): for e in l: for e2 in l2: if e in e2: return True else: return False |
这将包括部分匹配。
更新:使用列表理解:
1 | [re.search(x,",".join(l2)) for x in l if re.search(x,",".join(l2)) is not None] and 'True' or 'False' |