List

列表切片

a[start:end] # items start through end-1
a[start:]    # items start through the rest of the array
a[:end]      # items from the beginning through end-1
a[:]         # a copy of the whole array

切片操作也提供一个步长参数,如下

[start:end:step] # start through not past end, by step

因此,通过-1步长就可以反转列表

In [1]: a = [1, 2, 3]

In [2]: b = a[:: -1]

In [3]: print(b)
[3, 2, 1]

results matching ""

    No results matching ""