


About the subscript problem of Sequence slicing and its solution
This article mainly introduces to you the relevant information about the Sequence slice subscript problem in Python. The article introduces it in detail through the example code, which has certain reference and learning value for everyone. Friends who need it Let’s take a look together below.
Preface
In python, slicing is a frequently used syntax, whether it is a tuple, a list or String, the general syntax is:
sequence[ilow:ihigh:step] # ihigh
, step can be empty; for the sake of simplicity and ease of understanding, the usage of step is temporarily excluded Consider
Let’s briefly demonstrate the usage
1 2 3 4 5 |
|
The syntax is very concise and easy to understand. This syntax It is simple and easy to use in our daily use, but I believe that when we use this slicing syntax, we will habitually follow some rules:
ilow, ihigh are both smaller than sequence. Length
ilow < ihigh
Because in most cases, only by following the above rules can we get what we expected Result! But what if I don't follow it? What happens to slicing?
No matter we are using tuples, lists or strings, when we want to get an element, we will use the following syntax:
1 2 3 |
|
Let’s call the 1 and 2 that appear above subscripts. Whether it is a tuple, a list or a string, we can use the subscript to get the corresponding value, but If the subscript exceeds the length of the object, an index exception (IndexError) will be triggered
1 2 3 4 5 6 7 8 |
|
So what about slicing? The two syntaxes are very similar, assuming that ilow and ihigh are respectively 10 and 20, then what is the result?
Reappearance of the scene
##
1 2 3 4 |
|
1 2 3 |
|
1 2 3 4 5 6 |
|
Principle Analysis
# #Before we reveal it, we must first figure out how python handles this slice. We can use the dis module to help:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
|
Here is a brief introduction to the dis module , experienced old drivers all know that when Python interprets a script, there is also a compilation process. The result of the compilation is the pyc file we often see, which contains the bytecode composed of codeobject object
, and dis displays these bytecodes in a more impressive way, allowing us to see the execution process. The following is an explanation of the output columns of dis:
- The first column is a number is the line number of the original source code.
- The second column is the offset of the bytecode: LOAD_CONST is at line 0. And so on.
- The third column is the human-readable name of the bytecode. They are prepared for programmers
- The fourth column represents the parameters of the instruction
- The fifth column is the actual parameters after calculation
. The main difference is: test.py slices use bytecode SLICE +3 is implemented, and the test2.py single subscript value is mainly implemented through the bytecode BINARY_SUBSCR. As we guessed, similar syntax is completely different code. Because what we want to discuss is slicing (SLICE+ 3), so we will not expand BINARY_SUBSCR anymore. Interested children can check the relevant source code to learn about the specific implementation. Location: python/object/ceval.c Then let’s discuss SLICE+3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
|
Although the above code is a bit long, the key places have been commented out, and we only need to pay attention to those places. As above, we know that we must eventually execute m->sq_slice(s, i1, i2)
, but this sq_slice is a bit special, because different objects have different corresponding functions. The following are the corresponding functions:
1 2 3 4 5 6 7 8 |
|
Because the function implementations of the three of them are roughly the same, we only need to analyze one of them. The following is the analysis of the slicing function of the list:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|
in conclusion
As can be seen from the slicing function corresponding to the sq_slice function above, if when using slicing, the left and right subscripts are greater than the length of the sequence, they will be reassigned to the length of the sequence, so our initial slicing: print a[10:20]
, what actually runs is: print a4:4
. Through this analysis, in the future when you encounter a slice with a subscript greater than the object length, you should I won’t be confused anymore~
The above is the detailed content of About the subscript problem of Sequence slicing and its solution. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics





eWe often use Excel to make some data tables and the like. Sometimes when entering parameter values, we need to superscript or subscript a certain number. For example, mathematical formulas are often used. So how do you type the subscript in Excel? ?Let’s take a look at the detailed steps: 1. Superscript method: 1. First, enter a3 (3 is superscript) in Excel. 2. Select the number "3", right-click and select "Format Cells". 3. Click "Superscript" and then "OK". 4. Look, the effect is like this. 2. Subscript method: 1. Similar to the superscript setting method, enter "ln310" (3 is the subscript) in the cell, select the number "3", right-click and select "Format Cells". 2. Check "Subscript" and click "OK"

The clustering effect evaluation problem in the clustering algorithm requires specific code examples. Clustering is an unsupervised learning method that groups similar samples into one category by clustering data. In clustering algorithms, how to evaluate the effect of clustering is an important issue. This article will introduce several commonly used clustering effect evaluation indicators and give corresponding code examples. 1. Clustering effect evaluation index Silhouette Coefficient Silhouette coefficient evaluates the clustering effect by calculating the closeness of the sample and the degree of separation from other clusters.

In Python, you can use string slicing to get substrings in a string. The basic syntax of string slicing is "substring = string[start:end:step]".

Known for its powerful performance and versatile features, the iPhone is not immune to the occasional hiccup or technical difficulty, a common trait among complex electronic devices. Experiencing iPhone problems can be frustrating, but usually no alarm is needed. In this comprehensive guide, we aim to demystify some of the most commonly encountered challenges associated with iPhone usage. Our step-by-step approach is designed to help you resolve these common issues, providing practical solutions and troubleshooting tips to get your equipment back in peak working order. Whether you're facing a glitch or a more complex problem, this article can help you resolve them effectively. General Troubleshooting Tips Before delving into specific troubleshooting steps, here are some helpful

To solve the problem that jQuery.val() cannot be used, specific code examples are required. For front-end developers, using jQuery is one of the common operations. Among them, using the .val() method to get or set the value of a form element is a very common operation. However, in some specific cases, the problem of not being able to use the .val() method may arise. This article will introduce some common situations and solutions, and provide specific code examples. Problem Description When using jQuery to develop front-end pages, sometimes you will encounter

There are three methods to remove slice elements in Go language: append function (not recommended), copy function and manually modifying the underlying array. The append function can delete tail elements, the copy function can delete middle elements, and manually modify the underlying array to directly assign and delete elements.

Many users have tried to update the win11 system, but found that the start menu cannot be used after the update. This may be because there is a problem with the latest update. We can wait for Microsoft to fix or uninstall these updates to solve the problem. Let's take a look at it together. Solution. What to do if the start menu cannot be used after win11 is installed. Method 1: 1. First open the control panel in win11. 2. Then click the "Uninstall a program" button below the program. 3. Enter the uninstall interface and find "View installed updates" in the upper left corner. 4. After entering, you can view the update time in the update information and uninstall all recent updates. Method 2: 1. In addition, we can also directly download the win11 system without updates. 2. This is a product without the most

What are the questions involved in the Yulong 8 Wine Master exam? What is the corresponding answer? How to pass the exam quickly? There are many questions that need to be answered in the Master of Wine Examination activities, and we can refer to the answers to solve them. These questions all involve knowledge of wine. If you need a reference, let’s take a look at the detailed analysis of the answers to the Yakuza 8 Wine Master exam questions! Detailed explanation of answers to questions in the Rulong 8 Wine Master exam 1. Questions about "wine". This is a distilled liquor produced by a distillery established by the royal family. It is brewed from the sugar of sugarcane grown in large quantities in Hawaii. What is the name of this wine? Answer: Rum 2. Question about "wine". The picture shows a drink made from dry ginseng and dry vermouth. It is characterized by the addition of olives and is known as "cockney"
