无法在Python中反转列表,将“ Nonetype”作为列表

无法在Python中反转列表,将“ Nonetype”作为列表,第1张

无法在Python中反转列表,将“ Nonetype”作为列表

正如jcomeau提到的,该

.reverse()
函数将列表更改到位。它不返回列表,而是进行
qSort
更改。

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

因此,更换

print qSort.reverse()
print qSort[::-1]


您应该了解切片及其有用的内容。我没有真正在本教程中看到一个同时描述了所有内容的地方((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]


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5663819.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存