
索引访问
通过索引编号访问元素。索引编号:从左往右以0开始,从右往左以-1开始
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])

查询索引
查询序列中元素第一次出现时的索引。如果元素不存在时,会抛异常
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(包含)开始后的所有元素
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。意为取出的元素索引的差值
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索引指向的元素 的后边
print("----------- 切片负数步长 ----------------")
nums = (0,11,2,33,4,55,6,77,8,99)
print("nums:", nums)
# 负数步长下, start索引指向的元素 必须在 end索引指向的元素 的后边
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])
# 负数步长下,当end索引省略时,意为可以向前提取所有元素
print("nums[6::-2] : ", nums[6::-2])
print("nums[7::-3] : ", nums[7::-3])
# 负数步长下,当start索引省略时,意为从最后一个元素开始向前提取元素
print("nums[:2:-2] : ", nums[:2:-2])
# 负数步长下,当start、end索引都省略时,意为从最后一个元素开始可以向前提取所有元素
print("nums[::-1] : ", nums[::-1])
print("nums[::-2] : ", nums[::-2])

加法
将指定序列拼接成一个新序列。仅支持对相同类型的序列进行加法操作
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次来创建一个新序列
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)

进行序列乘法时,如果序列中的元素是可变对象的引用时,可能会引发问题
# 构建一个4*3的棋盘: 使用序列乘法
board1 = [ ["_"]*3 ] * 4
print(f"board 1 #1: {board1}")
# 只是修改第0行、第1列的元素
board1[0][1] = "X"
# 说明board1中4个列表类型的元素,并不是4个不同的列表对象,而是指向同一个相同的列表的引用
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}")

此时,可以考虑使用推导式
# 构建一个4*3的棋盘: 使用推导式
board3 = [ ["_"]*3 for i in range(4) ]
print(f"board 3 #1: {board3}")
# 只是修改第0行、第1列的元素
board3[0][1] = "X"
# 说明board2中4个列表类型的元素,指向4个不同的列表对象
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}")

成员资格判断
判断指定元素是否 在/不在 序列中
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)

序列长度、最小元素、最大元素
获取序列的长度、最小的元素、最大的元素
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著
原文始发于微信公众号(青灯抽丝):Python 数据容器之Sequence序列
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/283422.html