Section 22.1: 使用第三个参数 “步长 ”

lst = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
lst[::2]
# Output: ['a', 'c', 'e', 'g']

lst[::3]
# Output: ['a', 'd', 'g']

使用第三个参数 “步长 ”

Section 22.2: 从列表中选择子列表

lst = ['a', 'b', 'c', 'd', 'e']
lst[2:4]
# Output: ['c', 'd']

lst[2:]
# Output: ['c', 'd', 'e']

lst[:4]
# Output: ['a', 'b', 'c', 'd']

从列表中选择子列表

Section 22.3: 用切分法逆转列表

a = [1, 2, 3, 4, 5]

# 在列表中倒退 (step=-1)
b = a[::-1]

# 内置列表法来反转 `a`
a.reverse()

if a == b:
    print(True)

print(b)

# Output:
# True
# [5, 4, 3, 2, 1]

用切分法逆转列表

Section 22.4: 使用切分法移动列表

def shift_list(array, s):
    """Shifts the elements of a list to the left or right.
    Args:
        array - the list to shift
        s - the amount to shift the list ('+': right-shift, '-': left-shift)
    Returns:
            shifted_array - the shifted list
    """
    # calculate actual shift amount (e.g., 11 --> 1 if length of the array is 5)
    s %= len(array)

    # reverse the shift direction to be more intuitive
    s *= -1

    # shift array with list slicing
    shifted_array = array[s:] + array[:s]

    return shifted_array

my_array = [1, 2, 3, 4, 5]

# negative numbers
>>> shift_list(my_array, -7)
[3, 4, 5, 1, 2]

# no shift on numbers equal to the size of the array
>>> shift_list(my_array, 5)
[1, 2, 3, 4, 5]

# works on positive numbers
>>> shift_list(my_array, 3)
[3, 4, 5, 1, 2]

使用切分法移动列表