My Python List of string is something like x
but long enough:
x = ['aaa','ab','aa','c','a','b','ba']
I wants to sort this list as: ['a', 'b', 'c', 'aa', 'ab', 'ba', 'aaa']
and I did as follows in two steps:
>>> x.sort()
>>> x.sort(key=len)
>>> x
['a', 'b', 'c', 'aa', 'ab', 'ba', 'aaa']
But I need in one-step: I also tied using lambda
function (taken help):
>>> x.sort(key=lambda item: (item, len(item)))
>>> x
['a', 'aa', 'aaa', 'ab', 'b', 'ba', 'c']
But not as I desired:
Is it possible in one-step? Please me.
My Python:
~$ python --version
Python 2.6.6
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…