如何使用Python检查两个列表中的两个相同值是否返回true
我有两个像素坐标列表如何使用Python检查两个列表中的两个相同值是否返回true,python,python-3.x,list,Python,Python 3.x,List,我有两个像素坐标列表 (确认像素[(60176),(60174),(63163),(61176)]& 白色像素[(64178),(60174),(61176)]) 我想比较两者,如果发现任何相同的值,比如(61176)和 (60174),它将返回True,这意味着至少需要匹配一个值 在这个if语句中如何实现这一点 已确认的_pixel==白色_pixel不起作用,因为两个列表中的所有值都必须相同才能返回true if confirmed_pixel == white_pixel and len
(确认像素[(60176),(60174),(63163),(61176)]&
白色像素[(64178),(60174),(61176)])
我想比较两者,如果发现任何相同的值,比如(61176)和
(60174),它将返回True,这意味着至少需要匹配一个值
在这个if语句中如何实现这一点
已确认的_pixel==白色_pixel不起作用,因为两个列表中的所有值都必须相同才能返回true
if confirmed_pixel == white_pixel and len(confirmed_pixel) != 0 and len(white_pixel) != 0:
print("True")
continue
用于此,这是有效测试交叉口的唯一方法:
confirmed = [(60, 176), (60, 174), (63, 163), (61, 176)]
white = [(64, 178), (60, 174), (61, 176)]
要到达十字路口:
print(set(confirmed).intersection(white))
# {(60, 174), (61, 176)}
confirmed = [(60, 176), (600, 174), (63, 163), (6100, 176)]
white = [(64, 178), (60, 174), (61, 176)]
print(set(confirmed).intersection(white))
# set()
print(bool(set(confirmed).intersection(white)))
# False
要获得True
或False
,只需将结果集强制转换为bool
:空集为False,非空集为True:
print(bool(set(confirmed).intersection(white)))
# True
另一个示例,使用空交叉点:
print(set(confirmed).intersection(white))
# {(60, 174), (61, 176)}
confirmed = [(60, 176), (600, 174), (63, 163), (6100, 176)]
white = [(64, 178), (60, 174), (61, 176)]
print(set(confirmed).intersection(white))
# set()
print(bool(set(confirmed).intersection(white)))
# False
这将为您完成预期的工作
if any([x==y for x in confirmed_pixel for y in white_pixel]):
return True
你可以通过一个循环来实现这一点。你有两个
元组的列表。使用循环
将第一个列表中的每个元组
与第二个列表中的所有其他元组
进行比较。在平等的情况下,您可以打印
True
。您好,Cytex,欢迎来到SO。使用google search很容易找到问题的重复项,因此我使用了以下方法:python检查两个列表是否与google query共享元素
。。。