Python 3.x关于列表使用sort()函数返回None的解决方案
问题描述
sort()方法是在原地对列表排序,是对原列表的直接操作,并不会返回一个新的列表。sort()方法需要单独使用,如果和赋值,打印等方法一起使用,结果会返回None
b = [1, 2, 8, 9, 10, 10]
c = b.sort()
print(c)
None
解决方案
利用sorted()函数,如
b = [1, 2, 8, 9, 10, 10]
c = sorted(b,reverse = True)#reverse = True为降序,默认为升序,即reverse = False
print(c)
[10, 10, 9, 8, 2, 1]