Jonline
Jonline

Reputation: 1747

Most pythonic way to check if a 1-dimensional list is an element of a 2-dimensional list?

Using Python 2.7.6, I have a list of rgb colors, each themselves a list, ie.:

color_list = [ [0, 0, 0], [255, 0, 0]....[255, 255, 255] ]

Calling this:

color = [0, 0, 0]

if color in color_list:
    # do stuff

Elicits:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

I'm concerned that doing what the error suggests, ie. color.any() or color.all() is literally going to go looking for those integers anywhere in my color list. I can think up ways to achieve my actual aims, but my intuition is that Python has well-seen this need coming and there's a Pythonic way to achieve it. Lil' help?

UPDATE

I'm fail. color in the above code was a numpy.ndarray

Upvotes: 0

Views: 59

Answers (1)

shx2
shx2

Reputation: 64318

The error message you're seeing is coming from numpy.

This means that either color is a numpy array, or color_list is, or color_list is a list of numpy arrays. If all were lists, your code would have worked.

color_list = [ [0, 0, 0], [255, 0, 0], [255, 255, 255] ]
color = [0, 0, 0]
color in color_list
=> True

Upvotes: 3

Related Questions