Home  >  Article  >  Backend Development  >  What does ::-1 stand for in python?

What does ::-1 stand for in python?

little bottle
little bottleOriginal
2019-05-21 10:26:2359111browse

In Python: :-1 means outputting characters or numbers in reverse order. For example, when line = "abcde", use the statement line[::-1], and the final running result is: 'edcba'. Please see the detailed explanation below.

What does ::-1 stand for in python?

1. Reverse

::-1 involves outputting numbers or characters in reverse order

2. Detailed explanation

1. i: j

a = [0,1,2,3,4,5,6, 7,8,9]
b = a[i:j] means copying a[i] to a[j-1] to generate a new list object
b = a[1:3] Then, The content of b is [1,2]
When i defaults, the default is 0, that is, a[:3] is equivalent to a[0:3]
When j defaults, the default is len(alist ), that is, a[1:] is equivalent to a[1:10]
When i and j are both default, a[:] is equivalent to a complete copy of a

For example:

line = "abcde"
line[:-1]
The result is: 'abcd'

2, ::- 1

b = a[i:j:s] In this format, i and j are the same as above, but s represents step, and the default is 1.
So a[ i:j:1] is equivalent to a[i:j]
When s<0, when i is defaulted, the default is -1. When j is defaulted, the default is -len(a)-1
So a[::-1] is equivalent to a[-1:-len(a)-1:-1], that is, copying from the last element to the first element. So you see something in reverse order.

For example:

line = "abcde"
line[::-1]
The result is: 'edcba'
line [:-1] is actually the remaining part after removing the last character (newline character) of this line of text.

In fact, the problem is not difficult. It will be very clear if you run it yourself.

The above is the detailed content of What does ::-1 stand for in python?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn