python2.7 - Python:为什么不可以这样:print list1.sort(),而是先:list1.sort()再print list1?
迷茫
迷茫 2017-04-17 15:41:36
0
6
614
迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(6)
阿神

Because the type returned by the sort method of List is <type 'NoneType'>, your print here is not list1.

>>> list1=[3,2,5,6,1]
>>> print type(list1.sort())
<type 'NoneType'>
迷茫

Because sort has no return value

PHPzhong

I just encountered this problem recently. The reason is that list1.sort() only sorts the elements in list1, and then returns NoneType. If you want to get the sorted list directly, you should use the sorted function.

Ty80

You can use sorted

>>> list1=[3,2,5,6,1]
>>> print sorted(list1)
[1, 2, 3, 5, 6]
黄舟

sorted returns a new list. sort sorts in place.

伊谢尔伦

Because the function of list.sort() is to sort the elements of the list, rather than turning list.sort() itself into a sorted list

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template