- 2020-06-03 05:11
*views 3*- Python
- experience

There is an operation called sharding in the list

General form ：

l2=[4,6,1,7,9] e = l2[2:4] print(e)

The code runs as follows ：[1, 7]

From the perspective of subscript, it's easy to know that subscript two corresponds to exactly the element 3, value 1

however , But no subscript 4 Elements of 5, value 9

therefore , We come to a conclusion ：

<>Python Slice operations of include left and not right

<> Operating range [m,n]—> Actual scope [m,n-1]

Suppose there is one list

l = [3,4,5,1,7]

If we want to extract the elements 3,5,7 What about ?

This involves the step size parameter in the slicing operation

The form is as follows ：

l[2:4:1]

<> In slice 1 Is the step size parameter

Overall representation from subscript 2 Start to remove , every time +1, To subscript 3 stop it

eg

l2=[4,6,1,7,9] e = l2[2:5:1] print(e)

Output results ：[1,7,9], It also proves that :

<> The default step size parameter for fragmentation is 1

From the above example, it is easy to see that the default step size parameter of fragmentation operation is a positive number , Can our step size be negative ? Similarly, can our subscript be negative ?

One . Negative subscript

l4 = [3,5,1,6,2] e4 = l4[-4:-2] print(e4)

The output is ：[5,1]

At this time , We need to popularize a knowledge :

<> When subscript is negative , The subscript of the last element of the list is -1

Namely ：2 The subscript of is -1, And it's the same as subscript timing , The higher the subscript value to the right

, So when the subscript is negative , The maximum subscript is -1( be careful , When the step size is positive , The start subscript of fragmentation operation must be less than the end subscript , It can be understood that the subscript increases from left to right when the step size is gradually increased by a positive number )

array l4 The subscripts corresponding to the elements in are 3 --> -5 , 5 --> -4 , 1 --> -3 , 6 --> -2 , 2 --> -1

however , adopt l4 Instance output for , We can see again , Output order is still [5,1], Output from left to right

At this time , It's about the default step size of the sharding operation

We mentioned in the above that the default of fragmentation operation is 1, So it's easy to meet subscripts -4, take out -4 Elements of , then -4+1 Offset , Remove subscript -3 Elements of , adopt **[m,n]–>[m,n-1]**, knowable -2-1

== -3, So subscript to -3 Stop , Finally, the subscript is -4 and -3 Elements of

that , What if we want to slice from right to left ?

above-mentioned , The default step size for fragmentation is 1, From left to right , So if the step size is -1, Is it right to left

l5=[4,5,7,1,9] e5 = l5[-2:-4:-1] print(e5)

Output results ：[1,7]

We mentioned above , When the step size is positive , Fragment start subscript must be less than end subscript , But when the step size is negative , Fragment start subscript must be greater than fragment end subscript ( Otherwise, an empty list will be returned )

Technology

- Java378 articles
- Python197 articles
- Linux110 articles
- MySQL83 articles
- Vue78 articles
- SpringBoot68 articles
- Spring61 articles
- javascript56 articles
- more...

Daily Recommendation

views 2

©2019-2020 Toolsou All rights reserved,

C Language programming to find a student's grade java Realize the function of grabbing red packets Random forest R Language implementation QCustomPlot series (5)- Real time dynamic curve Software testing BUG describe use C++ I want to talk to you “ Prototype mode ” （ copy / copy constructor ）JS How to operate China's longest high speed rail officially opened ! The fastest way to finish the race 30.5 hour Zimi apologizes ：33W Gan charger delayed due to force majeure 2021 year 2 Chinese programming language ranking