如何按key=len
first 然后 by对字符串列表进行排序key=str
?我已经尝试了以下方法,但它没有给我想要的排序:
>>> ls = ['foo','bar','foobar','barbar']
>>>
>>> for i in sorted(ls):
... print i
...
bar
barbar
foo
foobar
>>>
>>> for i in sorted(ls, key=len):
... print i
...
foo
bar
foobar
barbar
>>>
>>> for i in sorted(ls, key=str):
... print i
...
bar
barbar
foo
foobar
我需要得到:
bar
foo
barbar
foobar