How to find maximal entry in a Numpy array?

Alex alex at somewhere.round.here
Sun May 2 10:08:43 EDT 1999


Hi, again.

In case anyone else needs to do something like sorting an array and
keeping track of what belongs where in the array, I've found the
following function useful.  It's not super-fast, but I guess it's doing
a lot.

import operator
from Numeric import *

def flatten_indices (array):
    index_array = indices (array.shape)
    index_list = map (ravel, tuple (index_array))
    index_list = apply (map, (None, ) + tuple (index_list))
    values = map (operator.getitem, \
                  len (index_list) * [array], \
                  index_list)
    return map (None, values, index_list)

E.g.

>>> b = reshape (arange (4), (2, 2))
>>> flatten_indices (b)
[(0, (0, 0)), (1, (0, 1)), (2, (1, 0)), (3, (1, 1))]
>>> 

Alex.




More information about the Python-list mailing list