Shane
Shane

Reputation: 100194

Sort numpy string array with negative numbers?

I'm having a problem sorting a numpy array that has numbers as strings. I need to keep these as strings because there are other words after the integers.

It's sorting negative numbers in reverse order:

>>> import numpy as np
>>> a = np.array(["3", "-2", "-1", "0", "2"])
>>> a.sort()
>>> a
array(['-1', '-2', '0', '2', '3'], dtype='|S2')

I would have expected the output to be:

array(['-2', '-1', '0', '2', '3'], dtype='|S2')

Any suggestions?

Upvotes: 1

Views: 3868

Answers (2)

unutbu
unutbu

Reputation: 880547

You could use natural sorting:

import numpy as np
import re

def atoi(text):
    try:
        return int(text)
    except ValueError:
        return text

def natural_keys(text):
    '''
    alist.sort(key=natural_keys) sorts in human order
    http://nedbatchelder.com/blog/200712/human_sorting.html
    '''    
    return [ atoi(c) for c in re.split('([-]?\d+)', text) ]

a = np.array(["3", "-2", "-1", "0", "2", "word"])
print(sorted(a,key=natural_keys))
# ['-2', '-1', '0', '2', '3', 'word']

a = np.array(["3", "-2", "-1", "0", "2", "word", "-1 word", "-2 up"])
print(sorted(a,key=natural_keys))
# ['-2', '-2 up', '-1', '-1 word', '0', '2', '3', 'word']

Upvotes: 6

Cosmologicon
Cosmologicon

Reputation: 2167

Assuming there's a space after the integer before the other words, then if a were a regluar python list you'd do:

a.sort(key = lambda s: int(s.split()[0]))

Not sure what the equivalent is in numpy (don't see how to specify a key), but one possibility is to convert to a list and back to an array.

Upvotes: 2

Related Questions