jeudi 16 juin 2016

sort a string array using numpy


I want to sort a string array using numpy by the length of the elements.

>>> arr = ["year","month","eye","i","stream","key","house"]
>>> x = np.sort(arr, axis=-1, kind='mergesort')
>>> print(x)
['eye' 'house' 'i' 'key' 'month' 'stream' 'year']

But it sorts them in alphanumeric order. How can I sort them using numpy by their length?


Aucun commentaire:

Enregistrer un commentaire