List
a = [' abc ', ' BCD ', ' CDE ', ' def ', ' EFG ']
print (a)
List of operations: Adding and deleting changes
1) Check: Slice
print (A[1:3]) #从 ' BCD ' taken to ' CDE ', the list value Gu Tou regardless of the tail.
print (a[1:]) #从 ' BCD ' to ' EFG ', from head to tail
print (a[1:-1]) #从 ' BCD ' to ' def ', the list value Gu Tou regardless of the tail.
print (A[1:-1:2]) #2代表步长, the code function is to take values from left to right, value ' BCD ', ' def '
print (A[1::2]) #2代表步长, the code function is to take values from left to right, value ' BCD ', ' def '
print (A[3::-2]) #-2 represents the step size, the code function is to take the value from right to left, value ' def ', ' BCD '
print (a[-2::-1]) #从右边第二个开始取值, take value from ' def ' to ' abc '
2) Increase: increased
Append Add a value to the list at the end
Insert Inserts a value anywhere in the middle of the list
a.append (' fgh ')
print (a)
A.insert (1, ' FGH ')
print (a)
3) Change: Modify
to modify a list slice before assigning a value
a[1] = ' Ghi '
print (a)
A[1:3] = [' A ', ' B ']
print (a)
4) Deleting: deleting
Remove deletes the specified content as a whole and cannot delete slices
Pop Deletes the specified position value and returns the deleted value
del You can delete the contents of the list or delete the entire list or object
a.remove (' abc ')
print (a)
B = A.pop (1)
print (a)
print (b)
del A[0]
print (a)
del a
print (a)
5) Count effect: Calculates the number of occurrences of an element in a list
t = [' A ', ' B ', ' B ', ' A ', ' C ', ' d '].count (' a ')
print (t)
6) Extend effect: Add list to another list
a = [a]
B = [4,5,6]
A.extend (b)
print (a)
print (b)
7) The position of the index output element in the list
a = [' abc ', ' BCD ', ' CDE ', ' def ', ' EFG ']
Print (A.index (' CDE '))
Python Learning: List