conditional expression sought

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Elaine Jackson

    conditional expression sought

    If bool(B_i)==True for 1<=i<=n and j is the smallest i with bool(A_j)==True ,
    then the evaluation of (A_1 and B_1) or ... or (A_n and B_n) returns B_j without
    evaluating any other B_i. This is such a useful mode of expression that I would
    like to be able to use something similar even when there is an i with
    bool(B_i)==Fals e. The only thing I can think of by myself is ( (A_1 and [B_1])
    or ... or (A_n and [B_n]) )[0], and I can't be satisfied with that for obvious
    reasons. Does anybody know a good way to express this? Any help will be mucho
    appreciado.

    Peace


  • Sidharth Kuruvila

    #2
    Re: conditional expression sought

    since True and False
    can also evaluate as 1 and 0
    you can use the binary operators
    | and &
    for or and and respectively
    [color=blue][color=green][color=darkred]
    >>> class F:[/color][/color][/color]
    .... a = 5
    .... def b(self):
    .... self.a = 4
    .... return True
    .... def a(self):
    .... self.a = 1
    .... return False
    ....[color=blue][color=green][color=darkred]
    >>> g = F()
    >>> g.a() & g.b()[/color][/color][/color]
    False[color=blue][color=green][color=darkred]
    >>> g.a[/color][/color][/color]
    4
    "Elaine Jackson" <elainejackson7 355@home.com> wrote in message
    news:pRbSb.3303 34$ts4.37644@pd 7tw3no...[color=blue]
    > If bool(B_i)==True for 1<=i<=n and j is the smallest i with[/color]
    bool(A_j)==True ,[color=blue]
    > then the evaluation of (A_1 and B_1) or ... or (A_n and B_n) returns B_j[/color]
    without[color=blue]
    > evaluating any other B_i. This is such a useful mode of expression that I[/color]
    would[color=blue]
    > like to be able to use something similar even when there is an i with
    > bool(B_i)==Fals e. The only thing I can think of by myself is ( (A_1 and[/color]
    [B_1])[color=blue]
    > or ... or (A_n and [B_n]) )[0], and I can't be satisfied with that for[/color]
    obvious[color=blue]
    > reasons. Does anybody know a good way to express this? Any help will be[/color]
    mucho[color=blue]
    > appreciado.
    >
    > Peace
    >
    >[/color]


    Comment

    • Mark McEahern

      #3
      Re: conditional expression sought

      Elaine Jackson wrote:
      [color=blue]
      >If bool(B_i)==True for 1<=i<=n and j is the smallest i with bool(A_j)==True ,
      >then the evaluation of (A_1 and B_1) or ... or (A_n and B_n) returns B_j without
      >evaluating any other B_i. This is such a useful mode of expression that I would
      >like to be able to use something similar even when there is an i with
      >bool(B_i)==Fal se. The only thing I can think of by myself is ( (A_1 and [B_1])
      >or ... or (A_n and [B_n]) )[0], and I can't be satisfied with that for obvious
      >reasons. Does anybody know a good way to express this? Any help will be mucho
      >appreciado.
      >[/color]
      Why not write a unit test that demonstrates the behavior you want?
      It'll then likely be obvious to someone both what your problem is and
      what a likely solution is.

      Cheers,

      // m

      Comment

      • wes weston

        #4
        ditto

        Preferably, a program that runs or a screen dump.

        Mark McEahern wrote:[color=blue]
        > Elaine Jackson wrote:
        >[color=green]
        >> If bool(B_i)==True for 1<=i<=n and j is the smallest i with
        >> bool(A_j)==True ,
        >> then the evaluation of (A_1 and B_1) or ... or (A_n and B_n) returns
        >> B_j without
        >> evaluating any other B_i. This is such a useful mode of expression
        >> that I would
        >> like to be able to use something similar even when there is an i with
        >> bool(B_i)==Fals e. The only thing I can think of by myself is ( (A_1
        >> and [B_1])
        >> or ... or (A_n and [B_n]) )[0], and I can't be satisfied with that for
        >> obvious
        >> reasons. Does anybody know a good way to express this? Any help will
        >> be mucho
        >> appreciado.
        >>[/color]
        > Why not write a unit test that demonstrates the behavior you want?
        > It'll then likely be obvious to someone both what your problem is and
        > what a likely solution is.
        >
        > Cheers,
        >
        > // m
        >[/color]

        Comment

        • Corey Coughlin

          #5
          Re: conditional expression sought

          "Elaine Jackson" <elainejackson7 355@home.com> wrote in message news:<pRbSb.330 334$ts4.37644@p d7tw3no>...[color=blue]
          > If bool(B_i)==True for 1<=i<=n and j is the smallest i with bool(A_j)==True ,
          > then the evaluation of (A_1 and B_1) or ... or (A_n and B_n) returns B_j without
          > evaluating any other B_i. This is such a useful mode of expression that I would
          > like to be able to use something similar even when there is an i with
          > bool(B_i)==Fals e. The only thing I can think of by myself is ( (A_1 and [B_1])
          > or ... or (A_n and [B_n]) )[0], and I can't be satisfied with that for obvious
          > reasons. Does anybody know a good way to express this? Any help will be mucho
          > appreciado.
          >
          > Peace[/color]

          Oh, this must be part of your truth table script. Interesting,
          looking for something like a fast SOP evaluator, or more like a
          function evaluation mechanism? It would probably be most useful to
          share your containers for A and B. Are you really going to have
          variables named A_1 to A_n, or will you just have a vector A[0:n]?
          The vector would probably be easier to deal with. Using integer
          representations for your boolean vectors is a good idea, and will
          probably buy you enough speed that you won't need a more serious form
          of short circuit evaluation, I imagine. Unless your vectors are very
          large indeed. Hmm...

          Comment

          • Dave K

            #6
            Re: conditional expression sought

            On Thu, 29 Jan 2004 17:58:45 GMT in comp.lang.pytho n, "Elaine Jackson"
            <elainejackson7 355@home.com> wrote:
            [color=blue]
            >If bool(B_i)==True for 1<=i<=n and j is the smallest i with bool(A_j)==True ,
            >then the evaluation of (A_1 and B_1) or ... or (A_n and B_n) returns B_j without
            >evaluating any other B_i. This is such a useful mode of expression that I would
            >like to be able to use something similar even when there is an i with
            >bool(B_i)==Fal se. The only thing I can think of by myself is ( (A_1 and [B_1])
            >or ... or (A_n and [B_n]) )[0], and I can't be satisfied with that for obvious
            >reasons. Does anybody know a good way to express this? Any help will be mucho
            >appreciado.
            >
            >Peace
            >[/color]

            I'm not sure if this is what you're looking for, but for i > a very
            small number, the zip function seems more appropriate:
            [color=blue][color=green][color=darkred]
            >>> def get_B_i(A, B):[/color][/color][/color]
            for a, b in zip(A, B):
            if a: return b
            return A[-1][color=blue][color=green][color=darkred]
            >>> print get_B_i([False, False, True, False], [0, 1, 2, 3])[/color][/color][/color]
            2[color=blue][color=green][color=darkred]
            >>> print get_B_i([False, False, False, False], [0, 1, 2, 3])[/color][/color][/color]
            False

            This has exactly the same effect provided that A and B are the same
            length, otherwise the return value by failure should be adjusted to
            whatever suits your purpose.

            If you really want to write a conditional expression out in full, I
            can't think of anything non-clumsy that would work for all possible
            values of B_i, so unfortunately can't help you there.

            Dave


            Comment

            • Elaine Jackson

              #7
              Re: conditional expression sought

              Sorry to take so long but I wasn't sure what a "unit test" was (the other guy's
              post clarified it). Tell me if this isn't what you're looking for:

              falsies=[0,0.0,[],(),{},'',None]
              truies=[49,3.14,[1,2,3],(4,5,6),{7:8,9 :10},'nonempty']

              def demo(A,B):
              print "If A is ",A
              print "and B is ",B
              print "then (A[0] and B[0]) or (A[1] and B[1]) or (A[2] and B[2]) = ",
              print (A[0] and B[0]) or (A[1] and B[1]) or (A[2] and B[2])

              A=[]
              from random import randint
              for i in range(3):
              A.append(bool(r andint(0,1)))
              B=truies[0:3]
              demo(A,B)

              A=[False,False,Fal se]
              B=falsies[0:3]
              demo(A,B)
              print "I would have liked this to be B[2] = ",B[2]


              "Mark McEahern" <mark@mceahern. com> wrote in message
              news:mailman.10 00.1075402284.1 2720.python-list@python.org ...
              | Elaine Jackson wrote:
              |
              | >If bool(B_i)==True for 1<=i<=n and j is the smallest i with bool(A_j)==True ,
              | >then the evaluation of (A_1 and B_1) or ... or (A_n and B_n) returns B_j
              without
              | >evaluating any other B_i. This is such a useful mode of expression that I
              would
              | >like to be able to use something similar even when there is an i with
              | >bool(B_i)==Fal se. The only thing I can think of by myself is ( (A_1 and
              [B_1])
              | >or ... or (A_n and [B_n]) )[0], and I can't be satisfied with that for
              obvious
              | >reasons. Does anybody know a good way to express this? Any help will be mucho
              | >appreciado.
              | >
              | Why not write a unit test that demonstrates the behavior you want?
              | It'll then likely be obvious to someone both what your problem is and
              | what a likely solution is.
              |
              | Cheers,
              |
              | // m
              |


              Comment

              • wes weston

                #8
                Re: conditional expression sought

                Elaine,
                The last code line:

                print "I would have liked this to be B[2] = ",B[2]

                prints the value of B[2]; the value you don't want.
                I think what you meant was that you want B[2] to be
                0.0 not false. bool(0.0) does equal false.
                ---------------------------------------------------
                The code:

                A.append(bool(r andint(0,1)))

                will always yield an A of [true,true,true]
                ---------------------------------------------------
                I didn't know this about python, and I'm not sure I
                like it:

                wes@linux:~/amy> python
                [color=blue][color=green][color=darkred]
                >>> print 1.1 and 2.2[/color][/color][/color]
                2.2[color=blue][color=green][color=darkred]
                >>> print 2.2 and 1.1[/color][/color][/color]
                1.1[color=blue][color=green][color=darkred]
                >>> print (1.1 and 2.2)[/color][/color][/color]
                2.2

                The order is important. To me, it should be printing true
                and not either number or the last number.

                Comment

                • Elaine Jackson

                  #9
                  Re: conditional expression sought

                  This is a good idea. Thanks for pointing it out.

                  "Dave K" <dk123456789@RE MOVEhotmail.com > wrote in message
                  news:866j101bbu uhtpan29eop9o4m k1mt7nckn@4ax.c om...
                  | On Thu, 29 Jan 2004 17:58:45 GMT in comp.lang.pytho n, "Elaine Jackson"
                  | <elainejackson7 355@home.com> wrote:
                  |
                  | >If bool(B_i)==True for 1<=i<=n and j is the smallest i with bool(A_j)==True ,
                  | >then the evaluation of (A_1 and B_1) or ... or (A_n and B_n) returns B_j
                  without
                  | >evaluating any other B_i. This is such a useful mode of expression that I
                  would
                  | >like to be able to use something similar even when there is an i with
                  | >bool(B_i)==Fal se. The only thing I can think of by myself is ( (A_1 and
                  [B_1])
                  | >or ... or (A_n and [B_n]) )[0], and I can't be satisfied with that for
                  obvious
                  | >reasons. Does anybody know a good way to express this? Any help will be mucho
                  | >appreciado.
                  | >
                  | >Peace
                  | >
                  |
                  | I'm not sure if this is what you're looking for, but for i > a very
                  | small number, the zip function seems more appropriate:
                  |
                  | >>> def get_B_i(A, B):
                  | for a, b in zip(A, B):
                  | if a: return b
                  | return A[-1]
                  | >>> print get_B_i([False, False, True, False], [0, 1, 2, 3])
                  | 2
                  | >>> print get_B_i([False, False, False, False], [0, 1, 2, 3])
                  | False
                  |
                  | This has exactly the same effect provided that A and B are the same
                  | length, otherwise the return value by failure should be adjusted to
                  | whatever suits your purpose.
                  |
                  | If you really want to write a conditional expression out in full, I
                  | can't think of anything non-clumsy that would work for all possible
                  | values of B_i, so unfortunately can't help you there.
                  |
                  | Dave
                  |
                  |


                  Comment

                  • Elaine Jackson

                    #10
                    Re: conditional expression sought

                    This is just for theoretical interest. The A_i's and B_i's were meant as
                    metavariables.

                    "Corey Coughlin" <corey.coughlin @attbi.com> wrote in message
                    news:a8623416.0 401291625.594a9 768@posting.goo gle.com...
                    | "Elaine Jackson" <elainejackson7 355@home.com> wrote in message
                    news:<pRbSb.330 334$ts4.37644@p d7tw3no>...
                    | > If bool(B_i)==True for 1<=i<=n and j is the smallest i with bool(A_j)==True ,
                    | > then the evaluation of (A_1 and B_1) or ... or (A_n and B_n) returns B_j
                    without
                    | > evaluating any other B_i. This is such a useful mode of expression that I
                    would
                    | > like to be able to use something similar even when there is an i with
                    | > bool(B_i)==Fals e. The only thing I can think of by myself is ( (A_1 and
                    [B_1])
                    | > or ... or (A_n and [B_n]) )[0], and I can't be satisfied with that for
                    obvious
                    | > reasons. Does anybody know a good way to express this? Any help will be
                    mucho
                    | > appreciado.
                    | >
                    | > Peace
                    |
                    | Oh, this must be part of your truth table script. Interesting,
                    | looking for something like a fast SOP evaluator, or more like a
                    | function evaluation mechanism? It would probably be most useful to
                    | share your containers for A and B. Are you really going to have
                    | variables named A_1 to A_n, or will you just have a vector A[0:n]?
                    | The vector would probably be easier to deal with. Using integer
                    | representations for your boolean vectors is a good idea, and will
                    | probably buy you enough speed that you won't need a more serious form
                    | of short circuit evaluation, I imagine. Unless your vectors are very
                    | large indeed. Hmm...


                    Comment

                    • Elaine Jackson

                      #11
                      Re: conditional expression sought

                      Thanks for your help. I just looked at some values of random.randint( 0,1) in my
                      interpreter, and one of them was 0. Getting nonboolean values from conjunction
                      and disjunction allows conditional evaluation of expressions; for example:
                      reciprocal = lambda x: (x!=0 and 1.0/x) or (x==0 and "undefined" ).

                      "wes weston" <wweston@att.ne t> wrote in message
                      news:6PjSb.1384 87$6y6.2697536@ bgtnsc05-news.ops.worldn et.att.net...
                      | Elaine,
                      | The last code line:
                      |
                      | print "I would have liked this to be B[2] = ",B[2]
                      |
                      | prints the value of B[2]; the value you don't want.
                      | I think what you meant was that you want B[2] to be
                      | 0.0 not false. bool(0.0) does equal false.
                      | ---------------------------------------------------
                      | The code:
                      |
                      | A.append(bool(r andint(0,1)))
                      |
                      | will always yield an A of [true,true,true]
                      | ---------------------------------------------------
                      | I didn't know this about python, and I'm not sure I
                      | like it:
                      |
                      | wes@linux:~/amy> python
                      |
                      | >>> print 1.1 and 2.2
                      | 2.2
                      | >>> print 2.2 and 1.1
                      | 1.1
                      | >>> print (1.1 and 2.2)
                      | 2.2
                      |
                      | The order is important. To me, it should be printing true
                      | and not either number or the last number.
                      |


                      Comment

                      • wes weston

                        #12
                        Re: conditional expression sought

                        oops;

                        The code:

                        A.append(bool(r andint(0,1)))

                        will always yield an A of [true,true,true]


                        WRONG; was thinking it was a rand float;.


                        Thanks for replying to all who tried to help.

                        wes



                        wes weston wrote:[color=blue]
                        > Elaine,
                        > The last code line:
                        >
                        > print "I would have liked this to be B[2] = ",B[2]
                        >
                        > prints the value of B[2]; the value you don't want.
                        > I think what you meant was that you want B[2] to be
                        > 0.0 not false. bool(0.0) does equal false.
                        > ---------------------------------------------------
                        > The code:
                        >
                        > A.append(bool(r andint(0,1)))
                        >
                        > will always yield an A of [true,true,true]
                        > ---------------------------------------------------
                        > I didn't know this about python, and I'm not sure I
                        > like it:
                        >
                        > wes@linux:~/amy> python
                        >[color=green][color=darkred]
                        > >>> print 1.1 and 2.2[/color][/color]
                        > 2.2[color=green][color=darkred]
                        > >>> print 2.2 and 1.1[/color][/color]
                        > 1.1[color=green][color=darkred]
                        > >>> print (1.1 and 2.2)[/color][/color]
                        > 2.2
                        >
                        > The order is important. To me, it should be printing true
                        > and not either number or the last number.
                        >[/color]

                        Comment

                        Working...