List as an ordered data type. Most of the operations are similar to strings. such as getting the elements of a list, slicing operations, and so on.
1: Index (subscript)
2: Increase, delete, change, check
3: Common functions
4: List parsing
One: Index: Pass string like, from left to right. The index starts at 0 and ends with Len (list)-1.
from right to left,-len (list) to-1.
A=[1,2,3,4]print (A[0]) print (a[-1]) 14
Two: 1, add an element to the list. The Append () function defaults from the last Add. Insert (Index,object) allows you to specify the location to add elements.
A=[1,2,3,4]b=[1,2,3,4]a.append (5) print (a) B.insert (b[0],0) print (b) [1, 2, 3, 4, 5][1, 0, 2, 3, 4]
2: Delete an element. The last element is deleted by default with Pop (index). or use the Remov (element) function. Note the difference.
A=[1,2,3,4]b=[1,2,3,4]a.pop (2) print (a) b.remove (1) print (b) [1, 2, 4][2, 3, 4]################## #a =[ 1,2,3,4,6,6,7,7,23,23,23,5] #del可以删除任何内存中的数据, variables, and so on. Del A[2:4]print (a) [1, 2, 6, 6, 7, 7, 23, 23, 23, 5]
Exercise: How do I delete a repeating element in a list?
A=[1,2,3,4,6,6,7,7,23,23,23,5]num=a.count (2, 3, 4, 6, 6, 7, 7, 5) for I in Range (Num): A.remove (All) print (a) [1]
Gets the number of occurrences of 23 elements through the count () function. The number of times as a for loop. In turn, 23 elements are removed from the repeating occurrence.
Three: List parsing:
List resolution:
Syntax: [Expr for Iter_var in iterable] or [expr for Iter_var in iterable if COND_EXPR]
The benefit is to avoid writing lengthy code to get the list.
list parsing is introduced in earlier versions of Python (as if it were version 2.0), and the generator expression is the new content introduced in 2.4, which resembles the syntax for list parsing. but in the case of big data processing, the advantage of the generator expression is that it is better and more efficient to use, and it does not create a list, but returns a generator.
1) list_a=[1,2,3,4,4]list_b=[3,4]list_contiain=[i for i in list_a if I in List_b]print (List_contiain) [3, 4, 4]2] a=[x for X In range (3)]print (a) [0, 1, 2]
IV: List of merges, with function extend ()
A=[x for x in range (3)]b=[i for I in range (2)]a.extend (b) print (a) [0, 1, 2, 0, 1]
Five: A deep copy of the list, using the function copy ()
A=[1,2,3,4,[1,2]]b=list (a) c=a[:]b[4][0]=55print (a,b,c) [1, 2, 3, 4, [55, 2]] [1, 2, 3, 4, [55, 2]] [1, 2, 3, 4, [55, 2]]### ########################### #a =[1,2,3,4,[1,2]]c=a.copy () c[4][0]= ' t ' Print (A,C) [1, 2, 3, 4, [' t ', 2]] [1, 2, 3, 4, [' t ', 2]] ###################################### #import copya=[1,2,3,[1,2]]c=copy.copy (a) c[3][1]= ' Fuck ' print (a,c) [1, 2, 3, [ 1, ' fuck '] [1, 2, 3, [1, ' Fuck ']]########## #深拷贝 ##################### #import copya=[1,2,3,[1,2]]c=copy.deepcopy (a) c[3 ][1]= ' Fuck ' print (a,c) [1, 2, 3, [1, 2]] [1, 2, 3, [1, ' fuck ']]
Idea one: Using slicing operations and factory methods The list method copy is called a shallow copy, just copy the outermost object itself, the internal elements are just a copy of a reference.
Idea two: Copying using the Deepcopy method in copy is called a deep copy, and the outer and inner elements copy the object itself, not the reference.
But for numbers, strings, and other atomic type objects, there is no copy of the argument, even with a deep copy, the same is true if you look at the ID, and if you re-assign it, just create a new object and replace the old one.
Python base List