Simplify alphanumeric_key logic

Thanks to Tony Yu for the suggestion.
This commit is contained in:
Juan Nunez-Iglesias
2012-07-21 00:51:55 -05:00
parent 699b5d9269
commit 9cbf2ef369
+3 -9
View File
@@ -11,12 +11,6 @@ import numpy as np
from ._io import imread
def _tryint(s):
try:
return int(s)
except ValueError:
return s
def alphanumeric_key(s):
"""Convert string to list of strings and ints that gives intuitive sorting.
@@ -34,11 +28,11 @@ def alphanumeric_key(s):
['z', 23, 'a']
>>> filenames = ['f9.10.png', 'f9.9.png', 'f10.10.png', 'f10.9.png']
>>> sorted(filenames)
['f10.10.png', 'f10.9.png', 'f9.10.png', 'f9.9.png']
['f10.10.png', 'f10.9.png', 'f9.10.png', 'f9.9.png', 'e10.png']
>>> sorted(filenames, key=alphanumeric_key)
['f9.9.png', 'f9.10.png', 'f10.9.png', 'f10.10.png']
['e10.png', 'f9.9.png', 'f9.10.png', 'f10.9.png', 'f10.10.png']
"""
k = [_tryint(c) for c in re.split('([0-9]+)', s)]
k = [int(c) if c.isdigit() else c for c in re.split('([0-9]+)', s)]
return k
class MultiImage(object):