Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
例如:
x = [0] * 10 x[2:8] = [1] * 2
结果变为:
[0,0,1,1,0,0]
为什么会这样?我没有改变 x 的长度,但它自动变成了 6。
此外,作业被分配到 x 列表的中间部分,有人可以告诉我为什么吗?
你确实改变了长度。您从 2 到 8(不包括 8)中选择了长度为 6 的切片,并将其替换为长度为 2 ( [1] * 2) 的列表。换句话说,你剪掉了列表的中间部分并将一个较小的中间部分放回原处。
[1] * 2