how to arrange sequence png name

Question:

I’m looking for a solution that would help me do the following.

Suppose I have
so it will be arrange with 35_2_20 the last number 35_2_

list_ =[['35_2_20.png'],
        ['35_2_18.png'],
        ['35_2_9.png'],
        ['35_2_11.png'],
        ['35_2_7.png'],
        ['35_2_16.png'],
        ['35_2_6.png'],
        ['35_2_17.png'],
        ['35_2_1.png'],
        ['35_2_10.png'],
        ['35_2_19.png'],
        ['35_2_8.png'],
        ['35_2_4.png'],
        ['35_2_15.png'],
        ['35_2_3.png'],
        ['35_2_12.png'],
        ['35_2_2.png'],
        ['35_2_13.png'],
        ['35_2_5.png'],
        ['35_2_14.png']]

l’d like to rearrange it based upon an less to larg ordering. If there were a method to do it would do the following

list_= [['35_2_20.png'],
    ['35_2_19.png'],
    ['35_2_18.png'],
    ['35_2_17.png'],
    ['35_2_16.png'],
    ['35_2_15.png'],
    ['35_2_14.png'],
    ['35_2_13.png'],
    ['35_2_12.png'],
    ['35_2_11.png'],
    ['35_2_10.png'],
    ['35_2_9.png'],
    ['35_2_8.png'],
    ['35_2_7.png'],
    ['35_2_6.png'],
    ['35_2_5.png'],
    ['35_2_4.png'],
    ['35_2_3.png'],
    ['35_2_2.png'],
    ['35_2_1.png']]
Asked By: mosa lorans

||

Answers:

install natsort: natsort

pip install natsort

then:

from natsort import natsorted
natsorted(list_)[::-1]

#OR

natsorted(list_,reverse=True)

#output
[['35_2_20.png'],
 ['35_2_19.png'],
 ['35_2_18.png'],
 ['35_2_17.png'],
 ['35_2_16.png'],
 ['35_2_15.png'],
 ['35_2_14.png'],
 ['35_2_13.png'],
 ['35_2_12.png'],
 ['35_2_11.png'],
 ['35_2_10.png'],
 ['35_2_9.png'],
 ['35_2_8.png'],
 ['35_2_7.png'],
 ['35_2_6.png'],
 ['35_2_5.png'],
 ['35_2_4.png'],
 ['35_2_3.png'],
 ['35_2_2.png'],
 ['35_2_1.png']]
Answered By: Talha Tayyab

I don’t know a word of python, but in js i would try

const newList = list_.flat().map(el => parseInt(el.split('_').at(2).split('.').at(0)))
const list = list_.flat()
let result = [19]
let order = 0
for (const index of newList) {
  result[index] = [list[order]]
  order++
}
Answered By: Javier Gutiérrez
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.