That's the above: A sloppy story about Slice in Python
Perhaps most programmers are conscious of specifying Index as a subscript when they want to extract an element from an array.
+++++++++++++++++++++++++++++++++++++
element + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+++++++++++++++++++++++++++++++++++++
index + 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 +
If you want to extract the 7th element of an array, it's unlikely that any programmer will find the code and results below.
l=[1,2,3,4,5,6,7,8,9]
print(l[6])
7
Therefore, even when performing a slice operation on an array, it naturally tries to specify the range with Index.
l=[1,2,3,4,5,6,7,8,9]
print(l[6:7])
[7]
oh ... I intended to specify Index 6-7, so the expected result is
[7,8]
However, the reality was ruthless.
In conclusion, it was Length, not Index, that should be an array subscript in slice processing.
+++++++++++++++++++++++++++++++++++++
element + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +
+++++++++++++++++++++++++++++++++++++
length 0___1___2___3___4___5___6___7___8___9
l=[1,2,3,4,5,6,7,8,9]
print(l[6:7])
[7]
If you compare the length chart and the process and replace it in your brain that the subscript is Length instead of Index, it will be the process to acquire the element between lengths 6 to 7, so there will be no discomfort in the output result. It was refreshing.
I do not know. The process of specifying the Length and acquiring the index range like the range function is certainly intuitive and convenient, but even so, the index standard is so much that the subscript notation based on the Length is mixed even for the mainstream array subscripts. After all, I didn't know what a wonderful reason for this.
I was negative about using Length as an array index above, but I'm sure everyone has tried to use Length as an index (probably).
When I was little, I used to buy Delphi 2.0 and my first Delphi-like reference book at my neighborhood Laox (I don't know why I chose Delphi, it's probably the cheapest GUI programming language. That's why), when I started studying the program triumphantly. While looking at the sample reference book, I wrote the code, repeated errors and corrections, and came to the array page.
Reference book "To get the first element of the array, specify 0 as the subscript." I "Why?"
At that time, no one around me could answer that question, and I imprinted it like a curse, with 0 when I wanted to take the first element and 1 when I wanted to take the next element.
So, if you don't know programming at all, you may accept the discomfort I had this time without even feeling discomfort in the first place. If a language in which all array operations are performed on the Length basis became mainstream,
Reference book "To get the first element of the array, specify 1 as the subscript." I "I see"
There may be such a world.
In conclusion, I didn't agree with it, but I understood it, so it wasn't very refreshing.
Nonetheless, it would be a problem if array operations based on Length became the mainstream in the world, and I hope that array subscripts will continue to be unified based on Index as much as possible. I will end it.
Recommended Posts