Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Comparing values in nested list

I have checked around and cannot find the easiest answer to this.

I have a nested list of items in python.

[['screens\\achieve.png', 378, 40, 194, 198, 234],
 ['screens\\test.png', 378, 40, 1, 8, 15],
 ['screens\\cutout.png', 378, 40, 4, 8, 14],
 ['screens\\sample.png', 378, 40, 1, 6, 12]]

These are pixel colours from images and I have around 60 images in the list, this is a snip of it.
Image name, x coord, y coord, red, green, blue values

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

What my tool does is display an image. I click a location in the image and it loops through the folder full of images and outputs the colour values at the location I clicked in the viewed image.

Now what I want to do is look through the list of colour values to see if any other image in the list has the same colour at the same location.

I know the record im checking. From the snippet above, lets say im viewing test.png (item[1]) in the list. I need to loop through the other items in the list to see if the location I clicked in test.png is unique in colour to any other item.

Thanks in advance.

>Solution :

If I understand you correctly, you want to check the color of image at specific index against all other images:

lst = [
    ["screens\\achieve.png", 378, 40, 194, 198, 234],
    ["screens\\test.png", 378, 40, 1, 8, 15],
    ["screens\\cutout.png", 378, 40, 4, 8, 14],
    ["screens\\sample.png", 378, 40, 1, 6, 12],
]


def check(lst, test_idx):
    *_, r, g, b = lst[test_idx]

    return any(
        (r, g, b) == (tr, tg, tb)
        for i, (*_, tr, tg, tb) in enumerate(lst)
        if i != test_idx
    )


print(check(lst, 1))

Prints:

False

If the list is:

lst = [
    ["screens\\achieve.png", 378, 40, 1, 8, 15],
    ["screens\\test.png", 378, 40, 1, 8, 15],
]

Then:

print(check(lst, 1))

Prints:

True
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading