python中 sort()是列表的内建函数,一般不写参数(取默认值),无返回值,sort()会改变列表,原地排序,因此无需返回值。字典、元组、字符串不具有sort()方法,如果调用将会返回一个异常。
>>> help(list.sort)
Help on method_descriptor:
sort(...)
L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;
cmp(x, y) -> -1, 0, 1
例如:
>>> l=[3,4,1,2,7,5,6]
>>> l.sort()
>>> l
[1, 2, 3, 4, 5, 6, 7]
>>> d={'b':1,'c':3,'a':4,'f':6,'d':7}
>>> d.sort() #报错
AttributeError: 'dict' object has no attribute 'sort'
>>> t=(2,3,4,1,6)
>>> t.sort()#报错
AttributeError: 'tuple' object has no attribute 'sort'
>>> s='python'
>>> s.sort()#报错
AttributeError: 'str' object has no attribute 'sort'
(完)