>>>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