5.1 List
5.1.1 Definition List
names = ["Zhangsan","Lisi","Wangwu","Liuxiao"]
Note: The list is ordered and can be added and censored. Lists can be nested in lists, dictionaries, and anything else
5.2 Slices
to access the elements in the list by subscript, the index is counted starting at 0
5.2.1 fetching Elements
#! Author:lanhan
#切片
names = [ "Zhangsan" , "Lisi" , "Wangwu" , "Liuxiao" ]
Print (names)
Print (names[0],names[2]) #分别在names列表取值 (number from left to right, first digit 0, and so on)
print (Names[1:3]) #在names列表一起取值 (Gu Tou regardless of tail)
################################## ##########################################
print (names[3]) #取最后一位
print (names[-1])      &NBSP #取最后一位 (starting from left, 1, and so on)
print (names[-2:-1])       &NBSP #取倒数第二位 (starting from right, reading from left, Gu Tou ignoring tail)
print (names[-2:])          &NBSP #取倒数2位, omitted after colon
5.2.2 Append
List.append (" add element ")
5.2.2 Insertion
List.insert (subscript value ," insert element ")
5.2.3 Modification
list [subscript value ] = " modified element "
5.2.4 Delete
List.remove ("deleted elements")
Del list [subscript value ]
List.pop (subscript value ) # subscript is not written, the last one is deleted by default
#! Author:lanhan
#切片
names = ["Zhangsan","Lisi","Wangwu","Liuxiao"]
Print (names)
# #新增
Names.append ("Lanhan1")# # #将lanhan1插到最后面
Print"New", names)
Names.insert (2,"Lanhan2")#将lanhan2插入到wangwu前面
Print"New", names)
# #修改
names [1] ="Lanhan3"#将lisi替换成lanhan3
Print"Modify", names)
# #删除
Names.remove ("Wangwu")#直接列表元素删除
Print"Delete", names)
delnames [0]#下标删除
Print"Delete", names)
Names.pop (2)#输入下标删除 (does not enter the subscript, the last element is deleted by default)
Print"Delete", names)
# # Print # #
[' Zhangsan ', ' Lisi ', ' Wangwu ', ' Liuxiao ']
New [' Zhangsan ', ' Lisi ', ' Wangwu ', ' Liuxiao ', ' lanhan1 ']
New [' Zhangsan ', ' Lisi ', ' lanhan2 ', ' Wangwu ', ' Liuxiao ', ' lanhan1 ']
Modify [' Zhangsan ', ' lanhan3 ', ' lanhan2 ', ' Wangwu ', ' Liuxiao ', ' lanhan1 ']
Delete [' Zhangsan ', ' lanhan3 ', ' lanhan2 ', ' Liuxiao ', ' lanhan1 ']
Delete [' Lanhan3 ', ' lanhan2 ', ' Liuxiao ', ' lanhan1 ']
Delete [' Lanhan3 ', ' lanhan2 ', ' lanhan1 ']
5.2.5 The index of the unknown element search
List.index ("search string")
5.2.6 Statistics
List.count ("string of Statistics")
5.2.7 Reversal
List.reverse ()
5.2.8 Sort
List.sort ()
5.2.9 Merging
List.extend (list2) # merges list2 into the list of lists
5.3.0 deleting variable names
Del variable name
5.3.1 Copy List
List.copy ()
Note: You can copy only the first-level list, not the second-level list ( light copy)
Import copy module (deep cpy)
5.3 meta-group
Tuples are actually similar to the list, but also to save a group of numbers, it is not once created, it can not be modified, so it is called a read-only list (tuples are ordered)
syntax:names = ("Alex", "Jack", "Eric")
it has only 2 methods, one is count, the other is index
Python path -05-list or tuple