A list is a very frequent data structure used in Python, which is an ordered sequence. The previously learned string is an ordered sequence. However, the list is mutable.
Create a list
Li = List () #构造方法创建一个空列表
Li = List (iterable_object) #以其它可迭代对象为参数初始化一个列表
Li = [] #快捷方法创建一个空列表
li= [I for I in Range (1,11)] #推导式创建 very pythonic:)
if __name__=="__main__": Li0= [] Print(Len (li0))#0
Li1 = List ("12345") Print("Li1 is", LI1)#Li1 is [' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ']Li2= List ('Init',' by','tuple')) Print("Li2 is", Li2)#i2 is [' init ', ' by ', ' tuple ']Li3= [I forIinchRange (1,11)ifi%2==0]Print("Li3 is", Li3)#Li3 is [2, 4, 6, 8, ten]Li4= [(I,J) forIinch "ABC" forJinch "123"] Print("Li4 is", LI4)#Li4 is [(' A ', ' 1 '), (' A ', ' 2 '), (' A ', ' 3 '), (' B ', ' 1 '), (' B ', ' 2 '), (' B ', ' 3 '), (' C ', ' 1 '), (' C ', ' 2 '), (' C ', ' 3 ')]
1. Variable reference type
Mutable is a variable of the Value list object itself. Support Append, expand, insert, delete, sort, invert and so on.
2. Ordered sequence
The list is ordered and supports indexed read-write elements. Of course also support slice, see my other essay
3. Element type arbitrary
The elements in the list can be any type, including the list type, and the different types can be mixed.
4. Operators
+
*
Inch
if __name__ = " __main__ " : Li = [1]+[2,3,4,5] # [1, 2, 3, 4, 5]
print li" +=[6,7,8] print (LI) # [1, 2, 3, 4, 5, 6, 7, 8] li = [1]*3 print (LI) # [1, 1, 1]
print (1 in Li) #True
5. Memory model
The list itself is data that does not contain elements, but only contains references to elements. By referencing, the object that the element actually points to is accessed.
if __name__ " __main__ " : = "123" Li =[a,str]
Let's go through an example of a deep copy to illustrate
if __name__ " __main__ " : = [[1,2],3] = li.copy (); Li_copy[0].pop () print(li) #[[1], 3] The discovery of a copy changed the source.
Print (ID (li[0]), ID (li_copy[0])) #1037590501832 1037590501832, same address
In the first picture, we say that the list holds only the references to the elements, and this reference, in other words, is the pointer. The Copy method copies the elements in the list, that is, the reference, and also the address.
The value of li[0] itself is 1037590501832 through the copy method, Li_copy[0] also obtains the value of li[0] itself, so he also points to that nested small list in the list.
So, changing that nested small list through li_copy is reflected in the LI.
If we need to avoid this situation, we can use deep copy.
#/usr/bin/env Python3#Coding:utf-8ImportCopyif __name__=="__main__": Li= [[1,2],3] Li_copy=copy.deepcopy (LI) li_copy[0].pop ()Print(LI)#[[1, 2], 3] The change of the found copy did not make the source changes Print(ID (li[0]), ID (li_copy[0]))#1046997603464 1046997603528 Address not the same
6. Common API
Li.append ("1") #追加字符串 "1"
Li.extend ([+]) #扩张列表, append multiple, equivalent to Li +=[1,2,3]
Li.insert (3, "Hello") #插入
Li.pop () #弹出最后一个元素
Li.pop (1) #删除索引为1的 elements
Li.remove (val) #根据值删除
Li.index (val) #返回val元素的索引 failed to find the error
Li.sort () #排序, which requires comparisons between elements. Sort in situ
Sorted (LI) #返回排序拷贝份, non-in-situ sorting
List of Python basic data types