0

I have a list of strings. I want to sort this list with respect to the number (which is represented by the element) and the length of the element.

19206_63083.png must come before 1019209_63119.png

mylist.sort(key=lambda x: x.split("_",)[0])

This doesn't work. 10163841_15591.png comes before 1882828_15293.png. However it shouldn't, because the first part before underscore is in fact a larger number.

My desired output would be like this:

    my_list = [19206_63083.png,
    75131_63085.png,
    129899_63087.png,
    185747_63089.png,
    240948_63091.png,
    296626_63093.png,
    351670_63095.png,
    407945_63097.png,
    463197_63099.png,
    518738_63101.png,
    577527_63103.png,
    629717_63105.png,
    686448_63107.png,
    741521_63109.png,
    796843_63111.png,
    852569_63113.png,
    907921_63115.png,
    962966_63117.png,
    1019209_63119.png,
    1073904_63121.png,
    1129968_63123.png,
    1185872_63125.png,
    1241292_63127.png,
    1297920_63129.png,
    1353177_63131.png,
    1408595_63133.png,
    1463281_63135.png,
    1519764_63137.png,
    1630077_63141.png,
    1686200_63143.png,
    1797925_63147.png,
    1909026_63151.png,
    2019367_63155.png,
    2185323_63161.png,
    2241858_63163.png,
    2352508_63167.png,
    2408718_63169.png,
    2464518_63171.png,
    2576205_63175.png,
    2685535_63179.png,
    2741758_63181.png,
    2853860_63185.png,
    2963621_63189.png,
    3074643_63193.png,
    3130612_63195.png,
    3241831_63199.png,
    3299111_63201.png,
    ...
    50675605_17049.png,
    50731840_17051.png,
    50786088_17053.png,
    50842895_17055.png,
    50898226_17057.png,
    50952973_17059.png,
    51010247_17061.png,
    51064070_17063.png,
    51120808_17065.png]
kneazle
  • 335
  • 4
  • 14

1 Answers1

0

You need to replace the '-' with an "_"

and as the comment above states, you need to wrap it in an

int()
bravosierra99
  • 1,331
  • 11
  • 23
  • Oh in fact I did before posting here. It still doesn't work because it considers the part before underscore as a string. But I need to convert it to integer – kneazle Feb 20 '19 at 15:23