Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

testing array of logicals

0 views
Skip to first unread message

John Henry

unread,
Jul 12, 2006, 2:14:43 PM7/12/06
to
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,

Stefan Behnel

unread,
Jul 12, 2006, 2:29:00 PM7/12/06
to
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

Fredrik Lundh

unread,
Jul 12, 2006, 2:30:32 PM7/12/06
to pytho...@python.org
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

</F>

Ant

unread,
Jul 12, 2006, 2:35:50 PM7/12/06
to

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
>>>

Gary Herron

unread,
Jul 12, 2006, 2:41:18 PM7/12/06
to John Henry, pytho...@python.org
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


Alex Martelli

unread,
Jul 13, 2006, 12:34:05 AM7/13/06
to
John Henry <john10...@hotmail.com> wrote:

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

Paul Rubin

unread,
Jul 13, 2006, 1:46:34 AM7/13/06
to
"John Henry" <john10...@hotmail.com> writes:
> # logflags is an array of logicals
> test=True
> for x in logflags:
> test = test and x
> print test

print (False not in map(bool, logflags))

Possibly more "pure" alternative (untested):

from operator import and_
print reduce(and_, map(bool, logflags))

Simon Brunning

unread,
Jul 13, 2006, 5:19:12 AM7/13/06
to John Henry, pytho...@python.org
On 12 Jul 2006 11:14:43 -0700, John Henry <john10...@hotmail.com> 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

min(logflags)

I feel dirty now. ;-)

--
Cheers,
Simon B,
si...@brunningonline.net,
http://www.brunningonline.net/simon/blog/

John Henry

unread,
Jul 13, 2006, 8:45:21 AM7/13/06
to

Simon Brunning wrote:
> On 12 Jul 2006 11:14:43 -0700, John Henry <john10...@hotmail.com> 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
>
> min(logflags)
>

!!!


> I feel dirty now. ;-)
>

ROFL

Thanks to everybody that replied.

Simon Brunning

unread,
Jul 13, 2006, 9:44:22 AM7/13/06
to John Henry, pytho...@python.org
On 13 Jul 2006 05:45:21 -0700, John Henry <john10...@hotmail.com> wrote:
>
> 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.

Bruno Desthuilliers

unread,
Jul 13, 2006, 5:03:30 PM7/13/06
to
Simon Brunning a écrit :

> On 13 Jul 2006 05:45:21 -0700, John Henry <john10...@hotmail.com> wrote:
>
>>
>> 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 :(

Simon Forman

unread,
Jul 13, 2006, 9:43:35 PM7/13/06
to

So many ways.... *drool*
How about:

False not in logflags


(Anybody gonna run all these through timeit? ;P )

Simon Forman

unread,
Jul 13, 2006, 9:53:36 PM7/13/06
to
>
> False not in logflags
>

Or, if your values aren't already bools

False not in (bool(n) for n in logflags)

Peace,
~Simon

John Henry

unread,
Jul 13, 2006, 10:25:44 PM7/13/06
to

Very intriguing use of "not in"...

Thanks,

Janto Dreijer

unread,
Aug 5, 2006, 8:20:56 AM8/5/06
to
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)

Janto Dreijer

unread,
Aug 5, 2006, 8:24:47 AM8/5/06
to

<slaps forehead> doh! Never mind.

Simon Forman

unread,
Aug 5, 2006, 6:39:53 PM8/5/06
to

*grin*

H J van Rooyen

unread,
Aug 7, 2006, 2:38:21 AM8/7/06
to pytho...@python.org

"Janto Dreijer" <jan...@gmail.com> wrote:

*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


0 new messages