首页 > 代码库 > python-增删改查

python-增删改查

###增删改查
names = ["zhangding","wangxu","wudong","cheng"]

#增
names.append("zhangzhengxin")  #增加
print(names)
names.insert(2,"liu")  #插入
names.insert(1,"shun")
print(names)

#删
#names.remove("liu")
#del names[3]
print(names.pop())
print(names)

#改
names[2] = "王旭"
print(names[0::2])


#查
print(names[-2])
print(names[0:])
print(names[-3:]) #get the last 3 element
print(names[:3])  #get the first 3 element
print(names.index("王旭"))   #取元素的下标

#两个列表的合并
n2=["jack","rain","1","6","50","B","*"]
names.extend(n2)
print(names,n2)

names.reverse()  #旋转
print(names)

names.sort()   #排序
print(names)

n3 = names.copy() #copy
n4 = names
print((n3),id(names),id(n3))
print(n4,id(names),id(n4))

names.pop()
print("------------")
print(n3,id(names),id(n3))
print(n4,id(names),id(n4))

 

python-增删改查