天天看点

python数组nonetype_无法反转Python中的列表,将“Nonetype”作为lis

正如jcomeau提到的,.reverse()函数将更改列表。它不返回列表,而是使qSort保持更改。

如果要“返回”反转的列表,以便可以像在示例中尝试的那样使用它,则可以执行方向为-1的切片

所以用print qSort[::-1]替换print qSort.reverse()

你应该知道切片,有用的东西。我并没有在教程中看到一个地方同时描述了所有内容,(http://docs.python.org/tutorial/introduction.html#lists并没有真正涵盖所有内容),所以希望这里有一些示例。

语法是:a[firstIndexInclusive:endIndexExclusive:Step]>>> a = range(20)

>>> a

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

>>> a[7:] #seventh term and forward

[7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

>>> a[:11] #everything before the 11th term

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

>>> a[::2] # even indexed terms. 0th, 2nd, etc

[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

>>> a[4:17]

[4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]

>>> a[4:17:2]

[4, 6, 8, 10, 12, 14, 16]

>>> a[::-1]

[19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

>>> a[19:4:-5]

[19, 14, 9]

>>> a[1:4] = [100, 200, 300] #you can assign to slices too

>>> a

[0, 100, 200, 300, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]