[Numpy-discussion] Sorting objects with ndarrays

Gael Varoquaux gael.varoquaux@normalesup....
Sun Feb 28 08:07:43 CST 2010

On Sun, Feb 28, 2010 at 03:01:18PM +0100, Friedrich Romstedt wrote:
> > Well, I might not have to compare ndarrays, but fairly arbitrary
> > structures (dictionnaries, classes and lists) as I am dealing with
> > semi-structured data coming from a stack of unorganised experimental
> > data. Python has some logic for comparing these structures by comparing
> > their members, but if these are ndarrays, I am back to my original
> > problem.

> I also do not understand how to build an oder on such a thing at all,
> maybe you can give a simple example?

Well, you can't really build an order in the mathematical sens of
ordering. All I care is that if you give me twice the samed shuffled list
of elements, it comes out identical. I am fighting the fact that
dictionnaries in Python have no order, and thus shuflle the data from run
to run.

> Hmm, you could also replace numpy.greater and similar temporarily with
> an with statement like:

> # Everything as usual, comparing ndarrays results in ndarrays here.

> with monkeypatched_operators:
>     # Comparing ndarrays may result in scalars or what you need.
>     pass  # Perform the sorting

> # Everything as usual ...

> Though that's maybe not threadsafe too.

Yes, it's not threadsafe either.

> Then you could use ndarray.flatten().tolist() to compare them using
> usual Python semantics?

That solves the local problem of comparing 2 arrays (though will be quite
slow), but not the general problem of sorting in a reproducible order
(may it be arbitary) objects containing arrays.

Anyhow, I solved the problem implementing a subclass of dict and using it
everywhere in my code. Right now it seems to be working for what I need.



More information about the NumPy-Discussion mailing list