>>>t= ["dddd","a","bb","cccc"]
>>>t.sort(key=lambda s:len(s))
>>>print t
輸出:作爲input.t被根據與第二線路碼長度排序給出如何打印字符串列表按每個字符串的長度排序,最短的字符串在下面的python腳本中?
['a', 'bb', 'dddd', 'cccc']
列表噸。最後我打印它。 so ..現在我想輸入,排序列表並打印它.. 所有在一行代碼(如果可能)。 我試過其他方式,如..
>>> print input().sort(key=lambda s:len(s))
["aaa","bb","cccc","q"] # input
None #output
- i think it take return value of input(),i.e is None.
使用'sorted'而不是'sort'。 – Kevin