Sorting a list of strings with a specific method

后端 未结 2 1388
小鲜肉
小鲜肉 2021-01-22 16:22

let\'s say i have a list of strings like this

L = [\'5\', \'3\', \'4\', \'1\', \'2\', \'2 3 5\', \'2 4 8\', \'5 22 1 37\', \'5 22 1 22\', \'5 22 1 23\', ....]
         


        
2条回答
  •  孤独总比滥情好
    2021-01-22 16:56

    A slightly different approach than @Daniel's one.

    idx = sorted(range(len(L)), key=lambda i: int(''.join(L[i].split())))
    L = [L[i] for i in idx]
    

    output

    ['1',
     '2',
     '3',
     '4',
     '5',
     '2 3 5',
     '2 4 8',
     '5 22 1 22',
     '5 22 1 23',
     '5 22 1 37']
    

提交回复
热议问题