Sequence序列是一种有序的数据容器类型,可以通过索引访问其中的元素。常见的序列类型包括字符串String、列表List、元组Tuple等。这里介绍序列的通用操作
索引访问 通过索引编号访问元素。索引编号:从左往右以0开始,从右往左以-1开始
1 2 3 4 5 6 7 8 9 10 11 print ("----------- List: 索引访问 ----------------" )animal_type = ["dog" ,"cat" ,"lion" ] print ("animal type:" , animal_type)print ("animal_type[0]:" , animal_type[0 ]," animal_type[1]:" , animal_type[1 ],"animal_type[2]:" , animal_type[2 ])print ("animal_type[-3]:" , animal_type[-3 ]," animal_type[-2]:" , animal_type[-2 ],"animal_type[-1]:" , animal_type[-1 ])print ("----------- Tuple: 索引访问 ----------------" )ages = (18 , 69 , 35 ) print ("ages:" , ages)print ("ages[0]:" , ages[0 ]," ages[1]:" , ages[1 ]," ages[2]:" , ages[2 ])print ("ages[-3]:" , ages[-3 ]," ages[-2]:" , ages[-2 ]," ages[-1]:" , ages[-1 ])
查询索引 查询序列中元素第一次 出现时的索引。如果元素不存在时,会抛异常
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 print ("----------- List: 查询元素第一次出现时的索引 ----------------" )animal_type = ["dog" ,"cat" ,"lion" ] print ("animal type:" , animal_type)print ("cat index :" , animal_type.index("cat" ))print ("lion index :" , animal_type.index("lion" ))print ("----------- Tuple: 查询元素第一次出现时的索引 ----------------" )ages = (18 , 69 , 35 , 69 ) print ("ages:" , ages)print ("69 index :" , ages.index(69 ))print ("----------- String: 查询元素第一次出现时的索引 ----------------" )name = "Aaron.Bob.Tony" print ("name :" , name)print ("B index" , name.index("B" ))print ("Bob index" , name.index("Bob" ))
切片 切片语法如下:
变量[start:] :提取start(包含)开始后的所有元素
变量[start:end] :提取start(包含)开始到end(不包含)的所有元素
变量[:end] :提取0(包含)开始到end(不包含)的所有元素
变量[:] :提取0(包含)开始后的所有元素
1 2 3 4 5 6 7 8 9 10 11 12 13 14 print ("----------- List: 切片 ----------------" )animal_type = ["dog" ,"cat" ,"lion" ] print ("animal type" , animal_type)print ("animal_type[-2:] : " , animal_type[-2 :])print ("animal_type[1:2] : " , animal_type[1 :2 ])print ("animal_type[:-1] : " , animal_type[:-1 ])print ("----------- Tuple: 切片 ----------------" )ages = (18 , 69 , 35 ) print ("ages:" , ages)print ("ages[0:2] : " , ages[0 :2 ])print ("ages[2:] : " , ages[2 :])print ("ages[:] : " , ages[:])
事实上,切片时还可以显式指定step步长。否则默认为1。意为取出的元素索引的差值
1 2 3 4 5 6 print ("----------- 切片步长 ----------------" )nums = (0 ,11 ,2 ,33 ,4 ,55 ,6 ,77 ,8 ,99 ) print ("nums: " , nums)print ("nums[0:6:2] : " , nums[0 :6 :2 ])print ("nums[-8::3] : " , nums[-8 ::3 ])print ("nums[::4] : " , nums[::4 ])
步长可以为负数。即从start处(包含)开始向前按指定步长提取元素,直到end处(不包含)停止。显然此时, start索引指向的元素 必须在 end索引指向的元素 的后边
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 print ("----------- 切片负数步长 ----------------" )nums = (0 ,11 ,2 ,33 ,4 ,55 ,6 ,77 ,8 ,99 ) print ("nums:" , nums)print ("nums[2:6:-2] : " , nums[2 :6 :-2 ])print ("nums[6:2:-2] : " , nums[6 :2 :-2 ])print ("nums[-1:2:-2] : " , nums[-1 :2 :-2 ])print ("nums[6:0:-2] : " , nums[6 :0 :-2 ])print ("nums[6::-2] : " , nums[6 ::-2 ])print ("nums[7::-3] : " , nums[7 ::-3 ])print ("nums[:2:-2] : " , nums[:2 :-2 ])print ("nums[::-1] : " , nums[::-1 ])print ("nums[::-2] : " , nums[::-2 ])
加法 将指定序列拼接成一个新序列。仅支持对相同类型的序列进行加法操作
1 2 3 4 5 6 7 8 9 10 11 print ("----------- List: 加法 ----------------" )animal_type = ["dog" ,"cat" ,"lion" ] animal_types = animal_type + animal_type print ("animal type:" , animal_type)print ("animal types:" , animal_types)print ("----------- Tuple: 加法 ----------------" )age = (18 , 69 , 35 ) ages = age+age+age print ("age :" , age)print ("ages :" , ages)
乘法 将指定序列重复n次来创建一个新序列
1 2 3 4 5 6 7 8 9 10 11 print ("----------- List: 乘法 ----------------" )animal_type = ["dog" ,"cat" ,"lion" ] animal_types = animal_type * 3 print ("animal type:" , animal_type)print ("animal types:" , animal_types)print ("----------- Tuple: 乘法 ----------------" )age = (18 , 69 , 35 ) ages = (18 , 69 , 35 )*2 print ("age :" , age)print ("ages :" , ages)
进行序列乘法时,如果序列中的元素是可变对象的引用时,可能会引发问题
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 board1 = [ ["_" ]*3 ] * 4 print (f"board 1 #1: {board1} " )board1[0 ][1 ] = "X" print (f"board 1 #2: {board1} " )board2 = [] row = ["_" ] * 3 for i in range (4 ): board2.append(row) print (f"board 2 #3: {board2} " )board2[2 ][0 ] = "X" print (f"board 2 #4: {board2} " )
此时,可以考虑使用推导式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 board3 = [ ["_" ]*3 for i in range (4 ) ] print (f"board 3 #1: {board3} " )board3[0 ][1 ] = "X" print (f"board 3 #2: {board3} " )board4 = [] for i in range (4 ): row = ["_" ] * 3 board4.append(row) print (f"board 4 #3: {board4} " )board4[0 ][1 ] = "X" print (f"board 4 #4: {board4} " )
成员资格判断 判断指定元素是否 在/不在 序列中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 print ("----------- String: 成员资格判断 ----------------" )animal_name = "Aaron.Tom.Bob" print ("name:" , animal_name)print ("Tom in name:" , "Tom" in animal_name)print ("Tom in name:" , "B" in animal_name)print ("Tony not in name:" , "Tony" not in animal_name)print ("Tom.B not in name:" , "Tom.B" not in animal_name)print ("----------- List: 成员资格判断 ----------------" )animal_types = ["dog" ,"cat" ,"lion" ] print ("animal type:" , animal_types)print ("cat in animal type:" , "cat" in animal_types)print ("cat not in animal type:" , "cat" not in animal_types)print ("pig in animal type:" , "pig" in animal_types)print ("pig not in animal type:" , "pig" not in animal_types)print ("----------- Tuple: 成员资格判断 ----------------" )age = (18 , 69 , 35 ) print ("age:" , age)print ("35 in age:" , 35 in age)print ("35 not in age:" , 35 not in age)print ("2222 in age:" , 2222 in age)print ("2222 not in age:" , 2222 not in age)
序列长度、最小元素、最大元素 获取序列的长度、最小的元素、最大的元素
1 2 3 4 5 6 7 8 9 10 11 12 13 print ("----------- List: 序列长度、最小元素、最大元素 ----------------" )animal_types = ["dog" ,"zoo" ,"lion" ,"aaron" ] print ("animal type:" , animal_types)print ("len(animal_types) :" , len (animal_types))print ("min(animal_types) :" , min (animal_types))print ("max(animal_types) :" , max (animal_types))print ("----------- Tuple: 序列长度、最小元素、最大元素 ----------------" )age = (18 , 69 , 35 , 11 ) print ("age:" , age)print ("len(age) :" , len (age))print ("min(age) :" , min (age))print ("max(age) :" , max (age))
参考文献
Python编程·第3版:从入门到实践 Eric Matthes著
Python基础教程·第3版 Magnus Lie Hetland著
流畅的Python·第1版 Luciano Ramalho著