python基础:列表、元组操作(列表可以嵌套)
names = ["Zhangsan"," Lisi"," Wangwu"," Zhoumazi"]
import copy
names = ["1Zhangsan"," #Lisi"," xWangwu",["Wangshanfeng","Paopao"], "Zhoumazi"]
print(names[0👎2])
print(names[👎2])
print(names[:])
for i in names:
print(i)
name2 = copy.deepcopy(names)#深深地复制
name2 = names.copy()#复制不全
print(names)
print(name2)
names[2] = "王五"
names[3][0] ="王珊凤"
print(names)
print(name2)
names.append("Wusangui") #追加一个人到这个列表里
names.insert(1,"Wusangui") #想插入一个人在某某的位置
names.insert(4,"Zhangsan") #想插入一个人在某某的后面位置
names[2] = "Zhaolaotou" #想把某某换成另一个人
print(names[0],names[2])
print(names[1:4]) #切片
print(names[0:3]) #切片 取前三位
print(names[:3]) #切片 取前面三个
print(names[-1]) #切片 不知道多长的情况下取值
print(names[-2:]) #切片 取最后面的两个值
names.remove("Lisi")#删除Lisi
del names[1]#删除Lisi
names.pop() #删除最后一个值
names.pop(1)#删除Lisi
print(names)
print(names.index("Zhaolaotou") ) #通过王五的名字找到它所在的位置
print(names[names.index("Zhaolaotou")])
print(names.count("Zhangsan"))
names.clear()
names.reverse()#列表反转
names.sort() #排序
names2 = [1,2,3,4]
names.extend(names2)#合并
print(names,names2)