Listlist creation, using []
A_list = [' A ', ' B ', ' C ']
Print A_list
Print A_list[0] #a
If you go to the last element of the list, in addition to calculating the index location, you can also use-1
Print A_list[-1] #c
And so on, you can get the bottom 2nd, the bottom 3rd one.
Print A_list[-2] #b
Print A_list[-3] #a
The addition of the list element
A_list.append (' d ') #增加到末尾
A_list.insert (1, ' e ') #增加到指定位置
A_list.extend ([' F ', ' g ', ' h ') #将两个个对象的元素加到该list的末尾
A_list*3 #使用乘法扩展list
Delete of list element
Del A_list[1] #删除指定元素
Del a_list #删除整个list
A_list.pop () #删除最后一个元素. To delete the specified index position element, pop (i)
A_list.remove (' a ') #删除首次出现的指定元素
Substitution of list elements
A_list[1] = ' Z ' #直接赋值给对应索引位置
The data type of the list element can be different
A_list = [' Apple ', ' 123 ', True] #True和False首字母要大写
The list element can also be another list
A_list = [' Apple ', ' orange ', [' Python ', 123], 321]
Print Len (a_list) #4
List element Access and Count
accessing directly using indexed locations
A_LIST[1]
Gets the index position of the first occurrence of the specified element
A_list.index (' a ')
Gets the number of occurrences of the specified element in the list
A_list.count (' a ')
For Yuanju, string, and range
Range (Ten). Count (3) #1
(2,2,2,4). Count (2) #3
(' A ', ' B ', ' C ', ' a '). Count (' a ') #2
Determines whether the specified element is in the list
' A ' in a_list
Slicing operations
x = Range (10,100)
x[::] #全取
X[::-1] #倒着全取
X[::2] #从头每两个取一个
X[::-2] #倒着每两个取一个
X[1::2] #从位置1每两个取一个
X[3:6] #取指定范围
X[:10:2] #前10个中每两个取一个
List sort
A_list = [1,2,3,4,5,6]
Import Random
Random.shuffle (a_list) #打乱顺序
A_list.sort () #默认升序
A_list.sort (reverse = True) #降序排序
You can also use sorted () to sort, unlike the sort () directly for list modifications, sorted () returns a new list and does not manipulate the original list
b = Sorted (a_list)
Reverse the list element
A_list.reverse ()
You can also sort by reversed (), unlike reverse (), which changes directly to the list, reversed () returns a new list and does not manipulate the original list
Newa = Reversed (a)