What does ::-1 stand for in python?

little bottle
Release: 2019-05-21 10:27:28
Original
59158 people have browsed it

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!

Related labels:
source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!