Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中检查列表中是否存在任何值_Python_List - Fatal编程技术网

如何在python中检查列表中是否存在任何值

如何在python中检查列表中是否存在任何值,python,list,Python,List,我有这样的清单 l = ['dd','rr','abcde'] l2 = ['ddf','fdfd','123'] 我想要一个函数,如果l中的任何值存在于l2中,该函数将返回true 现在这也可以是部分匹配。我的意思是字符串应该出现在l2 编辑: 输出应为true或false 就像在我的示例中一样,它应该返回true,因为dd与ddf匹配,如果l中的任何值是l2中任何值的子字符串,则返回true: any(l_value in l2_value for l_value in l for l2

我有这样的清单

l = ['dd','rr','abcde']

l2 = ['ddf','fdfd','123']
我想要一个函数,如果
l
中的任何值存在于
l2
中,该函数将返回true

现在这也可以是部分匹配。我的意思是字符串应该出现在
l2

编辑:

输出应为true或false


就像在我的示例中一样,它应该返回true,因为
dd
ddf

匹配,如果
l
中的任何值是
l2
中任何值的子字符串,则返回
true

any(l_value in l2_value for l_value in l for l2_value in l2)
这将包括部分匹配

更新: 使用列表理解:

[re.search(x,",".join(l2)) for x in l if re.search(x,",".join(l2)) is not None] and 'True' or 'False'
嵌套循环:

print any(sub in full for sub in l for full in l2)
有效嵌套循环

from itertools import product
print any(sub in full for sub, full in product(l, l2))
无循环:

import re
print re.match('|'.join(l), ' '.join(l2))

您的示例的预期输出是什么?还有,到目前为止你都尝试了什么?这里已经回答了这个问题,这是一个很好的选择solution@cyroxx在最后一句中,输出应该是true或false。我的意思是,当l1中的
dd
ddf
匹配时,您的函数应该给出true,因为
产品的效率不高。所有这些元组的创建和解包加在一起。@PavelAnossov:efficient!=快速的
import re
print re.match('|'.join(l), ' '.join(l2))