testing array of logicals

J

John Henry

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
 
S

Stefan Behnel

John said:
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
 
F

Fredrik Lundh

John said:
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>
 
G

Gary Herron

John said:
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
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
 
A

Alex Martelli

John Henry said:
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

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
 
P

Paul Rubin

John Henry said:
# 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))
 
S

Simon Brunning

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. ;-)
 
S

Simon Brunning


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

Simon Forman

John said:
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

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

False not in logflags


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

H J van Rooyen

|
| 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)
|
| <slaps forehead> doh! Never mind.
|

*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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top