Python 3.2.3 (default, Apr 11 2012, 07:15:24) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> u = [4, 5, 6, 7, 8, 9]
>>> u[1::1] = [3, 2, 1, 0]
>>> u
[4, 3, 2, 1, 0]
>>> u[9:0:-1] = [8, 7, 6, 5]
>>> u
[4, 5, 6, 7, 8]
>>> u[9:0:-1] = [16, 12, 8]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: attempt to assign sequence of size 3 to extended slice of size 4
>>> u
[4, 5, 6, 7, 8]
>>>
Expected behaviour: no exception thrown on final assignment statement; u
should print on final line as [4, 8, 12, 16]
.
I can assign to an extended slice whose step is 1, even if the iterable I'm assigning is "the wrong length". Why then can't I assign to an extended slice whose step is -1 and have it work in the obvious way?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…