Skip to content

Commit

Permalink
Simplify alphanumeric_key logic
Browse files Browse the repository at this point in the history
Thanks to Tony Yu for the suggestion.
  • Loading branch information
jni committed Jul 21, 2012
1 parent 699b5d9 commit 9cbf2ef
Showing 1 changed file with 3 additions and 9 deletions.
12 changes: 3 additions & 9 deletions skimage/io/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@
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.
Expand All @@ -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):
Expand Down

0 comments on commit 9cbf2ef

Please sign in to comment.