array manipulation without for loops

Gary Herron gherron at islandtraining.com
Sun Jun 25 12:19:39 EDT 2006


Sheldon wrote:
> Hi,
>
> I have two arrays that are of the same dimension but having 3 different
> values: 255, 1 or 2.
> I would like to set all the positions in both arrays having 255 to be
> equal, i.e., where one array has 255, I set the same elements in the
> other array to 255 and visa versa. Does anyone know how to do this
> without using for loops?
>
> Sincerely,
> Sheldon
>
>   
Whatever for?  Have you got something against for loops?

However...

You could roll you own loop:
i=0
while i < whatever:
    # ... do something with i
    i += 1

But what's the point?  This does the same as a for loop but slower.

If you don't want any kind of a loop (again, What's the point?) you
could write something recursive:

def proc(i, ...):
    # ... do something with i
    if i < whatever:
        proc(i+1, ...)

But this would be even slower.

Gary Herron




More information about the Python-list mailing list