Reputation: 43
In a python console (using 2.7) if I put in the following code:
vals = [1.2e-5, 1.5e-5, 3.2e-5, 4.5e-5]
for val in vals: print val < 0.001,
The output is True True True True
as expected.
But! Here is my problem, if I try all(vals) < 0.001
it returns false?
Is it the number formatting giving it problems or something else? If I do this again but replace the vals list with vals = [2,2,2,2]
and check for < 3
I get the desired output both ways!
EDIT Helpful answers, it is interesting to note that all([0.1, 0.1, 0.1]) evaluates to True, but 0.1 == True evaluates to False? What's up with this? Is it that a "nonzero" value will evaluate to True but is not actually "True"?
Upvotes: 2
Views: 57
Reputation: 198486
Your usage is wrong. all(x < 0.001 for x in vals)
should be okay.
all(vals) < 0.001
will check whether all vals
is truthy, then compare the True
or False
you get as the result with 0.001
, which is weird.
Upvotes: 2
Reputation: 251578
all(vals)
checks whether all the values are boolean True (i.e., nonzero). That is True. True is not less than 0.001
.
I think you want something like all(val < 0.001 for val in vals)
.
Upvotes: 2