Multiple equates

Discussion in 'Python' started by jzakiya, Nov 17, 2008.

  1. jzakiya

    jzakiya Guest

    I looked online and in books, but couldn't find a definitive answer to
    this.

    I have an array and set multiple elements to either True or False at
    one time.

    Question: Which way is faster (or does it matter)?

    1)

    array[x1]=array[x2]=........= array[x10] = \
    array[x11]=array[x12]=... = array[x20] = \
    .......
    .......
    array[x40]=array[x41]=....= array[x50] = False (or True)


    2)

    array[x1]=array[x2]=........= array[x10] = False
    array[x11]=array[x12]=... = array[x20] = False
    .......
    .......
    array[x40]=array[x41]=....= array[x50] = False
    jzakiya, Nov 17, 2008
    #1
    1. Advertising

  2. jzakiya <> writes:

    > I looked online and in books, but couldn't find a definitive answer to
    > this.
    >
    > I have an array and set multiple elements to either True or False at
    > one time.
    >
    > Question: Which way is faster (or does it matter)?
    >
    > 1)
    >
    > array[x1]=array[x2]=........= array[x10] = \
    > array[x11]=array[x12]=... = array[x20] = \
    > ......
    > ......
    > array[x40]=array[x41]=....= array[x50] = False (or True)
    >
    >
    > 2)
    >
    > array[x1]=array[x2]=........= array[x10] = False
    > array[x11]=array[x12]=... = array[x20] = False
    > ......
    > ......
    > array[x40]=array[x41]=....= array[x50] = False


    It doesn't matter as none of this is valid Python. In Python you have to
    write

    array[x1] = False
    array[x2] = False

    Etc...

    --
    Arnaud
    Arnaud Delobelle, Nov 17, 2008
    #2
    1. Advertising

  3. jzakiya

    jzakiya Guest

    On Nov 17, 2:10 pm, Arnaud Delobelle <> wrote:
    > jzakiya <> writes:
    > > I looked online and in books, but couldn't find a definitive answer to
    > > this.

    >
    > > I have an array and set multiple elements to either True or False at
    > > one time.

    >
    > > Question: Which way is faster (or does it matter)?

    >
    > > 1)

    >
    > > array[x1]=array[x2]=........= array[x10] = \
    > > array[x11]=array[x12]=... = array[x20] = \
    > > ......
    > > ......
    > > array[x40]=array[x41]=....= array[x50] = False (or True)

    >
    > > 2)

    >
    > > array[x1]=array[x2]=........= array[x10] = False
    > > array[x11]=array[x12]=... = array[x20] = False
    > > ......
    > > ......
    > > array[x40]=array[x41]=....= array[x50] = False

    >
    > It doesn't matter as none of this is valid Python. In Python you have to
    > write
    >
    > array[x1] = False
    > array[x2] = False
    >
    > Etc...
    >
    > --
    > Arnaud


    Could you explain please.
    Python allows this, so is it just not considered good idiomatic
    python?

    jz
    jzakiya, Nov 17, 2008
    #3
  4. Arnaud Delobelle wrote:

    > jzakiya <> writes:
    >
    >> I looked online and in books, but couldn't find a definitive answer to
    >> this.
    >>
    >> I have an array and set multiple elements to either True or False at
    >> one time.
    >>
    >> Question: Which way is faster (or does it matter)?
    >>
    >> 1)
    >>
    >> array[x1]=array[x2]=........= array[x10] = \
    >> array[x11]=array[x12]=... = array[x20] = \
    >> ......
    >> ......
    >> array[x40]=array[x41]=....= array[x50] = False (or True)
    >>
    >>
    >> 2)
    >>
    >> array[x1]=array[x2]=........= array[x10] = False
    >> array[x11]=array[x12]=... = array[x20] = False
    >> ......
    >> ......
    >> array[x40]=array[x41]=....= array[x50] = False

    >
    > It doesn't matter as none of this is valid Python. In Python you have to
    > write
    >
    > array[x1] = False
    > array[x2] = False
    >
    > Etc...
    >


    No.


    l = range(10)
    l[0] = l[1] = 100

    And there seems to be a small difference between both versions,
    but I can't judge for sure if that favors one of them - yet I tend to think
    it's not worth the effort...

    import dis

    def a():
    l = range(10)
    l[0] = l[1] = 100

    def b():
    l = range(10)
    l[0] = 100
    l[1] = 100

    print "------------ a ---------------"
    dis.dis(a)
    print "------------ b ---------------"
    dis.dis(b)

    ------------ a ---------------
    4 0 LOAD_GLOBAL 0 (range)
    3 LOAD_CONST 1 (10)
    6 CALL_FUNCTION 1
    9 STORE_FAST 0 (l)

    5 12 LOAD_CONST 2 (100)
    * 15 DUP_TOP
    16 LOAD_FAST 0 (l)
    19 LOAD_CONST 3 (0)
    22 STORE_SUBSCR
    23 LOAD_FAST 0 (l)
    26 LOAD_CONST 4 (1)
    29 STORE_SUBSCR
    30 LOAD_CONST 0 (None)
    33 RETURN_VALUE
    ------------ b ---------------
    8 0 LOAD_GLOBAL 0 (range)
    3 LOAD_CONST 1 (10)
    6 CALL_FUNCTION 1
    9 STORE_FAST 0 (l)

    9 12 LOAD_CONST 2 (100)
    15 LOAD_FAST 0 (l)
    18 LOAD_CONST 3 (0)
    21 STORE_SUBSCR

    10 22 LOAD_CONST 2 (100)
    25 LOAD_FAST 0 (l)
    28 LOAD_CONST 4 (1)
    31 STORE_SUBSCR
    32 LOAD_CONST 0 (None)
    35 RETURN_VALUE
    Diez B. Roggisch, Nov 17, 2008
    #4
  5. jzakiya

    Tim Chase Guest

    > It doesn't matter as none of this is valid Python. In Python you have to
    > write
    >
    > array[x1] = False
    > array[x2] = False


    Uh...not so much...

    >>> a = [1,2,3,4,5]
    >>> x1, x2 = 1, 3
    >>> a[x1] = a[x2] = False
    >>> a

    [1, False, 3, False, 5]

    Works for me.

    To the OP, I think rather than cluttering my code, I'd just
    create a loop

    for i in [x1,x2,x3,x4,...x1024]:
    a = False

    From Diez's disassembly of it (as an aside, nifty little intro
    to dis.dis()...thanks, Diez!), it looks like it boils down to "is
    DUP_TOP faster than LOAD_CONST" because the rest of the
    operations. At this point, it's pretty nitty-gritty.

    Unless the code is in an inner loop somewhere, the simple loop
    should be more than fast enough. Without knowing the source of
    the [x1,...] index variables, it's hard to tell if there's a more
    optimal way to do this.

    -tkc
    Tim Chase, Nov 17, 2008
    #5
  6. Arnaud Delobelle <> writes:

    > jzakiya <> writes:
    >
    >> I looked online and in books, but couldn't find a definitive answer to
    >> this.
    >>
    >> I have an array and set multiple elements to either True or False at
    >> one time.
    >>
    >> Question: Which way is faster (or does it matter)?
    >>
    >> 1)
    >>
    >> array[x1]=array[x2]=........= array[x10] = \
    >> array[x11]=array[x12]=... = array[x20] = \
    >> ......
    >> ......
    >> array[x40]=array[x41]=....= array[x50] = False (or True)
    >>
    >>
    >> 2)
    >>
    >> array[x1]=array[x2]=........= array[x10] = False
    >> array[x11]=array[x12]=... = array[x20] = False
    >> ......
    >> ......
    >> array[x40]=array[x41]=....= array[x50] = False

    >
    > It doesn't matter as none of this is valid Python. In Python you have to
    > write
    >
    > array[x1] = False
    > array[x2] = False
    >
    > Etc...


    Sorry, I don't know what came over me then, some kind of brain
    short-circuit... No doubt plenty of people will correct me before this
    is posted!

    Of course it's valid Python, but it's not very stylish. Assuming your
    array is a list, you could put all your indices that are meant to be
    True in a list:

    indices = [x1, x2, x3, ..., x50]

    Then loop over them:

    for i in indices:
    array = True

    It might not be as fast of course

    --
    Arnaud
    Arnaud Delobelle, Nov 17, 2008
    #6
  7. In article <>,
    Tim Chase <> wrote:
    .
    .
    .
    >To the OP, I think rather than cluttering my code, I'd just
    >create a loop
    >
    > for i in [x1,x2,x3,x4,...x1024]:
    > a = False

    .
    .
    .
    I much prefer this, too.

    I've been trying to decide if there's any sober reason to advocate
    the one-liner

    map(lambda i: a.__setitem__(i, False), [x1, x2, x3, ..., x1024])

    My answer: no.
    Cameron Laird, Nov 17, 2008
    #7
  8. jzakiya

    CarlFK Guest

    On Nov 17, 2:54 pm, (Cameron Laird) wrote:
    > In article <>,
    > Tim Chase  <> wrote:
    >                         .
    >                         .
    >                         .>To the OP, I think rather than cluttering my code, I'd just
    > >create a loop

    >
    > >   for i in [x1,x2,x3,x4,...x1024]:
    > >     a = False

    >
    >                         .
    >                         .
    >                         .
    > I much prefer this, too.
    >
    > I've been trying to decide if there's any sober reason to advocate
    > the one-liner
    >
    >     map(lambda i: a.__setitem__(i, False), [x1, x2, x3, ..., x1024])
    >
    > My answer:  no.


    well, the OP was "...faster?" not "best, cleanest, pythonic..." so
    the reason would be speed.

    (looks faster, don't know for sure)
    (and yeah, looks ugly. but we need the speed...)

    Carl K
    CarlFK, Nov 18, 2008
    #8
  9. jzakiya

    Tim Roberts Guest

    jzakiya <> wrote:
    >
    >I looked online and in books, but couldn't find a definitive answer to
    >this.
    >
    >I have an array and set multiple elements to either True or False at
    >one time.
    >
    >Question: Which way is faster (or does it matter)?


    Answer: it does not matter. This is premature optimization. First, make
    it work. Then, figure out whether it is fast enough. THEN, figure out
    what's taking the most time.

    I'd be very, very surprised if this was an important part of your run time.
    --
    Tim Roberts,
    Providenza & Boekelheide, Inc.
    Tim Roberts, Nov 20, 2008
    #9
  10. Cameron Laird wrote:

    > I've been trying to decide if there's any sober reason to advocate
    > the one-liner
    >
    > map(lambda i: a.__setitem__(i, False), [x1, x2, x3, ..., x1024])


    Are lambdas like the Dark Side of Python?

    :)
    Lawrence D'Oliveiro, Nov 25, 2008
    #10
  11. jzakiya

    Iain King Guest

    On Nov 17, 7:41 pm, Tim Chase <> wrote:
    > > It doesn't matter as none of this is valid Python. In Python you have to
    > > write

    >
    > > array[x1] = False
    > > array[x2] = False

    >
    > Uh...not so much...
    >
    > >>> a = [1,2,3,4,5]
    > >>> x1, x2 = 1, 3
    > >>> a[x1] = a[x2] = False
    > >>> a

    > [1, False, 3, False, 5]
    >
    > Works for me.
    >
    > To the OP, I think rather than cluttering my code, I'd just
    > create a loop
    >
    > for i in [x1,x2,x3,x4,...x1024]:
    > a = False
    >
    > From Diez's disassembly of it (as an aside, nifty little intro
    > to dis.dis()...thanks, Diez!), it looks like it boils down to "is
    > DUP_TOP faster than LOAD_CONST" because the rest of the
    > operations. At this point, it's pretty nitty-gritty.
    >
    > Unless the code is in an inner loop somewhere, the simple loop
    > should be more than fast enough. Without knowing the source of
    > the [x1,...] index variables, it's hard to tell if there's a more
    > optimal way to do this.
    >
    > -tkc


    The loop is much nicer, especially as your array gets longer. The
    generic:

    for i in xrange(array):
    array = False

    will set the entire array.

    Regarding your original question:

    array[x1] = array[x2] = False

    array[x1] = False
    array[x2] = False

    These two blocks are functionally the same when you are setting to
    True or False (or any immutable), but are not if setting to
    immutables, which could give you some real head-scratching bugs if you
    were unaware of the difference - the first version assigns the same
    object to both names:

    >>> array[x1] = array[x2] = []
    >>> array[x1].append("Hi")
    >>> array[x2]

    ['Hi']


    Iain
    Iain King, Nov 25, 2008
    #11
  12. jzakiya

    Iain King Guest

    On Nov 25, 11:29 am, Iain King <> wrote:
    > On Nov 17, 7:41 pm, Tim Chase <> wrote:
    >
    >
    >
    > > > It doesn't matter as none of this is valid Python. In Python you have to
    > > > write

    >
    > > > array[x1] = False
    > > > array[x2] = False

    >
    > > Uh...not so much...

    >
    > > >>> a = [1,2,3,4,5]
    > > >>> x1, x2 = 1, 3
    > > >>> a[x1] = a[x2] = False
    > > >>> a

    > > [1, False, 3, False, 5]

    >
    > > Works for me.

    >
    > > To the OP, I think rather than cluttering my code, I'd just
    > > create a loop

    >
    > > for i in [x1,x2,x3,x4,...x1024]:
    > > a = False

    >
    > > From Diez's disassembly of it (as an aside, nifty little intro
    > > to dis.dis()...thanks, Diez!), it looks like it boils down to "is
    > > DUP_TOP faster than LOAD_CONST" because the rest of the
    > > operations. At this point, it's pretty nitty-gritty.

    >
    > > Unless the code is in an inner loop somewhere, the simple loop
    > > should be more than fast enough. Without knowing the source of
    > > the [x1,...] index variables, it's hard to tell if there's a more
    > > optimal way to do this.

    >
    > > -tkc

    >
    > The loop is much nicer, especially as your array gets longer. The
    > generic:
    >
    > for i in xrange(array):
    > array = False
    >
    > will set the entire array.
    >
    > Regarding your original question:
    >
    > array[x1] = array[x2] = False
    >
    > array[x1] = False
    > array[x2] = False
    >
    > These two blocks are functionally the same when you are setting to
    > True or False (or any immutable), but are not if setting to
    > immutables, which could give you some real head-scratching bugs if you
    > were unaware of the difference - the first version assigns the same
    > object to both names:
    >
    > >>> array[x1] = array[x2] = []
    > >>> array[x1].append("Hi")
    > >>> array[x2]

    >
    > ['Hi']
    >
    > Iain



    ....and of course, the second time I say 'immutable' I mean 'mutable'.
    Hopefully the example was clearer than the text.

    Iain
    Iain King, Nov 25, 2008
    #12
  13. In article <ggg3oe$vfe$>,
    Lawrence D'Oliveiro <_zealand> wrote:
    >Cameron Laird wrote:
    >
    >> I've been trying to decide if there's any sober reason to advocate
    >> the one-liner
    >>
    >> map(lambda i: a.__setitem__(i, False), [x1, x2, x3, ..., x1024])

    >
    >Are lambdas like the Dark Side of Python?
    >
    >:)


    Enough so, apparently, that I'm reluctant even to touch that question.
    Cameron Laird, Nov 26, 2008
    #13
  14. In message <>, Cameron Laird wrote:

    > In article <ggg3oe$vfe$>,
    > Lawrence D'Oliveiro <_zealand> wrote:
    >
    >>Cameron Laird wrote:
    >>
    >>> I've been trying to decide if there's any sober reason to advocate
    >>> the one-liner
    >>>
    >>> map(lambda i: a.__setitem__(i, False), [x1, x2, x3, ..., x1024])

    >>
    >>Are lambdas like the Dark Side of Python?
    >>
    >>:)

    >
    > Enough so, apparently, that I'm reluctant even to touch that question.


    So how else would you express something like

    def shell_escape(Arg) :
    """returns Arg suitably escaped for use as a command-line argument
    to Bash."""
    return \
    re.sub \
    (
    r"[\<\>\"\'\|\&\$\#\;\(\)\[\]\{\}\`\!\~\ \\]",
    lambda Match : "\\" + Match.group(0),
    Arg
    )
    # Need to catch anything that might be meaningful to shell
    #end shell_escape

    ?
    Lawrence D'Oliveiro, Dec 2, 2008
    #14
  15. In article <gh4e6n$pn$>,
    Lawrence D'Oliveiro <_zealand> wrote:
    >In message <>, Cameron Laird wrote:
    >
    >> In article <ggg3oe$vfe$>,
    >> Lawrence D'Oliveiro <_zealand> wrote:
    >>
    >>>Cameron Laird wrote:
    >>>
    >>>> I've been trying to decide if there's any sober reason to advocate
    >>>> the one-liner
    >>>>
    >>>> map(lambda i: a.__setitem__(i, False), [x1, x2, x3, ..., x1024])
    >>>
    >>>Are lambdas like the Dark Side of Python?
    >>>
    >>>:)

    >>
    >> Enough so, apparently, that I'm reluctant even to touch that question.

    >
    >So how else would you express something like
    >
    > def shell_escape(Arg) :
    > """returns Arg suitably escaped for use as a command-line argument
    > to Bash."""
    > return \
    > re.sub \
    > (
    > r"[\<\>\"\'\|\&\$\#\;\(\)\[\]\{\}\`\!\~\ \\]",
    > lambda Match : "\\" + Match.group(0),
    > Arg
    > )
    > # Need to catch anything that might be meaningful to shell
    > #end shell_escape
    >
    >?


    I suspect we're confusing each other. I *like* lambdas--at least,
    more than Guido does, which I recognize is a low standard.

    When I take your question at face value, my response is

    def shell_escape(Arg) :
    """returns Arg suitably escaped for use as a command-line argument
    to Bash."""

    pattern = r"[\<\>\"\'\|\&\$\#\;\(\)\[\]\{\}\`\!\~\ \\]"
    def f1(Match):
    return
    return re.sub(pattern, f1, Arg)
    # Need to catch anything that might be meaningful to shell
    #end shell_escape

    'cept that I'd hope to find a way to simplify pattern. Was that
    what you were asking?
    Cameron Laird, Dec 3, 2008
    #15
  16. Cameron Laird wrote:

    > def f1(Match):
    > return


    Something missing here?
    Lawrence D'Oliveiro, Dec 3, 2008
    #16
  17. In article <gh5f7h$jqh$>,
    Lawrence D'Oliveiro <_zealand> wrote:
    >Cameron Laird wrote:
    >
    >> def f1(Match):
    >> return

    >
    >Something missing here?


    Ugh; yes, sorry:

    def shell_escape(Arg) :
    """returns Arg suitably escaped for use as a command-line argument
    to Bash."""

    pattern = r"[\<\>\"\'\|\&\$\#\;\(\)\[\]\{\}\`\!\~\ \\]"
    def f1(Match):
    return "\\" + Match.group(0)
    return re.sub(pattern, f1, Arg)
    # Need to catch anything that might be meaningful to shell
    #end shell_escape
    Cameron Laird, Dec 3, 2008
    #17
  18. In message <>, Cameron Laird wrote:

    > def shell_escape(Arg) :
    > """returns Arg suitably escaped for use as a command-line argument
    > to Bash."""
    >
    > pattern = r"[\<\>\"\'\|\&\$\#\;\(\)\[\]\{\}\`\!\~\ \\]"
    > def f1(Match):
    > return "\\" + Match.group(0)
    > return re.sub(pattern, f1, Arg)
    > # Need to catch anything that might be meaningful to shell
    > #end shell_escape


    So why is that better?
    Lawrence D'Oliveiro, Dec 3, 2008
    #18
  19. In article <gh5q7c$r1e$>,
    Lawrence D'Oliveiro <_zealand> wrote:
    >In message <>, Cameron Laird wrote:
    >
    >> def shell_escape(Arg) :
    >> """returns Arg suitably escaped for use as a command-line argument
    >> to Bash."""
    >>
    >> pattern = r"[\<\>\"\'\|\&\$\#\;\(\)\[\]\{\}\`\!\~\ \\]"
    >> def f1(Match):
    >> return "\\" + Match.group(0)
    >> return re.sub(pattern, f1, Arg)
    >> # Need to catch anything that might be meaningful to shell
    >> #end shell_escape

    >
    >So why is that better?


    I'm very lost, now. As best I can remember and/or reconstruct
    this conversation, the form above is adequately readable and
    does not involve lambda (and fits in eighty-column lines, and
    has a few even more mundane features). There's a realm within
    Pythonia that favors lambdalessness.

    I think that's all I'm claiming at this point.
    Cameron Laird, Dec 3, 2008
    #19
  20. In message <>, Cameron Laird wrote:

    > There's a realm within Pythonia that favors lambdalessness.


    Why is that? Was Rossum frightened in his cradle by a lambda when he was a
    baby? Are some people afraid of lambdas the way others are afraid of
    spiders?
    Lawrence D'Oliveiro, Dec 4, 2008
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Calvin
    Replies:
    2
    Views:
    658
    Calvin
    Aug 1, 2004
  2. Stephen Miller
    Replies:
    0
    Views:
    1,631
    Stephen Miller
    Jun 24, 2003
  3. Andrew Kidd
    Replies:
    3
    Views:
    461
    Andrew Kidd
    Apr 22, 2004
  4. Diane Selby
    Replies:
    4
    Views:
    6,760
  5. Mike Copeland

    Equates or Synonyms?

    Mike Copeland, Nov 27, 2011, in forum: C++
    Replies:
    14
    Views:
    448
    Jorgen Grahn
    Nov 29, 2011
Loading...

Share This Page