A refreshing story about Python's Slice

Discomfort in slice processing

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.

The true nature of the sense of incongruity

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.

But why Length?

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.

Actually easy to understand (?) Array operation using Length

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.

so

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

A refreshing story about Python's Slice
A sloppy story about Python's Slice
A story about using Python's reduce
A story about remodeling Lubuntu into a Chromebook
A story about machine learning with Kyasuket
A story about Python pop and append
A story about a 503 error on Heroku open
A story about operating a GCP instance from Discord
A story about Go's global variables and scope
A story about displaying article-linked ads on Jubatus
A story about implementing a login screen with django
A story about running Python on PHP on Heroku
A story about modifying Python and adding functions
A story about data analysis by machine learning
Escape from Python's virtual environment ~ A story about being trapped in a virtual environment I created ~
A story about predicting exchange rates with Deep Learning
A story about migrating entire Linux disk via SSH
A memorandum about matplotlib
A memorandum about Nan.
A story about making Hanon-like sheet music with Python
A story about trying a (Golang +) Python monorepo with Bazel
A story about kindergartens, nursery schools, and children's gardens
A story about reflecting Discord activity in Slack Status
A note about __call__
A story about struggling to loop 3 million ID data
A story about how theano was moved with TSUBAME 2.0
A note about subprocess
A story about changing the master name of BlueZ
A note about mprotect (2)
A story about a Linux beginner passing LPIC101 in a week
A story about a Linux beginner putting Linux on a Windows tablet
A story about stumbling through PATH after installing anaconda
A story that took time to understand python's argsort (memorial)
A story about creating a UNIX / Linux compatible OS from scratch
A story about how to specify a relative path in python.
A story about competing with a friend in Othello AI Preparation
A story about installing matplotlib using pip with an error
A little deeper story about blockchain, ticking the digital world
A story about an amateur making a breakout with python (kivy) ②
A story about how to deal with the CORS problem
A story about an amateur making a breakout with python (kivy) ①
A story about a war when two newcomers developed an app
A story about making a tanka by chance with Sudachi Py
A story about clustering time series data of foreign exchange
A story about a 40-year-old engineer manager passing "Deep Learning for ENGINEER"
A story about trying to implement a private variable in Python.
A story about a tragedy happening by exchanging commands in chat
A story about a GCP beginner building a Minecraft server on GCE
A story about a python beginner stuck with No module named'http.server'
A note about KornShell (ksh)
A memorandum about correlation [Python]
A note about TensorFlow Introduction
A memorandum about Python mock
A little more about FIFO
A note about [python] __debug__
A story about trying to automate a chot when cooking for yourself
A story about adding a REST API to a daemon made with Python
A story about a build error in a shared library that references libusb
A story about putting Chrome OS (not Chromium) on surface GO
A story about developing a soft type with Firestore + Python + OpenAPI + Typescript
A story about trying to run multiple python versions (Mac edition)