1.列表元组
names=[“张三”,“李四”,“王五”,“赵六”,“刘七”,“陈八”] name[0] '张三' name[2] '王五' name[-1] '陈八' name[-2] '刘七'
切片:取多个元素
names=[“张三”,“李四”,“王五”,“赵六”,“刘七”,“陈八”] name[1:4] #取下标1到4之间的数,包括1,不包括4 '''[李四”,“王五”,“赵六”]''' name[1:-1] #取下标1到-1的数,不包括-1 '''[“李四”,“王五”,“赵六”,“刘七”]''' names[0:3] #取0到3不包括3 '''[“张三”,“李四”,“王五”]''' names[:3] #取0到3不包括3 从头取0可以忽略 '''[“张三”,“李四”,“王五”]''' names[3:] #如果想取最后一个就不能写-1 '''[“赵六”,“刘七”,“陈八”]''' names[-3:-1] '''[“赵六”,“刘七"]''' names[0::2] #后面的2代表没隔一个元素取一个 '''[“张三”,“王五”,“刘七”]''' names[::2] #和上面效果一样 '''[“张三”,“王五”,“刘七”]'''
追加
names=["张三",“李四”,“王五”,“赵六”] names.append("六六") names '''["张三",“李四”,“王五”,“赵六”,"六六"]'''
插入
names=["张三",“李四”,“王五”,“赵六”] names.insert(2,"插入王五前面") names '''["张三",“李四”,"插入王五前面",“王五”,“赵六”]''' names.insert(4,"从王五后面插入试试新姿势") names '''["张三",“李四”,"插入王五前面",“王五”,“赵六”,"从王五后面插入试试新姿势"]'''
修改
names = ["张三",“李四”,"插入王五前面",“王五”,“赵六”,"从王五后面插入试试新姿势"] names[2]="该换人了" names ''' ["张三",“李四”,"该换人了",“王五”,“赵六”,"从王五后面插入试试新姿势"]'''
删除
names= ["张三",“李四”,"插入王五前面",“王五”,“赵六”,"从王五后面插入试试新姿势"] del names[2] names ''' ["张三",“李四”,"插入王五前面",“王五”,“赵六”,"从王五后面插入试试新姿势"]''' names.remove("张三") #删除指定元素 names ''' [“李四”,"插入王五前面",“王五”,“赵六”,"从王五后面插入试试新姿势"]''' names.pop() #删除列的最后一个值 names ''' [“李四”,"插入王五前面",“王五”,“赵六"]'''
扩展
names=["张三",“李四”,“王五”,“赵六”] b = [1,2,3] names.extend(b) names '''["张三",“李四”,“王五”,“赵六”1,2,3]'''
拷贝
names=["张三",“李四”,“王五”,“赵六”] b = [1,2,3] name_cp=names.copy() name_cp '''["张三",“李四”,“王五”,“赵六”]'''
统计
names=["张三",“李四”,“王五”,“赵六”] names.count("张三") 1
排序
names=["张三",“李四”,“王五”,“赵六”,1,2,3] names.sort() #排序 names names.reverse() #反转 names
下标
name.index()
元组
names = ("张三",“李四”,“王五”,“赵六“)
它只有2个方法,一个是count,一个是index,完毕。