Slicing是一种在有序的对象类型中(数组,元组,字符串)节选某一段的语法。

下面的代码都是在cmd命令行演示的:

>>> lst=[11,22,33,44,55]
>>> lst[0:2]=[111,222,333]
>>> lst
[111, 222, 333, 33, 44, 55]
>>>
>>> lst=[11,22,33,44,55]
>>> lst[0:4:2]=[888,999]
>>> lst
[888, 22, 999, 44, 55]

# 注意步长为2
>>> lst=[11,22,33,44,55]
>>> lst[0:4:2]=[666,777,000]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: attempt to assign sequence of size 3 to extended slice of size 2
>>>

#带有步长的时候,替换的元素必须和提供的个数一样,否则报错