Usage of Python [ ], [:] and [::]

To operate on slices:

[ ]: Mainly takes an element in the sequence, for example: str_list[2] takes the third element in the sequence, where 2 refers to the index in the sequence.

[:]: Mainly takes a section of elements in the sequence , for example str_list[1:4] means accessing the second to fourth elements in the str_list sequence (str_list[1], str_list[2], str_list[3]), Does not contain str_list[4] elements.

If no index value is provided, it defaults to 0. str_list[:4] means accessing the first to fourth elements of this sequence (excluding str_list[4] elements), str_list[4:] means accessing the fifth to last elements. Note: m\n can be negative, and returns empty when m>n.

[::]: Mainly takes elements in the sequence in reverse order and intervals, for example: (1) Flip the elements of the sequence, for example, str_list[::-1] means flipping the elements in the sequence, str_list is the element in the sequence. (2): Interval fetching elements: str_list[1::2], starting from index 1, jumping 2 fetch elements each time. Note: When n is negative, the number is taken in reverse order. When n is positive and m is empty, the default is m=0. When n is negative and m is empty, the default is -1.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import absolute_import
from __future__ import print_function
from __future__ import division

import os
import sys
import cv2
import numpy as np
import matplotlib.pyplot as plt

print(16 * "++--")

str_list = ['A', 'B', 'C', 'D', 'E', 'F', 'G']
print("str_list:", str_list)
print("str_list[3]:", str_list[3])
print("str_list[1:4]:", str_list[1:4])
print("str_list[:4]:", str_list[:4])
print("str_list[4:]:", str_list[4:])
print("str_list[::-1]:", str_list[::-1])
print("str_list[::2]:", str_list[::2])
---------------------------------------------------

++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--
str_list: ['A', 'B', 'C', 'D', 'E', 'F', 'G']
str_list[3]: D
str_list[1:4]: ['B', 'C', 'D']
str_list[:4]: ['A', 'B', 'C', 'D']
str_list[4:]: ['E', 'F', 'G']
str_list[::-1]: ['G', 'F', 'E', 'D', 'C', 'B', 'A']
str_list[::2]: ['A', 'C', 'E', 'G']

Process finished with exit code 0

The prototype of the slicing operator in Python is [start:stop:step], [start index: end index: step value].
Start index: starts from 0. In the left-to-right direction of the sequence, the first value has an index of 0 and the last value has an index of -1.
End index: The slicing operator will fetch until the index, excluding the value of the index .
Step value: The default is to cut one after another. If it is 2, it means that every other operation is performed. When the step value is positive, it means taking from left to right. If negative, it means taking from right to left. The step value cannot be 0.
[:] is to copy a list as it is.


import os
import sys
import cv2
import numpy as np

print(16 * "++--")
str_data = 'abcdefg'
print("str_data[3]:", str_data[3])
print("str_data[3:5]:", str_data[3:5])
print("str_data[:5]:", str_data[:5])
print("str_data[3:]:", str_data[3:])
print("str_data[::2]:", str_data[::2])
print("str_data[::-1]:", str_data[::-1])
print("str_data[-1]:", str_data[-1])
print("str_data[-2]:", str_data[-2])
print("str_data[-5:-2]:", str_data[-5:-2])
print("str_data[-2:-5]:", str_data[-2:-5])
--------------------------------------------------

++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--
str_data[3]: d
str_data[3:5]: de
str_data[:5]: abcde
str_data[3:]: defg
str_data[::2]: aceg
str_data[::-1]: gfedcba
str_data[-1]: g
str_data[-2]: f
str_data[-5:-2]: cde
str_data[-2:-5]: 

Process finished with exit code 0

Guess you like

Origin blog.csdn.net/m0_62278731/article/details/130315674
Recommended