一、列表(list)
序列是 Python 中最基本的数据结构,序列中的每个值都有对应的位置值,称之为索引,第一个索引是 0,第二个索引是 1,依此类推。
列表可以进行的操作包括索引,切片,追加,删除,长度,切片,循环,包含。
创建一个列表,用 [ ] 定义,元素之间使用 , 分隔
#创建列表 list1 = ['hello', 23, '张三', 70.2,8.88] 或者 list2 =list (['hello', 23, '张三', 70.2,8.88]) #创建一个空列表 list = []
1、索引
与字符串的索引一样,列表索引从 0 开始,第二个索引是 1,依此类推
list = ['hello', 23, '张三', 70.2,None, True, ["xiaohua", "tony",45],8.88] #取出列表中第一个元素,hello print( list[0] ) print( list[-8]) #可以连续取值,如取出列表["xiaohua", "tony",45]中的tony print( list[6][1])
2、切片
与字符串的索引一样,可以使用方括号 [] 来截取列表,截取的语法格式为:列表[开始索引:结束索引:步长]
• 指定的区间属于“左闭右开” 型,从起始位开始,到结束位的前一位 结束(不包含结束位本身)
• 从头开始,开始索引的数字 0 可以省略,冒号不能省略
• 到末尾结束,结束索引的数字-1可以省略,冒号不能省略
• 步长默认为 1,如果连续切片,数字和冒号都可以省略
• 步长的值为负数时,表示从右向左取字符串,步长的绝对值大于1表示间隔的取数
list = ['hello', 23, '张三', 66,70.2,None, True, ["xiaohua", "tony",45],8.88,'sunny','梨花'] print(list[0:3]) # 通过顺序索引,获取['hello', 23, '张三'] print(list[:3]) # 起始索引为 0 可以省略,['hello', 23, '张三'] print(list[:]) # 全部取出['hello', 23, '张三', 66, 70.2, None, True, ['xiaohua', 'tony', 45], 8.88, 'sunny', '梨花'] print(list[::-1]) # 倒序输出 ['梨花', 'sunny', 8.88, ['xiaohua', 'tony', 45], True, None, 70.2, 66, '张三', 23, 'hello'] print(list[6:10]) # 顺序获取[True, ['xiaohua', 'tony', 45], 8.88, 'sunny'],默认步长为1 print(list[-5:-2]) # 倒序获取[True, ['xiaohua', 'tony', 45], 8.88],默认步长为1 print(list[6:10:2]) # 顺序获取[True, 8.88],默认步长为2 print(list[-5:-2:2]) # 倒序获取[True, 8.88],默认步长为2
3、增
• 追加(append())
在列表后面追加元素,新增加的元素追加到列表末尾,每次只能追加一个元素
list = ['hello', 23, '张三',70.2,None, True, ["xiaohua", "tony",45],'sunny'] list1 = ["wangxian","john",13] list.append("哈哈哈") # 追加字符串 list.append(12) # 追加数字 list.append([1,3,5,"学院"]) # 追加列表 list.append(list1) # 追加另一个列表 print(list)
• 追加(extend())
在列表后面追加元素,新增加的元素追加到列表末尾,每次只接受一个列表作为参数,并将该参数的每个元素都添加到原有的列表中
list = ['hello', 23, '张三',70.2,None, True, ["xiaohua", "tony",45],'sunny'] list1 = ["wangxian","john",13] list.extend("哈哈哈") # 追加字符串 list.extend([1,3,5,"学院"]) # 追加列表 list.extend(list1) # 追加另一个列表 print(list) #结果为:['hello', 23, '张三', 70.2, None, True, ['xiaohua', 'tony', 45], 'sunny', '哈', '哈', '哈', 1, 3, 5, '学院', 'wangxian', 'john', 13]
• 插入(insert())
在指定位置插入一个元素,格式为insert(index,msg),index为列表的索引位置,msg为插入内容
list = ['hello', 23, '张三',70.2,None, True, ["xiaohua", "tony",45],'sunny'] list1 = ["wangxian","john",13] list.insert(2,"哈哈哈") # 追加字符串 list.insert(2,12) # 追加数字 list.insert(-1,[1,3,5,"学院"]) # 追加l列表 list.insert(-1,list1) # 追加另一个列表 # 如果索引超出范围,则如果索引为正,则该项目将追加到末尾;如果索引为负,则将其追加到开头 list.insert(8,'最后一个') list.insert(-10,'第一个') print(list)
4、修改
根据列表索引可更新或者修改列表的值,格式为 list[index] = 新值
list = ['hello', 23, '张三',70.2,None, True, ["xiaohua", "tony",45],'sunny'] list[2] = "李四" #修改列表元素['hello', 23, '李四', 70.2, None, True, ['xiaohua', 'tony', 45], 'sunny'] list[6][1] ='啦啦' #修改列表中的列表元素['hello', 23, '张三', 70.2, None, True, ['xiaohua', '啦啦', 45], 'sunny'] list[2:4] = [1,2] #批量修改['hello', 23, 1, 2, None, True, ['xiaohua', 'tony', 45], 'sunny'] list[2:4] = [1,2,3,4,5,6,7,8,9] #批量修改['hello', 23, 1, 2, 3, 4, 5, 6, 7, 8, 9, None, True, ['xiaohua', 'tony', 45], 'sunny'] print(list)
5、删除
• pop()方法
pop() 函数用于移除列表中的一个元素(默认最后一个元素),并且返回该元素的值,语法:list.pop(index),index为列表的索引,默认index= -1
list = ['hello', 23, '张三',70.2,None, True, ["xiaohua", "tony",45],'sunny'] print(list.pop()) # 默认删除最后一个元素sunny,并返回sunny print(list.pop(2)) # 默认指定元素张三,并返回张三 # 注意:如果传递给pop()方法的索引不在范围内,则会引发IndexError:pop index out of range异常 print(list.pop(20)) # IndexError: pop index out of range
• remove()方法
remove()函数没有返回值,但是会移除列表中的某个值的第一个匹配项
list = ['hello', 23, '张三',70.2,None,'张三', True, ["xiaohua", "tony",45],'sunny'] list.remove('张三') # 移除匹配到的第一个元素张三 ['hello', 23, 70.2, None, '张三', True, ['xiaohua', 'tony', 45], 'sunny'] list.remove('hello') # 移除元素hello [23, '张三', 70.2, None, '张三', True, ['xiaohua', 'tony', 45], 'sunny'] # 注意: 如果要移除的参数不在列表内,则会引发ValueError: list.remove(x): x not in list异常 list.remove('李四') # ValueError: list.remove(x): x not in list print(list)
• del方法
根据索引删除,也就是根据位置删除元素,格式为:del list[index],index为列表的索引。也可以根据列表变量名直接删除整个列表如,del name
list1 = ['hello', 23, '张三',70.2,None, True, ["xiaohua", "tony",45],'sunny'] del list1[2] #删除指定位置的值 张三 ['hello', 23, 70.2, None, True, ['xiaohua', 'tony', 45], 'sunny'] del list1[2:5] #删除指定位置的值'张三',70.2,None,['hello', 23, True, ['xiaohua', 'tony', 45], 'sunny'] del list1 #删除整个列表 NameError: name 'list1' is not defined print(list1)
• clear()方法
清空列表
list = ['hello', 23, '张三',70.2,None, True, ["xiaohua", "tony",45],'sunny'] list.clear() #清空列表 [] print(list)
6、列表常用函数
• 最大(max),最小(min),统计(count),长度(len)
list1 = [11,22,44,23,32,5,10,21,43] print(max(list1)) # 最大值 44 print(min(list1)) # 最小值 5 print(len(list1)) # 列表长度,即元素个数 9 print(list1.count(22)) # 元素出现的次数 1 print(list1.index(44)) # 获取元素索引 3,若是有多个相同元素,则取第一个元素的索引
• 排序(sort和sorted),反转(reverse)
sort和sorted两种方式的区别:
- sort在原序列上进行直接修改,sorted会生成一个新的序列,不影响原序列
- list.sort()方法仅被定义在list中,sorted()方法对所有的可迭代序列都有效
list1 = [11,22,44,23,32,5,10,21,43] #排序 list1.sort() #排序,升序,默认从小到大 [5, 10, 11, 21, 22, 23, 32, 43, 44] list1.sort(reverse=True) #排序,降序,从大到小[44, 43, 32, 23, 22, 21, 11, 10, 5] print(list1) list2=sorted(list1) #排序,升序,默认从小到大 [5, 10, 11, 21, 22, 23, 32, 43, 44] list2=sorted(list1,reverse=True) #排序,降序,从大到小[44, 43, 32, 23, 22, 21, 11, 10, 5] print(list2) # 注意:3.0里不同数据类型不能放在一起排序了,报TypeError: '<' not supported between instances of 'str' and 'int'异常 list1 = [11,22,44,23,32,5,10,21,43,'张三'] list1.sort() # TypeError: '<' not supported between instances of 'str' and 'int' print(list1) #反转 list1.reverse() #反转 ,降序输出 [43, 21, 10, 5, 32, 23, 44, 22, 11] print(list1)
• 复制(copy)
list1 = ['hello', 23, '张三',70.2,None, True, ["xiaohua", "tony",45],'sunny'] list2 = list1.copy() #复制 print(list2)
二、元组(tuple)
Python 的元组与列表类似,不同之处在于元组的元素不能修改。
创建一个元组,使用小括号 ( ),元素之间使用逗号分隔。
#创建元组 t = ('hello', 23, '张三', 70.2,8.88) t= tuple(('hello', 23, '张三', 70.2,8.88)) t = 'hello', 23, '张三', 70.2,8.88 # 不需要括号也可以 # 创建空元组 t1 = () # 注意:元组中只包含一个元素时,需要在元素后面添加逗号,否则括号会被当作运算符使用 t2 = (18,) #虽然tuple的元素不可改变,但它可以包含可变的对象,比如list列表。 tuple = ( 'hello', 23, '张三',8.88,[11,45,67] )
1、索引
与列表的索引一样,列表索引从 0 开始,第二个索引是 1,依此类推,元组也可以使用下标索引来访问元组中的值
t = ('hello', 23, '张三', 70.2,None, True, ["xiaohua", "tony",45],8.88,(11,33,44)) #取出元组中第一个元素,hello print( t[0] ) print( t[-8]) #可以连续取值, print( t[6][1]) # 取出元组下列表中的["xiaohua", "tony",45]中的tony print(t[8][1]) # 取出元组下元组中的(11,33,44)中的33
2、切片
与列表的切片方式一致,截取的语法格式为:元组[开始索引:结束索引:步长]
t = ('hello', 23, '张三', 66,70.2,None, True, ["xiaohua", "tony",45],8.88,'sunny','梨花',("lele","开心",12)) print(t[0:3]) # 通过顺序索引,获取['hello', 23, '张三'] print(t[:3]) # 起始索引为 0 可以省略,['hello', 23, '张三'] print(t[:]) # 全部取出('hello', 23, '张三', 66, 70.2, None, True, ['xiaohua', 'tony', 45], 8.88, 'sunny', '梨花', ('lele', '开心', 12)) print(t[::-1]) # 倒序输出 (('lele', '开心', 12), '梨花', 'sunny', 8.88, ['xiaohua', 'tony', 45], True, None, 70.2, 66, '张三', 23, 'hello') print(t[6:10]) # 顺序获取(True, ['xiaohua', 'tony', 45], 8.88, 'sunny'),默认步长为1 print(t[-6:-2]) # 倒序获取(True, ['xiaohua', 'tony', 45], 8.88, 'sunny'),默认步长为1 print(t[6:10:2]) # 顺序获取(True, 8.88),默认步长为2 print(t[-6:-2:2]) # 倒序获取(True, 8.88),默认步长为2
3、修改
元组中的元素值是不允许修改的,但我们可以对元组进行连接组合
t1 = ('张三', '李四','王五','sunny') t2 = (18,22,32,25) # 创建一个新的元组 t3 = t1 + t2 #('张三', '李四', '王五', 'sunny', 18, 22, 32, 25), print(t3)
4、删除
元组中的元素值是不允许删除的,但我们可以使用del语句来删除整个元组
t1 = ('张三', '李四','王五','sunny') print(t1) # 删除前,('张三', '李四', '王五', 'sunny') del t1 print(t1) # 删除后,NameError: name 't1' is not defined
5、列表与元组数据类型转换
# 元组转换为列表 t1 = ('张三', '李四','王五','sunny',[22,'haha','啦啦'],11,55,43,("wangwang",'小花')) L1= list(t1) #['张三', '李四', '王五', 'sunny', [22, 'haha', '啦啦'], 11, 55, 43, ('wangwang', '小花')] print(L1) # 列表转换为元组 L1=['张三', '李四', '王五', 'sunny', [22, 'haha', '啦啦'], 11, 55, 43, ('wangwang', '小花')] t1 = tuple(L1) print(t1)
6、元组常用函数
• 最大(max),最小(min),统计(count),长度(len)
t1 = (11,22,44,23,32,5,10,21,43) print(max(t1)) # 最大值 44 print(min(t1)) # 最小值 5 print(len(t1)) # 列表长度,即元素个数 9 print(t1.count(22)) # 元素出现的次数 1 print(t1.index(44)) # 获取元素索引 3,若是有多个相同元素,则取第一个元素的索引
• 排序(sorted)
t1 = (11,22,44,23,32,5,10,21,43) t2 = sorted(t1) #排序,升序,默认从小到大 [5, 10, 11, 21, 22, 23, 32, 43, 44] t2 = sorted(t1,reverse=True) #排序,降序,从大到小[44, 43, 32, 23, 22, 21, 11, 10, 5] print(t2) # 注意:3.0里不同数据类型不能放在一起排序了,报TypeError: '<' not supported between instances of 'str' and 'int'异常 t1 = (11,22,44,23,32,5,10,21,43,'张三') t2 = sorted(t1) # TypeError: '<' not supported between instances of 'str' and 'int' print(t2)