Multiple equates

Arnaud Delobelle arnodel at googlemail.com
Mon Nov 17 20:50:41 CET 2008


Arnaud Delobelle <arnodel at googlemail.com> writes:

> jzakiya <jzakiya at mail.com> 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[i] = True

It might not be as fast of course

-- 
Arnaud



More information about the Python-list mailing list