# Multiple equates

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

1. ### jzakiyaGuest

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

2. ### Arnaud DelobelleGuest

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

3. ### jzakiyaGuest

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

Python allows this, so is it just not considered good idiomatic
python?

jz

jzakiya, Nov 17, 2008
4. ### Diez B. RoggischGuest

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 ---------------
6 CALL_FUNCTION 1
9 STORE_FAST 0 (l)

* 15 DUP_TOP
22 STORE_SUBSCR
29 STORE_SUBSCR
33 RETURN_VALUE
------------ b ---------------
6 CALL_FUNCTION 1
9 STORE_FAST 0 (l)

21 STORE_SUBSCR

31 STORE_SUBSCR
35 RETURN_VALUE

Diez B. Roggisch, Nov 17, 2008
5. ### Tim ChaseGuest

> 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
6. ### Arnaud DelobelleGuest

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
7. ### Cameron LairdGuest

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

Cameron Laird, Nov 17, 2008
8. ### CarlFKGuest

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

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
9. ### Tim RobertsGuest

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
10. ### Lawrence D'OliveiroGuest

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
11. ### Iain KingGuest

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.

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
12. ### Iain KingGuest

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.
>
>
> 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
13. ### Cameron LairdGuest

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
14. ### Lawrence D'OliveiroGuest

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
15. ### Cameron LairdGuest

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

Cameron Laird, Dec 3, 2008
16. ### Lawrence D'OliveiroGuest

Cameron Laird wrote:

> def f1(Match):
> return

Something missing here?

Lawrence D'Oliveiro, Dec 3, 2008
17. ### Cameron LairdGuest

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
18. ### Lawrence D'OliveiroGuest

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
19. ### Cameron LairdGuest

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
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
20. ### Lawrence D'OliveiroGuest

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