python列表技巧
1、访问列表元素
test1 = ["chengqian","and","what"] print(test1[0])#第一个元素 print(test1[-1])#最后一个元素
2、修改、添加和删除元素
test2 = ["sometime","pressure","love"] test2[0] ="anytime" #替换第一个值 test3 = test2.append("last")#末尾增加一个值 test4 = test2.insert(0,"first")#第一处增加一个值 test5 = del test2[0]#删除第一个值
3、删除一个元素后立马进行使用
another_test = ["1","2","3"] test2 = another_test.pop()#test2等于删除下来的那个元素,并且是末尾删除
print(test2)#结果是"3"
test3 = another_test.pop(0)
4、根据值删除元素
test = ["one","two"] test2 = test.remove("two")#删除指定元素
5、组织列表
cars = ["a","b","c"] cars.sort()#按字母永久改变排序 cars.sort(reverse=True)#按字母反过来永久排序 #临时改变排序:sorted函数 #永久反过来改变排序:reverse函数
浙公网安备 33010602011771号