python - Python2: List splitting syntax - tail of a list -
this question has answer here:
- explain slice notation 24 answers
why sequence[1:0] return empty list when sequence[1:] or sequence[1:n] (where n >= sequence length) returns tail of list successfully?
i'm pretty sure it's way python iterates on loop, can't fathom why wouldn't work, considering works numbers less zero.
example:
>>> l = [1,2,3,4] >>> l[1:] [2,3,4] >>> l[1:100] [2,3,4] >>> l[1:0] [] >>> l[1:0:1] [] >>> l[1:-2] [2]
to elements backwards, need pass step negative value
l = [1,2,3,4] print l[1:0:-1] # [2] when l[1:0] python takes the step value 1, default, step 1.
and when step greater 0 , start greater or equal stop, length of slice returned set 0. so, empty list returned.
if ((*step < 0 && *stop >= *start) || (*step > 0 && *start >= *stop)) { *slicelength = 0; } when use negative indices start or stop, python adds length of sequence it.
Comments
Post a Comment