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()
>&开发者_开发百科gt;> 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?
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']
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.
精彩评论