On 21 mar, 17:43, Martin Manns <[EMAIL PROTECTED]> wrote: > Basically I have a lot of functions inside a numpy array, > of which most return "" and actually are derived as follows: > > from numpy import * > > grid_size = 1000000 > > class F(object): > def __cmp__(self, other): return 0 > def __call__(self, dummy): return "" > f = F() > myarray = array([f] * grid_size, dtype="O") > > # I re-define an element in the array > > myarray[34424] = lambda x: "23" > > # Now, I would like to loop over all re-defined elements: > > for i in itertools.izip(*nonzero(myarray)): > print myarray[i]() > > If I fill the array with 0 instead of the functions that > return "" this works really fast. > > However, I would like to be able call the content of each > cell in the array as a function.
If you drop this condition then you could fill the array with zeroes as before, replace only the interesting ones with actual functions, and write: for item in myarray[nonzero(myarray)]: print item() if callable(item) else item > As I said, the callable object approach works but is > about as slow as iterating through the whole array. > Perhaps I should add a __call__ function to the built-in > 0? But I doubt that this helps. You can't - anyway, being Python code, would not help with the speed. -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list