[SciPy-User] numpy.lexsort

Oleksandr Huziy guziy.sasha at gmail.com
Fri Aug 21 11:53:12 EDT 2015


Hi:

Here's a way I found in documentation (tested on py3):

In [1]: import numpy as np

In [2]: surnames = ['Hertz', 'Galilei', 'Hertz']

In [3]:  names = ['Heinrich', 'Galilao', 'Gustav']

In [4]: arr = np.fromiter(zip(surnames, names), dtype=[("lastname",
"S100"), ("firstname", "S100")])

In [5]: arr
Out[5]:
array([(b'Hertz', b'Heinrich'), (b'Galilei', b'Galilao'),
       (b'Hertz', b'Gustav')],
      dtype=[('lastname', 'S100'), ('firstname', 'S100')])

In [6]: arr.sort()

In [7]: surnames2 = arr["lastname"]; names2 = arr["firstname"]

In [8]: surnames2
Out[8]:
array([b'Galilei', b'Hertz', b'Hertz'],
      dtype='|S100')

In [9]: names2
Out[9]:
array([b'Galilao', b'Gustav', b'Heinrich'],
      dtype='|S100')

Cheers

2015-08-21 10:50 GMT-04:00 Gabriele Brambilla <
gb.gabrielebrambilla at gmail.com>:

> Hi I want to order a numpy.array using lexsort.
>
> >>> import numpy as np
>
> >>> surnames = ['Hertz', 'Galilei', 'Hertz']
>
> >>> names = ['Heinrich', 'Galilao', 'Gustav']
>
> >>> ind = np.lexsort((names,surnames))
>
> >>> ind
>
> array([1, 2, 0])
>
> But how can I have an array ordered according to this indexes?
>
> I would do
>
> surnames2 = surnames.copy()
>
> for i, a in enumerate(ind):
>
>        surnames2[i] = surnames[a]
>
> But I think there should be another way without writing the cycle by
> myself.
>
> Thanks
>
> GB
>
>
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
>


-- 
Sasha
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20150821/28bfb204/attachment.html>


More information about the SciPy-User mailing list