Nested lambda statements when sorting lists

前端 未结 8 1027
再見小時候
再見小時候 2020-12-03 21:20

I wish to sort the below list first by the number, then by the text.

lst = [\'b-3\', \'a-2\', \'c-4\', \'d-2\']

# result:
# [\'a-2\', \'d-2\', \'b-3\', \'c-         


        
8条回答
  •  粉色の甜心
    2020-12-03 22:02

    lst = ['b-3', 'a-2', 'c-4', 'd-2']
    def xform(l):
        return list(map(lambda x: x[1] + '-' + x[0], list(map(lambda x: x.split('-'), lst))))
    lst = sorted(xform(lst))
    print(xform(lst))
    

    See it here I think @jpp has a better solution, but a fun little brainteaser :-)

提交回复
热议问题