Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

windows - Understanding default arguments in range and list slicing in Python

Good day. I don’t entirely understand this code. I understand that the syntax for range is range(start, stop, step/stride). And that the default arguments are range(start = 0, stop=, step= 1), so what I understand from your code is: for i in range(len(list) - 1, -1, -1): is that the index (i) should start at len(lst) - 1, because you want to start at the index of end of the list, which would be len(list) - 1, because in a list of 10 elements it would be index 9, as index starts from zero. I also understand the step/stride portion, because we want i to iterate backwards from the last index.

But what I do not understand is why the end argument is -1, which is at the end of the list. Since we are iterating backwards, shouldn't it end at the start of the list, which should be zero? That is the portion I don’t understand. Please can you shed more light on this?

Sorry to bother you again but I also don’t fully understand the list slice. I know the syntax for list slicing is list[start : stop : step], I understand the step or stride portion and I know the default arguments are list[start= 0 : stop = -1 and step =1]. I understand the step part since we want to iterate backwards when using list[: : -1].

What I don’t understand, is how python knows to start at the end of the list since the default argument is still -1 or is it because 0-1=-1 so it starts from the end?

Furthermore, how does it know to end at the beginning rather than at -1, which is the default argument?

Thank you very much.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

From the official documentation:

For a negative step, the contents of the range are still determined by the formula r[i] = start + step*i, but the constraints are i >= 0 and r[i] > stop.

r[i] > stop means that stop will never be part of the sequence. Thus, to end the range at 0, the first index, you have to wrie range(len(list)-1, -1, -1), where the second argument is one step further than the last element you want to be included in the range. For example range(5, 0, -1) contains 5, 4, 3, 2, 1, since it stops when the next element (0) would have been smaller or equal to the second argument (0).

Why list slicing works the way it does is just because that is how the python interpreter was made to understand it. You can think of negative indexes as normal indexes but backwards (starting at -1, not -0).

[1, 2, 3, 4][-1:0:-1] does not work for reversing the list, since it stops one element before index 0, returning [4,3,2], and [1,2,3,4][-1:-1:-1] does not work since the first element is the element to stop at, so the returned list is empty. If you omit start or stop, the slice just includes the rest of the list in that direction ([1,2,3,4][1:] == [2,3,4] and [1,2,3,4][:3] == [1,2,3]). Thus there is another way to reverse the order of lists in python: [1,2,3,4][::-1].


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...