列表
列表的增删改查
增
names = ["马云","比尔·盖茨","扎克伯格","杰夫·贝索斯"] names.append("马化腾") print(names)
======================
['马云', '比尔·盖茨', '扎克伯格', '杰夫·贝索斯', '马化腾']
names = ["马云","比尔·盖茨","扎克伯格","杰夫·贝索斯"] names.insert(1,"张志强") print(names)
===================
['马云', '张志强', '比尔·盖茨', '扎克伯格', '杰夫·贝索斯']
删
names = ["马云","比尔·盖茨","扎克伯格","杰夫·贝索斯"] names.remove("杰夫·贝索斯") print(names)
=============
['马云', '比尔·盖茨', '扎克伯格']
names = ["马云","比尔·盖茨","扎克伯格","杰夫·贝索斯"] del names[-1] print(names)
==============
['马云', '比尔·盖茨', '扎克伯格']
names = ["马云","比尔·盖茨","扎克伯格","杰夫·贝索斯"] names.pop(0) #不输入序号会默认删除最后一个 print(names)
================
['比尔·盖茨', '扎克伯格', '杰夫·贝索斯']
改
names = ["马云","比尔·盖茨","扎克伯格","杰夫·贝索斯"] names[1] = "张志强" print(names)
===============
['马云', '张志强', '扎克伯格', '杰夫·贝索斯']
查
names = ["马云","比尔·盖茨","扎克伯格","杰夫·贝索斯"] print(names) print(names.index("马云"))
=====================
['马云', '比尔·盖茨', '扎克伯格', '杰夫·贝索斯']
0
copy
names = ["马云","比尔·盖茨","扎克伯格","杰夫·贝索斯"] print(names) names2 = names.copy() print(names2) names[0] = "马化腾" print(names) ============= ['马云', '比尔·盖茨', '扎克伯格', '杰夫·贝索斯'] ['马云', '比尔·盖茨', '扎克伯格', '杰夫·贝索斯'] ['马化腾', '比尔·盖茨', '扎克伯格', '杰夫·贝索斯']