Reputation: 5873
I have a look which looks like the following:
yyy=[(2.0, 3.4, 3.75), (2.0, 3.4, 0.0), (2.0, 3.4, 0.0), (2.0, 3.4, 3.5), (2.0, 3.4, 0.0)]
What I would like to do is identify if 0.0 is present in any of the sublists (true or false). So, I follow itertools, but I am unsure how the logic should be constructed.
from itertools import *
selectors = [x is 0 for x in yyy]
#[False, False, False, False, False]
Obviously, my above sytax does not seem right - I was wondering if someone could point me in the right direction for the syntax.
Upvotes: 1
Views: 125
Reputation: 3820
Try [0.0 in x for x in yyy]
>>> yyy=[(2.0, 3.4, 3.75), (2.0, 3.4, 0.0), (2.0, 3.4, 0.0), (2.0, 3.4, 3.5), (2.0, 3.4, 0.0)]
>>> [0.0 in x for x in yyy]
[False, True, True, False, True]
>>>
You were close.
Upvotes: 5
Reputation: 310049
what about just:
any(0.0 in subtup for subtup in yyy)
demo:
>>> yyy=[(2.0, 3.4, 3.75), (2.0, 3.4, 0.0), (2.0, 3.4, 0.0), (2.0, 3.4, 3.5), (2.0, 3.4, 0.0)]
>>> any(0.0 in subtup for subtup in yyy)
True
>>> yyy=[(2.0, 3.4, 3.75), (2.0, 3.4, 10.0), (2.0, 3.4, 10.0), (2.0, 3.4, 3.5), (2.0, 3.4,10.0)]
>>> any(0.0 in subtup for subtup in yyy)
False
Upvotes: 1