455,547 Members | 1,429 Online
Need help? Post your question and get tips & solutions from a community of 455,547 IT Pros & Developers. It's quick & easy.

# testing array of logicals

 P: n/a Hi list, 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, Jul 12 '06 #1
17 Replies

 P: n/a John Henry wrote: 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 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 Jul 12 '06 #2

 P: n/a John Henry wrote: 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 Jul 12 '06 #3

 P: n/a John Henry wrote: Hi list, 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 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 >>> Jul 12 '06 #4

 P: n/a John Henry wrote: Hi list, 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, The builtin "reduce" does that kind of thing for any function you wish to apply across the list. So then its only a matter of giving it a function that "and"s two arguments: Either: reduce(lambda a,b: a and b, logFlags) or def and2(a,b): return a and b reduce(and2, logFlags) Gary Herron Jul 12 '06 #5

 P: n/a John Henry

 P: n/a "John Henry"

 P: n/a On 12 Jul 2006 11:14:43 -0700, John Henry

 P: n/a Simon Brunning wrote: On 12 Jul 2006 11:14:43 -0700, John Henry

 P: n/a On 13 Jul 2006 05:45:21 -0700, John Henry Simon Brunning wrote: min(logflags) !!! 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. -- Cheers, Simon B, si***@brunningonline.net, http://www.brunningonline.net/simon/blog/ Jul 13 '06 #10

 P: n/a Simon Brunning a écrit : On 13 Jul 2006 05:45:21 -0700, John Henry >Simon Brunning wrote: > min(logflags) !!! Be aware that not only is this an outrageous misuse of min(), +1 QOTW Ho, my, I've already proposed another one today :( Jul 13 '06 #11

 P: n/a John Henry wrote: Hi list, 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, So many ways.... *drool* How about: False not in logflags (Anybody gonna run all these through timeit? ;P ) Jul 14 '06 #12

 P: n/a > False not in logflags Or, if your values aren't already bools False not in (bool(n) for n in logflags) Peace, ~Simon Jul 14 '06 #13

 P: n/a Simon Forman wrote: False not in logflags 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, Jul 14 '06 #14

 P: n/a John Henry wrote: Simon Forman wrote: > False not in logflags > Or, if your values aren't already bools False not in (bool(n) for n in logflags) Very intriguing use of "not in"... Is there a reason why you didn't write True in (bool(n) for n in logflags) Aug 5 '06 #15

 P: n/a Janto Dreijer wrote: John Henry wrote: Simon Forman wrote: False not in logflags > Or, if your values aren't already bools > False not in (bool(n) for n in logflags) Very intriguing use of "not in"... Is there a reason why you didn't write True in (bool(n) for n in logflags)

 P: n/a Janto Dreijer wrote: Janto Dreijer wrote: John Henry wrote: Simon Forman wrote: > False not in logflags > Or, if your values aren't already bools False not in (bool(n) for n in logflags) > Very intriguing use of "not in"... Is there a reason why you didn't write True in (bool(n) for n in logflags)

 P: n/a "Janto Dreijer" | Or, if your values aren't already bools | > | False not in (bool(n) for n in logflags) | | Very intriguing use of "not in"... | > | Is there a reason why you didn't write | True in (bool(n) for n in logflags) | | 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 Aug 7 '06 #18

### This discussion thread is closed

Replies have been disabled for this discussion.