[Numpy-discussion] Sorting objects with ndarrays

Pauli Virtanen pav at iki.fi
Sun Feb 28 06:05:15 EST 2010


su, 2010-02-28 kello 10:25 +0100, Gael Varoquaux kirjoitti:
[clip]
> The problem is that ndarrays cannot be compared. So I have tried to
> override the 'cmp' in the 'sorted' function, however I am comparing
> fairly complex objects, and I am having a hard time predicting wich
> member of the object will contain the array. 

I don't understand what "predicting which member of the object" means?
Do you mean that in the array, you have classes that contain ndarrays as
their attributes, and the classes have __cmp__ implemented?

If not, can you tell why

        def xcmp(a, b):
            a_nd = isinstance(a, ndarray)
            b_nd = isinstance(b, ndarray)
        
            if a_nd and b_nd:
                pass # compare ndarrays in some way
            elif a_nd:
                return 1  # sort ndarrays first
            elif b_nd:
                return -1 # sort ndarrays first
            else:
                return cmp(a, b) # ordinary compare

does not work?

Cheers,
Pauli





More information about the NumPy-Discussion mailing list