python中的list.sort()不能直接用print打印

sort()

Sort the list in ascending order and return None.

按升序对列表排序并返回None。

>>> ls = [1,5,2,4]
>>> ls.sort()     #先排序,然后再打印,可出现结果
>>> print(ls)
[1, 2, 4, 5]
>>> ls = [1,3,2,1]
>>> print(ls.sort()) #直接打印排序结果啥也没有
None

如果实在想直接打印,可以用内置函数sorted(),写为:

>>> ls = [1,5,2,4]
>>> print(sorted(ls))
[1, 2, 4, 5]

猜你喜欢

转载自blog.csdn.net/panlan7/article/details/123997928