python & numpy slice (-)

In the syntax with extended slices, different index slice operations are allowed. There are three types of slice operations:

Of course, slices can slice strings, lists, and tuples. Let's take the most common list slices as an example.

1: Step slice, the syntax is simple, list1[start position subscript: end position subscript: step length] (list1[star:end:step]) of course, the cut out does not contain the data corresponding to the end position subscript, and list1 The default value of star in [star:end:step] is 0, the default value of end is len(list1), and the default value of step is 1. The parameters can be omitted:

list1=[1,2,3,4,5,6,7]
print(list1[::])
print(list1[0:len(list1):1])

The result of the operation is: [1, 2, 3, 4, 5, 6, 7"

                  [1, 2, 3, 4, 5, 6, 7]

Anyway, stepping and slicing is very simple, and it is not cumbersome here for practical use.

2: Multidimensional slice:

Multidimensional slices need to import the numpy module, the syntax is list1[start1:end1,star2:end2], for example

list1 = [[1,2,3,4,5],
         [6,7,8,9,10],
         [11,12,13,14,15],
         [16,17,18,19,20],
         [21,22,23,24,25]]
list2 = list1[1:3,1:4]
print(list2)
The output is:
[[7,8,9],[12,13,14]]
 
 

3: Omit the slice: the syntax is list1[...,start1:end1], the last wc==







Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325660397&siteId=291194637