#edit by weiwei xu
names=["xww","wangyu","yujunxiang","shaolun"]
names.append("zhuijia")#追加到表最后
names.insert(1,"charu")#1,代表插入到1这个位置
names[0]="xiugai"#将xww改为xiugai,直接在位置重新赋值
print(names[2])#从0开始第2个
print(names[1:3])#顾头不顾尾,包含1,不包含3,取1,2
print(names[-1])#从后往前取
print(names[-2:-1])
print(names[-2:])#从-2开始取到最后
print(names)
#delete
names.remove("wangyu")#删除
#del names[2]#删除2位置数据
#names.pop(2)#删除2位置数据,默认删除最后一个数据
print(names)
print(names.index("charu"))#查找charu所在的位置
print(names[names.index("charu")])#找到并打印
names.append("zhuijia")
print(names.count("zhuijia"))#查找有几个zhuijia
names.reverse()#前后颠倒
#names.clear()#清除
names.sort()#排序,按照特殊符号、数字、大写、小写
print(names)
names2=[1,2,3,4,5,6,7]
names.extend(names2)#两表合并
del names2#删除 表2
print(names)
浅copy
#edit by weiwei xu
#utf-8-
names=["xww","yujunxiang","wangzhuzhu",["haha","shamao","二楞"],"wuyeqing","luojinxin"]
name2=names.copy()#浅copy,从结果可以看到,name2里的wangzhuzhu没变,但二哈变了(如果开始引用了copy,import copy,此处也可以写成 name2=copy.copy(names))
print(names)
print(name2)
names[2]="dage"
names[3][0]="二哈"
print(names)
print(name2)
print(names[0:-1:2])#正常情况下0和-1可以省略,可以写成print(::2),从0开始到最后,每隔2个打印
for i in names: #打印列表
print(i)
列表,元组
‘‘‘
name = ["alex","tenglan","eric","rai","tom","amy"]
print(name)
print(name[1:4])#取1-4之间的数据,不包括第4个
print(name[1:-1])
print(name[:3])
print(name[3:])#后面不写-1,就可以取到最后的值
print(name[0:-1:2])
print(name[::2])#同上面效果一样,0和-1可以不写
name.append("new_student")#追加到最后
print(name)
name.insert(2,‘强行插入‘) #在2的位置插入数据
print(name)
name[2]=‘换新人了‘#修改
print(name)
del name[2]#删除某列
print(name)
name.remove(‘tenglan‘)#删除特定值的列
print(name)
name.pop()#删除列表最后一列
print(name)
names = [‘xww‘, ‘tenglan‘, ‘Eric‘,‘tom‘,‘amy‘,"tom"]
b = [1,2,3]
names.extend(b)#扩展
print(names)
name_copy =names.copy()#copy
print(name_copy)
print(names.count("tom"))#统计tom的数量
names.remove(1)
names.remove(2)
names.remove(3)
‘‘‘
names = [‘xww‘, ‘tenglan‘, ‘Eric‘,‘tom‘,‘amy‘]
print(names.index("Eric"))#获取列的下标
print(names)
names.sort()#排序完毕后打印原来的值
print(names)
names.reverse()#翻转排序
print(names)
namexx = ("xww","wangsong","kill","piapia")#元组,是只读文件,无法修改,
ddd=namexx.count("xww")#汇总xww数量
ttt=namexx.index("kill")#查看kill的下标
print(ddd)
print(ttt)
原文地址:https://www.cnblogs.com/xuxiaole/p/8615947.html