The most basic data structure in Python. Each element in the sequence is assigned a number-its position, or index, the first index is 0, the second index is 1, and so on
List
1. Define the list and remove the values from the list
1 names = [] #定义空列表 2 names = [' A ', ' B ', ' C '] #定义一个非空列表 3 4 # Remove the value from the list 5 6 >>> names = [' A ', ' B ', ' C '] 7 >& Gt;> Names[0] 8 ' a ' 9 >>> names[1]10 ' B ' >>> names[2]12 ' C ' >>> names[-1] #倒着取最后一个值14 ' C '
2. Slicing
1 >>> names = [' A ', ' B ', ' C ', ' d '] # The subscript value of the list is from 0 to 2 >>> Names[1:3] #取1到3之间的元素, including 1, not including 3 3 [' B ', ' C '] 4 ;>> Names[1:-1] #取1到-1 elements, including 1, excluding -1 5 [' B ', ' C '] 6 >>> names[0:3] 7 [' A ', ' B ', ' C '] 8 >>> names[ : 3] #从头开始取, 0 can be omitted, the effect is equivalent to Names[0:3] 9 [' A ', ' B ', ' C ']10 >>> names[3:] #想取到最后一个值, must not write-1, can only write one by one [' d ']12 >& Gt;> Names[0::2] #后面的2表示: Every other element takes a [' a ', ' C ']14 >>> Names[::2] #从头开始0可以省略, the effect is the same as the previous sentence [' A ', ' C ']
Section Summary:
The ① sequence is always sliced from left to right and cannot be
① the list slice, the element of the starting bit is included, the element of the ending bit is not included (also called Gu Tou regardless of the tail), and the last position represents the step (names[start bit: End bit: Step))
② If a value is taken from the 0 position, 0 can be omitted
③ you want to take the last value, the ending bit cannot be-1, because the element of the end bit is not included, so you can only leave it blank
3. List Functions & Methods
Function:
Method:
Demonstrate:
1 # Append Add new object at the end of the list 2 >>> names = [' A ', ' B ', ' C ', ' d '] 3 >>> names.append (' E ') 4 >>> names 5 [' A ', ' B ', ' C ', ' d ', ' e '] #e是新加的元素 6 7 # Insert (subscript value, insert content) 8 >>> names = [' A ', ' B ', ' C ', ' d '] 9 >>> names.in SERT (0, ' 1 ') #0表示需要插入的下标值, ' 1 ' represents the inserted content of >>> names11 [' 1 ', ' A ', ' B ', ' C ', ' d '] #在下标值为0的地方插入 ' 1 ' #修改列表中的元素, directly NA mes[Subscript value] = new value >>> names = [' A ', ' B ', ' C ', ' d ']15 >>> names[1] = ' 1 ' + >>> names17 [' A ', ' 1 ', ' C ' , the list of ' d ']18 #扩展 (Extend) Names2 is merged into the names1, but the Names2 list still exists, and if you want to delete names2 this variable, just del names2 can >>> names1 = [' A ', ' B ', ' C ', ' d ']22 >>> names2 = [1,2,3,4]23 >>> names1.extend (names2) >>> names125 [' a ', ' B ', ' C ', ' d ', 1, 2, 3, 4]26 #统计 (count (Element)) >>> names = [' A ', ' B ', ' C ', ' d ', ' a ']29 >>> names.count (' a ' The number of #统计 ' a ' elements is 231 #翻转 (reverse ()) >>> names = [' A ', ' B ', ' C ', ' d ']34 >>> names.reverse () >>& Gt names36 [' d ', ' C ', ' B ', ' A'] #将整个列表翻转过来37 #排序 (sort ()) >>> names = [4,2,3,1]40 >>> names.sort () names42 [1, 2, 3, 4]43 #获取下标值 (index (element)) >>> names = [' A ', ' B ', ' C ', ' d ']46 >>> names.index (' a ') 048 #清空列表 (Clea R ()) >>> names = [' A ', ' B ', ' C ', ' d ']51 >>> names.clear () * names53 []
Delete (Del, remove (Element), pop ())
#根据下标值删除元素 >>> names = [' A ', ' B ', ' C ', ' d ']>>> del names[0] >>> names[' B ', ' C ', ' d '] #根据元素删除 >& gt;> names = [' A ', ' B ', ' C ', ' d ']>>> names.remove (' a ') >>> names[' B ', ' C ', ' d '] #删除最后一个 >>> names = [' A ', ' B ', ' C ', ' d ']>>> names.pop () ' d ' >>> names[' A ', ' B ', ' C ')
Extended:
#如果pop () has subscript value, it is to delete the specific element, the effect and Del effect is the same >>> names = [' A ', ' B ', ' C ', ' d ']>>> names.pop (1) # The input subscript is worth the case and Del's effect is the same as ' B ' >>> names[' a ', ' C ', ' d '] #del关键字不仅可以删除列表中的元素, you can also delete the variable names = [' A ', ' B ', ' C ', ' d ']# Delete names this variable del names
Copy
1 >>> names = [' A ', ' B ', ' C ', ' d ']2 >>> names2 = names.copy () 3 >>> names24 [' A ', ' B ', ' C ', ' d ']
Note: Copy Here is a shallow copy, can only copy the first layer. Deep copy Details: Click here