Home > database >  Python: Sorting by two numbers (not ascii)
Python: Sorting by two numbers (not ascii)

Time:10-13

I've got list of paths for example:

list = ['abcdef\\02\\01.jpg', 'abcdef\\02\\10.jpg', 'abcdef\\02\\11.jpg',
        'abcdef\\02\\02.jpg', 'abcdef\\02\\03.jpg', 'abcdef\\04\\01.jpg', ...]

I want to sort it first by '02' and then by '01' in 'abcdef\02\01.jpg'

I tried

list = ['abcdef\\02\\01.jpg', 'abcdef\\02\\10.jpg', 'abcdef\\02\\11.jpg',
        'abcdef\\02\\02.jpg', 'abcdef\\02\\03.jpg', 'abcdef\\04\\01.jpg', ...]

def myFunc(e):
    print(e)
    a = e.split('\\')[-2]
    b = e.split('\\')[-1][0:-4]
    return int(a b)
list.sort(key=myFunc)

Output what i got from aforementioned code:

list = ['abcdef\\02\\1.jpg', 
       ... ,
       'abcdef\\02\\9.jpg',
       'abcdef\\04\\1.jpg',
       ... ,
       'abcdef\\04\\9.jpg',
       'abcdef\\02\\10.jpg']

Wanted output:

list = ['abcdef\\02\\1.jpg', 
       ... ,
       'abcdef\\02\\9.jpg',
       'abcdef\\02\\10.jpg',
       ... ,
       'abcdef\\02\\17.jpg',
       'abcdef\\03\\1.jpg']

It's sorting by ASCII. How do I sort it numerically?

CodePudding user response:

Thanks you guys @mkrieger1 @Johnny Mopp for that answer.

Code:

list = ['abcdef\\02\\01.jpg', 'abcdef\\02\\10.jpg', 'abcdef\\02\\11.jpg',
    'abcdef\\02\\02.jpg', 'abcdef\\02\\03.jpg', 'abcdef\\04\\01.jpg', ...]

def myFunc(e):
    print(e)
    a = e.split('\\')[-2]
    b = e.split('\\')[-1][0:-4]
 -  return int(a b)
    return int(a), int(b)
    list.sort(key=myFunc)

Output:

['arifureta\\02\\1.jpg', 'arifureta\\02\\2.jpg', ..., 'arifureta\\02\\9.jpg',
 'arifureta\\02\\10.jpg', 'arifureta\\02\\11.jpg', 'arifureta\\02\\12.jpg',
 ... , 'arifureta\\02\\20.jpg', 'arifureta\\02\\21.jpg', 'arifureta\\03\\1.jpg',
 'arifureta\\03\\2.jpg', ...]

CodePudding user response:

You can do the sorting like this,

sorted(l, key=lambda x: (int(x.split('\\')[1]), int(x.split('\\')[-1].split('.')[0])))

# Output
['abcdef\\02\\01.jpg',
 'abcdef\\02\\02.jpg',
 'abcdef\\02\\03.jpg',
 'abcdef\\02\\10.jpg',
 'abcdef\\02\\11.jpg',
 'abcdef\\04\\01.jpg']

It's essential that the format of the string remain the same to work the above code.

CodePudding user response:

Trying to sort using pkg_resources package, which generally use for versions.

Code:

from pkg_resources import parse_version 
sorted(Mylist, key=parse_version)]

Output:

['abcdef\\02\\01.jpg',
 'abcdef\\02\\02.jpg',
 'abcdef\\02\\03.jpg',
 'abcdef\\02\\10.jpg',
 'abcdef\\02\\11.jpg',
 'abcdef\\04\\01.jpg']

With complicated inputs' output:

['abcdef\\02\\03.jpg',
 'abcdef\\02\\05.jpg',
 'abcdef\\02\\05.2.jpg',
 'abcdef\\02\\10.jpg',
 'abcdef\\02\\11.jpg',
 'abcdef\\03\\heloo.jpg',
 'abcdef\\04\\01.jpg']

Your actual input:

li =  ['arifureta\\03\\2.jpg', 'arifureta\\02\\9.jpg','arifureta\\02\\1.jpg', 'arifureta\\02\\2.jpg', 'arifureta\\02\\20.jpg', 'arifureta\\02\\21.jpg', 'arifureta\\03\\1.jpg', 'arifureta\\02\\10.jpg', 'arifureta\\02\\11.jpg', 'arifureta\\02\\12.jpg']

['arifureta\\02\\1.jpg',
 'arifureta\\02\\2.jpg',
 'arifureta\\02\\9.jpg',
 'arifureta\\02\\10.jpg',
 'arifureta\\02\\11.jpg',
 'arifureta\\02\\12.jpg',
 'arifureta\\02\\20.jpg',
 'arifureta\\02\\21.jpg',
 'arifureta\\03\\1.jpg',
 'arifureta\\03\\2.jpg']
  • Related