By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,547 Members | 1,429 Online
Bytes IT Community
+ Ask a Question
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
Share this Question
Share on Google+
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

</F>

Jul 12 '06 #3

P: n/a
Ant

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 <jo**********@hotmail.comwrote:
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
Jul 13 '06 #6

P: n/a
"John Henry" <jo**********@hotmail.comwrites:
# 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))
Jul 13 '06 #7

P: n/a
On 12 Jul 2006 11:14:43 -0700, John Henry <jo**********@hotmail.comwrote:
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/
Jul 13 '06 #8

P: n/a

Simon Brunning wrote:
On 12 Jul 2006 11:14:43 -0700, John Henry <jo**********@hotmail.comwrote:
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.

--
Cheers,
Simon B,
si***@brunningonline.net,
http://www.brunningonline.net/simon/blog/
Jul 13 '06 #9

P: n/a
On 13 Jul 2006 05:45:21 -0700, John Henry <jo**********@hotmail.comwrote:
>
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 <jo**********@hotmail.comwrote:
>>
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)
<slaps foreheaddoh! Never mind.

Aug 5 '06 #16

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)

<slaps foreheaddoh! Never mind.
*grin*

Aug 5 '06 #17

P: n/a

"Janto Dreijer" <ja****@gmail.comwrote:
|
| 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 foreheaddoh! 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
Aug 7 '06 #18

This discussion thread is closed

Replies have been disabled for this discussion.