-1

I have 2 lists of pixel coordinates

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

I want to compare them both, and if any same values are found such as (61, 176) and (60, 174), it will return True, meaning at least one value needs to match.

How can I do this in this if statement?

confirmed_pixel == white_pixel doesn't work as all the values in both lists need to be the same for it to return true

if confirmed_pixel == white_pixel and len(confirmed_pixel) != 0 and len(white_pixel) != 0:
    print("True")
    continue
Jack Fleeting
  • 24,385
  • 6
  • 23
  • 45
Cytex
  • 23
  • 2
  • You could do this with a loop. – zomega Aug 10 '19 at 08:21
  • You have two `list` of `tuples`. Compare each `tuple` from the 1st `list` with all other `tuples` from the second `list` using `loop`. You can `print` `True` in case of equality. – idkman Aug 10 '19 at 08:22
  • Hello Cytex, welcome to SO. It is easy to find duplicates of your problem on SO using google search - I used this: `python check if two lists share elements` as google query... – Patrick Artner Aug 10 '19 at 08:30

2 Answers2

2

Use sets for that, that's the only way to test intersections efficiently. :

confirmed = [(60, 176), (60, 174), (63, 163), (61, 176)]
white = [(64, 178), (60, 174), (61, 176)]

To get the intersection:

print(set(confirmed).intersection(white))
# {(60, 174), (61, 176)}

To get True or False, just cast the resulting set to a bool: empty sets are False, non-empty ones will be True:

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

Another example, with empty intersection:

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
Thierry Lathuille
  • 23,663
  • 10
  • 44
  • 50
0

This will do the expected work for you

if any([x==y for x in confirmed_pixel for y in white_pixel]):
    return True
Himanshu
  • 666
  • 1
  • 8
  • 18