testing array of logicals

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Henry

    #1

    testing array of logicals

    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

    #2
    Re: testing array of logicals

    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.ifilt erfalse( 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

    Comment

    • Fredrik Lundh

      #3
      Re: testing array of logicals

      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:



      </F>

      Comment

      • Ant

        #4
        Re: testing array of logicals


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

        Comment

        • Gary Herron

          #5
          Re: testing array of logicals

          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


          Comment

          • Alex Martelli

            #6
            Re: testing array of logicals

            John Henry <john106henry@h otmail.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

            Comment

            • Paul Rubin

              #7
              Re: testing array of logicals

              "John Henry" <john106henry@h otmail.comwrite s:
              # 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))

              Comment

              • Simon Brunning

                #8
                Re: testing array of logicals

                On 12 Jul 2006 11:14:43 -0700, John Henry <john106henry@h otmail.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,
                simon@brunningo nline.net,

                Comment

                • John Henry

                  #9
                  Re: testing array of logicals


                  Simon Brunning wrote:
                  On 12 Jul 2006 11:14:43 -0700, John Henry <john106henry@h otmail.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.

                  Comment

                  • Simon Brunning

                    #10
                    Re: testing array of logicals

                    On 13 Jul 2006 05:45:21 -0700, John Henry <john106henry@h otmail.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,
                    simon@brunningo nline.net,

                    Comment

                    • Bruno Desthuilliers

                      #11
                      Re: testing array of logicals

                      Simon Brunning a écrit :
                      On 13 Jul 2006 05:45:21 -0700, John Henry <john106henry@h otmail.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 :(

                      Comment

                      • Simon Forman

                        #12
                        Re: testing array of logicals


                        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 )

                        Comment

                        • Simon Forman

                          #13
                          Re: testing array of logicals

                          >
                          False not in logflags
                          >
                          Or, if your values aren't already bools

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



                          Peace,
                          ~Simon

                          Comment

                          • John Henry

                            #14
                            Re: testing array of logicals


                            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,

                            Comment

                            • Janto Dreijer

                              #15
                              Re: testing array of logicals

                              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)

                              Comment

                              Working...