Python list 备查

# 原始数据
test = [1, 2, 3, 4, 5, 6, 7, 8, 9]
# 正向切片
print(test[2:4])

[3, 4]

# 反向切片
print(test[-4:-2])

[6, 7]

# 从头切片
print(test[:-2])

[1, 2, 3, 4, 5, 6, 7]

# 从尾切片
print(test[-2:])

[8, 9]

# 分割
print(test[::2])

[1, 3, 5, 7, 9]

# 倒序
print(test[::-1])

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

# 分割后倒序
print(test[::-2])

[9, 7, 5, 3, 1]

原文地址:https://www.cnblogs.com/congxinglong/p/14699109.html