Python simple string (back)

Yes, I know that there are many similar questions. But I just can’t find what I was looking for.

My misunderstanding is related to the reverse slicing.

my_jumble = ['jumbly', 'wumbly', 'number', 5] print(my_jumble[:1:-1]) 

Now I have found that the result will be

 [5, 'number'] 

So, I thought that maybe I would go through it by changing the ends of this line cut.

 print(my_jumble[:2:-1]) 

I really was sure that Python would give me something like

 [5, 'number', 'wumbly'] 

Instead, he gave me this, which made me completely lost ...

 [5] 

Can someone explain what is going on here? I am new to Python and find this very confusing. Thanks for any help.

+5
source share
4 answers

I think one of the easiest ways to understand what is happening in the code is to realize that reverse slicing changes the direction of the index of your list (visually, this is like changing the list) before slicing , but the indices of the elements in the list by themselves do not change.

Thus, when you have a list like this:

 ['jumbly', 'wumbly', 'number', 5] 0 1 2 3 #<-- index 

making it a reverse read (adding -1 as the third indexer), you make it look like this (because now it is indexed from the last to the first, and not from the first to the last):

 [5, 'number', 'wumbly', 'jumbly'] 3 2 1 0 #<-- index 

and then when you cut the “start” into one ( :1 ), you get everything from the “start” (now the “start” 3 ) and stops when you view 1 :

 [5, 'number', 'wumbly', 'jumbly'] 3 2 1 0 #<-- index ^ ^ x grab! grab! nope! 

So you got your return:

 [5, 'number'] 

The same principle applies when you drop a slice using [:2:-1] :

 [5, 'number', 'wumbly', 'jumbly'] 3 2 1 0 #<-- index ^ x grab! nope! 

So you got your result:

 [5] 

Now, using this principle, you know what to add as a second indexer if you want to return what you want: zero! → [:0:-1] :

 [5, 'number', 'wumbly', 'jumbly'] 3 2 1 0 #<-- index ^ ^ ^ x grab! grab! grab! nope! 

Then you will get the result you want:

 [5, 'number', 'wumbly'] 
+7
source

The syntax you use is list[start:stop:step]

If you do not enter a value for start , not even zero , then python decides the appropriate start. This will be 0 for the positive step and the last element for the negative step.

So, in the first example, you actually say that you select all the elements, starting from 0 to 1 , but in reverse order. So he printed [5,'number']

In your second example, what you say selects all objects, from the first to the third, in reverse order. So, in reverse order, you start at 5 , the third item in your list is 'number' , but since you said only until the third, it stops right there.

Since you gave a positive value for stop , it will be left-right, therefore, the third element is in the correct order in your case.

Also note that in the python list[start: stop] equivalent to [start: stop) , the first element is considered, excluding the right border.

+1
source

array[start:end:step] means start with the start index, and then add step to each cycle of the cycle and split the cycle if the index becomes greater than or equal to end . If start omitted, it is 0. If end omitted, it is set to len(array) . If start or end negative value, it is set to len(array) + start or len(array) + end . If step negative, it is added to the current index for each cycle of the cycle, but the condition for continuing the cycle is current_index > end , and not current_index < end when step positive.

So ['jumbly', 'wumbly', 'number', 5][:1:-1] means that we need to start accepting elements from the len(array) index to index 1 (not including) - therefore we are assigned the elements ['number', 5] :

 >>> ['jumbly', 'wumbly', 'number', 5][:1:-1] >>> [5, 'number'] 

['jumbly', 'wumbly', 'number', 5][:2:-1] means that you need to start accepting elements from index len(array) to index 2 (the value of 'number' stored in the list at index 2) (not including) - therefore we have [5] :

 >>> ['jumbly', 'wumbly', 'number', 5][:2:-1] >>> [5] 

Or is it better to explain with a string whose symbols are indices:

 >>> '0123'[:1:-1] >>> '32' >>> '0123'[:2:-1] >>> '3' 
+1
source

@HarryLens To do what you really want, you will need to do it like this.

 print(my_jumble[-1:0:-1]) 

Even this would do:

 print(my_jumble[:0:-1]) 

I think you thought that when you do it at -1, the list changes to the opposite. You see that the first number represents the starting position, and the second number represents the ending position (of the desired fragment) in the current list, not the inverted list. See this.

0
source

Source: https://habr.com/ru/post/1244931/


All Articles