array manipulation without for loops

Gary Herron gherron at islandtraining.com
Sun Jun 25 12:54:07 EDT 2006


Sheldon wrote:
> Hi Gary,
>
> I am really trying to cut the time down as I have 600+ arrays with
> dimensions (1215,1215) to compare and I do a lot more things with the
> arrays. If I understand you correctly, there is no way around a for
> loop?
>   
Well no. I gave you two alternatives to for loops. But once we learn
that your motivation is speed on large arrays, then, by all means, go
with Alex's suggestion. Use numpy (or one if its earlier incarnations).
See: http://numeric.scipy.org/

This is a HIGHLY efficient implementation of arrays for Python. It
provides a number of very general operations that can be performed
across arrays.

Good luck
Gary Herron

>
> /Sheldon
>
> Gary Herron wrote:
>
>   
>> 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