Is there a more elagant way of doing this?
# logflags is an array of logicals
test=True
for x in logflags:
test = test and x
print test
--
Thanks,
Py2.5:
test = all( logflags )
Py2.4 (although somewhat ugly):
try:
test = itertools.ifilterfalse( logflags ).next()
except StopIteration:
test = True
otherwise: your above code will do just fine. Note that you can shortcut,
though, if any of the flags evaluates to False:
test = True
for x in logflags:
if not x:
test = False
break
Stefan
> Is there a more elagant way of doing this?
>
> # logflags is an array of logicals
> test=True
> for x in logflags:
> test = test and x
> print test
your code checks all members, even if the first one's false. that's not
very elegant. here's a better way to do it:
def all(S):
for x in S:
if not x:
return False
return True
print all(logfiles)
if you upgrade to 2.5, you can get rid of the function definition; "all"
is a built-in in 2.5.
also see:
http://www.artima.com/weblogs/viewpost.jsp?thread=98196
</F>
There's reduce, but it's not as explicit, and see F's post RE
efficiency:
>>> x = [True, True, True]
>>> y = [True, False, True]
>>> print reduce(lambda a, b: a and b, x)
True
>>> print reduce(lambda a, b: a and b, y)
False
>>>
Either:
reduce(lambda a,b: a and b, logFlags)
or
def and2(a,b):
return a and b
reduce(and2, logFlags)
Gary Herron
test = sum(bool(x) for x in logflags)==len(logflags)
is yet another possibility (without the effectiveness of
shortcircuiting, just like the quoted approach). Some might prefer
test = not sum(not x for x in logflags)
but that's starting to border on the obscure;-).
If by "logicals" you mean "bool" instances (True and False) only, then
test = sum(logflags) == len(logflags)
is simpler and fast than, but equivalent to, my first suggestion.
Alex
print (False not in map(bool, logflags))
Possibly more "pure" alternative (untested):
from operator import and_
print reduce(and_, map(bool, logflags))
min(logflags)
I feel dirty now. ;-)
--
Cheers,
Simon B,
si...@brunningonline.net,
http://www.brunningonline.net/simon/blog/
!!!
> I feel dirty now. ;-)
>
ROFL
Thanks to everybody that replied.
Be aware that not only is this an outrageous misuse of min(), it's
also almost certainly much less efficient than /F's suggestion, 'cos
it always iterates through the entire list.
+1 QOTW
Ho, my, I've already proposed another one today :(
So many ways.... *drool*
How about:
False not in logflags
(Anybody gonna run all these through timeit? ;P )
Or, if your values aren't already bools
False not in (bool(n) for n in logflags)
Peace,
~Simon
Very intriguing use of "not in"...
Thanks,
Is there a reason why you didn't write
True in (bool(n) for n in logflags)
<slaps forehead> doh! Never mind.
*grin*
*lol* - don't feel bad about this - real programmers make this mistake with a
varying frequency -
>From once every six days or so if you are no good, to once in a lifetime if
you are brilliant, and never only if you are a genius...
First time it bit me I was an apprentice writing in Cobol.
- Hendrik