python sequence slicing operation

Function: intercept any part of the list.

Slicing is applicable to list, tuple, string, range object and other types..

Format::] slicing is done with three numbers separated by two colons.

The first number indicates the slice start position (0 by default).

The second number indicates the cut-off (but not included) position of the slice (the default is the list length).

The third number indicates the step size of the slice (1 by default). If the step size is omitted, the last colon can be omitted.

1. Slicing does not throw an exception because the subscript is out of bounds, but simply truncates or returns an empty list at the end of the list.   

>>> alist=[1,2,3,4,5,6,7,8,9,0,]
>>> alist[::]    #Returns a new list of containing elements
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
>>> alist[::-1]   #All elements in reverse order
[0, 9, 8, 7, 6, 5, 4, 3, 2, 1]
>>> alist[::2]    #Even position, take one every other
[1, 3, 5, 7, 9]
>>> alist[1::2]    #Odd position, one from each other
[2, 4, 6, 8, 0]
>>> alist[3::]    #All elements from subscript 3
[4, 5, 6, 7, 8, 9, 0]
>>> alist[3:6]    #All elements with subscripts between [3, 6]
[4, 5, 6]
>>> alist[0:100:1]  #Top 100 elements, auto truncated
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
>>> a[100:]      #All elements after subscript 100, automatically truncated
[]
>>> x[100]      #Direct use of subscript access will cause cross boundary
Traceback (most recent call last):
  File "<pyshell#131>", line 1, in <module>
    x[100]
IndexError: list index out of range

2. You can use slices to modify the contents of the list in place

>>> alist
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
>>> alist[len(alist):]=[9]    #Append element at end
>>> alist
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 9]>>> alist[:3]=[3,2,1]      #Replace the first three elements
>>> alist
[3, 2, 1, 4, 5, 6, 7, 8, 9, 0, 9]
>>> alist[:3]=[]          #Delete the first three elements
>>> alist
[4, 5, 6, 7, 8, 9, 0, 9]
>>> alist=list(range(10))
>>> alist
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> alist[::2]=[0]*5        #Replace element in even position
>>> alist
[0, 1, 0, 3, 0, 5, 0, 7, 0, 9]  
>>> alilst[::2]=[0]*3        #Slice is not continuous, the number of two elements must be the same
Traceback (most recent call last):
  File "<pyshell#153>", line 1, in <module>
    alilst[::2]=[0]*3
NameError: name 'alilst' is not defined

3. Use del and slice to delete list elements

 

>>> alist=[3,4,5,6,7,]
>>> del alist[:3]      #Delete the first three elements
>>> alist
[6, 7]
>>> alist=[3,4,5,6,7]  
>>> del alist[::2]    #Delete element in even position
>>> alist
[4, 6]

4. The slice returns a shallow copy of the list element

 

>>> alist=[3,5,7]
>>> blist=alist
>>> blist
[3, 5, 7]
>>> blist[1]= 8
>>> alist
[3, 8, 7]
>>> alist==blist
True
>>> alist is blist
True
>>> id(alist)
2848177707136
>>> id(blist)
2848177707136

5. Shallow copy is to copy the references of all elements in the original list into the trust list

The original list contains only basic types such as integers, real numbers, complex numbers, or immutable types such as tuples and strings. Generally, there is no problem.

The original list contains variable data types such as list. Because shallow copy only copies the reference of sublist to the new list, modifying any one will affect the other.

>>> alist=[1,2,3]
>>>Blist = alist [::] (slice, light copy)
>>>Alist = = blist - the elements of the two lists are exactly the same
True
>>>Alist is blist but not the same object
False
>>>ID (alist) = = ID (blist) × different memory addresses
False
>>>Blist [1] = 8. Modifying one does not affect the other
>>> blist
[1, 8, 3]
>>> alist
[1, 2, 3]

Tags: Python

Posted on Sat, 21 Mar 2020 10:28:14 -0400 by angelac