Python list operation--Increase
Append: Append a piece of data to the end of the list
name = ["Zhangsan", "Xiongda", "Lisi"]name.append ("Wangwu") Print name output: [' Zhangsan ', ' Xiongda ', ' Lisi ', ' Wangwu ']
Insert : Inserts a single piece of data at the specified location
name = ["Zhangsan", "Xiongda", "Lisi"]name.insert (1, "Wangwu") #在下标为1的位置插入一条数据 "Wangwu" Print name output: [' Zhangsan ' , ' Wangwu ', ' Xiongda ', ' Lisi ']
Python list operation--delete
name = ["Zhangsan", "Xiongda", "Lisi"]name.remove ("Lisi") #删除指定的数据print name output: [' Zhangsan ', ' Xiongda ']
name = ["Zhangsan", "Xiongda", "Lisi"]del name[0] #删掉下标为0的一条数据print name output: [' Xiongda ', ' Lisi ']
name = ["Zhangsan", "Xiongda", "Lisi"]name.pop () #删除最后一条数据print name output: [' Zhangsan ', ' Xiongda ']
if a parameter is brought into the pop (), the effect is the same as the Del
Name.pop (1) = = del Name[1]
Python list operation--Change
name = ["Zhangsan", "Xiongda", "Lisi"]name[1] = "Wangwu" Print name output: [' Zhangsan ', ' Wangwu ', ' Lisi ']
Python list operations-check
name = ["Zhangsan", "Xiongda", "Lisi", "Wangwu"]print (name[1]) #直接取出下标为1的数据输出结果: Xiongda
name = ["Zhangsan", "Xiongda", "Lisi", "Wangwu"]print (Name[0:2]) #取出从下标0到下标1的数据, excluding 2 (Gu Tou regardless of tail) output: [' Zhangsan ', ' Xiongda ']
When the current is marked as a negative number, the right start takes
name = ["Zhangsan", "Xiongda", "Lisi", "Wangwu"]print (name[-1]) #直接取出下标为1的数据输出结果: Wangwu
name = ["Zhangsan", "Xiongda", "Lisi", "Wangwu"]print (name[-3:-1]) #从倒数第三个开始取, take the second-to-last, because it does not include the 1 output: [' Xiongda ', ' Lisi ']
name = ["Zhangsan", "Xiongda", "Lisi", "Wangwu"]print (name[-3:]) #从倒数第三个开始取, take the first output of the countdown: [' Xiongda ', ' Lisi ', ' Wangwu '] Similarly from yesterday to take the time name[0:3] = = Name[:3]
List Other actions:
name = ["Zhangsan", "Xiongda", "Lisi", "Wangwu", "Lisi"]name.count ("Lisi") #统计Lisi出现的次数name. Clear () # Empties the data name.reverse () #反转列表name. Sort () forward sort name2 = ["1", "2"]name.extend (name2) #两个数组合并, name2 on the back