[SciPy-User] numpy.lexsort

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

[SciPy-User] numpy.lexsort

Gabriele Brambilla
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
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: numpy.lexsort

Oleksandr Huziy
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 <[hidden email]>:
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
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user




--
Sasha

_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user