faqts : Computers : Programming : Languages : Python : Snippets : Arrays

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

1 of 2 people (50%) answered Yes

Entry

How do you keep track of elements of "ravel"ed Numeric arrays

Jul 5th, 2000 09:59
Nathan Wallace, Rob Hooft, unknown unknown, Hans Nowak, Snippet 93, Alex


"""
Packages: maths.numeric
"""
"""
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))]
>>>
"""