Basic learning of python - slicing of data containers (sequences)

Slices of data containers (sequences)

Sequence: A type of data container whose content is continuous and ordered and can be indexed using subscripts .
Lists, tuples, and strings can all be regarded as sequences.

Element 1, element 2, element n
  0, 1, n - forward subscript
 -n, -2, -1 - reverse subscript

Typical characteristics of sequences are:Ordered and indexable with subscripts, strings, tuples, and lists all meet this requirement.

Common operations on sequences - slicing

Sequences support slicing, that is, lists, tuples, and strings all support slicing operations.
Slicing: Remove a subsequence from a sequence.

Syntax: sequence [start index:end index:step]

Indicates that from the sequence, starting from the specified position , elements are taken out in sequence and ending at the specified position to obtain a new sequence:
1. The starting subscript indicates where to start. It can be left blank. If left blank, it is regarded as starting from the beginning
. 2. End subscript The mark (not included) indicates where to end. It can be left blank. If it is left blank, it is regarded as intercepting to the end.
3. Step length indicates that the intervals of elements are taken in sequence:
① Step length 1 indicates that elements are taken one by one.
② Step length 2 indicates that each element is taken. Skipping 1 element at a time
is represented by ③ step size N, skipping N-1 elements each time is represented by
④ step size being a negative number, and taking it in reverse direction (note that the starting subscript and ending subscript must also be marked in reverse)

Note that this operation will not affect the sequence itself, but will result in a new sequence (list, tuple, string).

Example:

# 对list进行切片,从1开始,4结束,步长1
my_list = [0, 1, 2, 3, 4, 5, 6]
result1 = my_list[1:4]  # 默认步长是1,所以可以省略不写
print(f"结果1:{
      
      result1}")

# 对tuple进行切片,从头开始,到最后结束,步长1
my_tuple = (0, 1, 2, 3, 4, 5, 6)
result2 = my_tuple[:]  # 起始和结束不写表示从头到尾,步长为1可以省略
print(f"结果1:{
      
      result2}")

# 对str进行切片,从头开始,到最后结束,步长2
my_str = "0,1,2,3,4,5,6,7"
result3 = my_str[::2]
print(f"结果1:{
      
      result3}")

# 对str进行切片,从头开始,到最后结束,步长-1
my_str = "0,1,2,3,4,5,6,7"
result4 = my_str[::-1]  # 等同于序列反转了
print(f"结果1:{
      
      result4}")

# 对列表进行切片,从3开始,1结束,步长-1
my_list = [0, 1, 2, 3, 4, 5, 6]
result5 = my_str[3:1:-1]
print(f"结果1:{
      
      result5}")

# 对元组进行切片,从头开始,到最后结束,步长-2
my_tuple = (0, 1, 2, 3, 4, 5, 6)
result6 = my_tuple[::2]
print(f"结果1:{
      
      result6}")

operation result:

Result 1: [1, 2, 3]
Result 1: (0, 1, 2, 3, 4, 5, 6)
Result 1: 01234567
Result 1: 7,6,5,4,3,2,1,0
Result 1:,1
Result 1: (0, 2, 4, 6)

Slice summary of sequence

1. Sequence: A type of data container with continuous and ordered content that supports subscript indexing.
2. Can be regarded as a sequence: list, tuple, string.
3. How to slice sequences:sequence[start:end:step].
4. The beginning can be omitted, and starting from the beginning can be omitted.
5. The end can be omitted, and it ends at the end.
6. The step size can be omitted, and the omitted step size is 1 (it can be a negative number, indicating execution in reverse order).

Sequence slicing practice

# 三种方式取出我是二狗
my_str = "狗二是我1,nohtyp学"
# 倒序字符串,切片取出
result1 = my_str[::-1][9:13]
print(f"1的结果:{
      
      result1}")

# 切片取出,然后倒序
result2 = my_str[0:4][::-1]
print(f"2的结果:{
      
      result2}")

# split分隔“,” replace替换“来”为空,倒序字符串
result3 = my_str.split(",")[0].replace("1", "")[::-1]
print(f"3的结果:{
      
      result3}")

operation result:

The result of 1: I am a second dog.
The result of 2: I am a second dog.
The result of 3: I am a second dog.

Guess you like

Origin blog.csdn.net/weixin_44996886/article/details/132678423