Created
July 16, 2018 09:06
-
-
Save vaibhavgeek/d6811a1c77f7d5b9da5ab3a92251dadb to your computer and use it in GitHub Desktop.
Sort Accorfing to string and integer type in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
test_files = os.listdir(f'{PATH}/test') | |
import re | |
def atoi(text): | |
return int(text) if text.isdigit() else text | |
def natural_keys(text): | |
return [ atoi(c) for c in re.split('(\d+)', text) ] | |
test_files.sort(key=natural_keys) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment