Py学习  »  Python

如何使用Python检查两个列表中的两个相同值是否返回true[duplicate]

Cytex • 5 年前 • 1545 次点击  

这个问题已经有了答案:

我有两个像素坐标列表

(confirmed pixel[(60, 176), (60, 174), (63, 163), (61, 176)] & 
white_pixel [(64, 178), (60, 174), (61, 176)])

我想比较两者,如果发现任何相同的值,例如 (61176) (60174) ,它将返回 真的 ,表示至少需要匹配一个值。

我怎么能这样 如果 陈述?

确认的像素=白色像素不起作用 全部的 两个列表中的值必须相同才能返回true

if confirmed_pixel == white_pixel and len(confirmed_pixel) != 0 and len(white_pixel) != 0:
    print("True")
    continue
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/50385
 
1545 次点击  
文章 [ 2 ]  |  最新文章 5 年前
Himanshu
Reply   •   1 楼
Himanshu    5 年前

这将为你做预期的工作

if any([x==y for x in confirmed_pixel for y in white_pixel]):
    return True
Thierry Lathuille
Reply   •   2 楼
Thierry Lathuille    5 年前

使用 sets 因此,这是有效测试交叉口的唯一方法。:

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)}

得到 True False ,只需将结果集转换为 bool :空集为False,非空集为True:

print(bool(set(confirmed).intersection(white)))
# True

另一个例子,有空交集:

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