import re
_nsre = re.compile('([0-9]+)')

def natural_sort_key(s):
    return [(int(text) if text.isdigit() else text.lower()) for text in re.
        split(_nsre, s)]
Sorting in python - how to sort a list containing alphanumeric values?