Sorting list of file name with text, number and underscore in Python

1.3k views Asked by At

I have a list of filenames, some of them have only text, some of them have text and number, and some of them have all.

Example:

[ 'mango_1.jpg', 'dog005.jpg', 'guru_2018_01_01.png', 'dog008.jpg', 'mango_6.jpg', 'guru_2018_5_23.png', 'dog01.png', 'mango_11.jpg', 'mango2.jpg', 'guru_2018_02_5.png', 'guru_2019_08_23.jpg', 'dog9.jpg', 'mango05.jpg' ]

My Code is :

import re
## ref: https://blog.codinghorror.com/sorting-for-humans-natural-sort-order/
def sort_nicely( l ):
    """ Sort the given list in the way that humans expect.
    """
    convert = lambda text: int(text) if text.isdigit() else text
    alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ]
    l.sort( key=alphanum_key )
    return print(l)

Actual output:

['dog01.png', 'dog005.jpg', 'dog008.jpg', 'dog9.jpg', 'guru_2018_01_01.png', 'guru_2018_02_5.png', 'guru_2018_5_23.png', 'guru_2019_08_23.jpg', 'mango2.jpg', 'mango05.jpg', 'mango_1.jpg', 'mango_6.jpg', 'mango_11.jpg']

Expected output:

['dog01.png', 'dog005.jpg', 'dog008.jpg', 'dog9.jpg', 'guru_2018_01_01.png', 'guru_2018_02_5.png', 'guru_2018_5_23.png', 'guru_2019_08_23.jpg', 'mango_1.jpg', 'mango2.jpg', 'mango5.jpg', 'mango_6.jpg', 'mango_11.jpg']

How do I get the expected output?

2

There are 2 answers

0
AudioBubble On BEST ANSWER

Looks like you are not giving any significance to _ character in that case, modify your code to exclude that

import re
## ref: https://blog.codinghorror.com/sorting-for-humans-natural-sort-order/
def sort_nicely( l ):
    """ Sort the given list in the way that humans expect.
    """
    convert = lambda text: int(text) if text.isdigit() else text
    alphanum_key = lambda key: [ convert(c.replace("_","")) for c in re.split('([0-9]+)', key) ]
    l.sort( key=alphanum_key )
    return print(l)
    ```
0
Exa On

From what I understand, you want to sort according to the "text" and then the "date" that may exist in each filename. So first you need a function that can split filenames into those two components:

def split(n):
    # back-to-front, find first letter index
    for (i, c) in enumerate(reversed(n)):
        if not (c.isdigit() or c == '_'):
            break
    # proper (non-reversed) index
    i = len(n) - i
    # split into name and date
    (n, t) = (n[:i], n[i:])
    # split and remove extra underscores
    t = filter(bool, t.split('_'))
    # convert to integers and return
    return n, tuple(map(int, t))

Then you need a function to get rid of any unwanted parts in filenames (like extensions):

import os
def parse(n):
    (n, e) = os.path.splitext(n)
    return split(n)

Now you can simply use this as a key in the built-in sorted function:

>>> sorted(l, key = parse)
['dog01.png', 'dog005.jpg', 'dog008.jpg', 'dog9.jpg', 'guru_2018_01_01.png', 'guru_2018_02_5.png', 'guru_2018_5_23.png', 'guru_2019_08_23.jpg', 'mango_1.jpg', 'mango2.jpg', 'mango05.jpg', 'mango_6.jpg', 'mango_11.jpg']