How to specify 2 keys in python sorted(list)?

后端 未结 4 565
我在风中等你
我在风中等你 2021-01-17 10:50

How do I sort a list of strings by key=len first then by key=str? I\'ve tried the following but it\'s not giving me the desired sort:



        
4条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-17 11:44

    Another form that works without a lambda:

    >>> [t[1] for t in sorted((len(s),s) for s in ls)]
    ['bar', 'foo', 'barbar', 'foobar']
    

提交回复
热议问题