Python序列类型详解:list、tuple与range的创建及操作指南
本文深入解析Python中三种核心序列类型:list、tuple和range。详细阐述list的可变特性、tuple的不可变特性及其创建语法,重点说明range函数的两种参数形式及在循环中的应用。同时,系统介绍序列的通用操作(如索引、切片)与list特有的增删改方法(如append、pop、del),帮助开发者掌握高效的数据处理技巧。
Python序列类型概述
本文详细解析Python中基本数据结构序列类型的核心用法,重点涵盖list、tuple和range三种类型。内容旨在为读者提供清晰的操作指南与最佳实践,供学习参考。
list与tuple基础对比
list 和 tuple 基础对比
- list(列表):使用方括号 [] 定义,具有有序性,且允许修改其中的元素。
- tuple(元组):使用圆括号 () 定义,同样具有有序性,但其核心特征在于元素不可修改,这是与list的主要区别。
list与tuple的创建方法
list 和 tuple 的创建方法
print([]) # 空list
print(["a",1,True]) # 元素类型不限
print([x for x in range(0,6)]) # 列表推导式
print(list("a"),type(list("a"))) # 强制转化
print(()) # 空tuple
print((1)) # 不是tuple
print((1,)) # 单一元素tuple 一定要加,
print(("a",1,True)) # 元素类型不限
print(tuple("a"),type(tuple("a"))) # 强制转化
创建空列表:l = []
创建包含元素的列表:使用一对中括号,内部元素以逗号分隔。l = [a] l = ["a",1,True] 列表中的元素类型可以混合,不受限制。
列表推导式示例:[x for x in range(0,6)](后续章节将详细介绍 range 及列表推导式的用法)。
类型转换:使用 list() 函数可将其他可迭代对象转换为列表。
创建空元组:t = ()
创建单元素元组时,必须注意语法:t = (1,) 括号内必须包含逗号,否则括号仅起到分组作用而非定义元组。
创建多元素元组:使用一对圆括号,内部元素以逗号分隔。t = ("a",1,True) 元素类型同样不限。
类型转换:使用 tuple() 函数可将其他可迭代对象转换为元组。
range函数的用法
range 类型介绍
range 用于高效生成等差数列,支持两种语法形式:range(stop) 和 range(start, stop[, step]),常用于 for 循环语句中。
range(stop) 生成从 0 到 stop(不包含 stop),步长为 1 的整数序列。例如,range(4) 生成 0, 1, 2, 3。
range(start, stop[, step]) 生成从 start 到 stop(不包含 stop),步长为 step 的整数序列。step 默认为 1,也可设置为负数以实现反向生成。
print(type(range(4))) # range本身就是一个type
for i in range(4):
print(i) # 0 1 2 3
for i in range(-1): # 从0计数,无值
print(i)
for i in range(4,7): # 4 5 6
print(i)
for i in range(2,7,2): # 2 4 6
print(i)
for i in range(5,2,-1): # 5 4 3
print(i)
序列的通用操作
序列通用操作
以下操作不会改变原序列对象本身,适用于 list 和 tuple。
| Operation | Result |
|---|---|
| x in s | True if an item of s is equal to x, else False |
| x not in s | False if an item of s is equal to x, else True |
| s + t | the concatenation of s and t |
| s * n or n * s | n shallow copies of s concatenated |
| s[i] | ith item of s, origin 0 |
| s[i:j] | slice of s from i to j |
| s[i:j:k] | slice of s from i to j with step k |
| len(s) | length of s |
| min(s) | smallest item of s |
| max(s) | largest item of s |
| s.index(x[, i[, j]]) | index of the first occurrence of x in s (at or after index i and before index j) |
| s.count(x) | total number of occurrences of x in s |
s = ["a",1,True,["b"],2]
print("a" in s) # 判断元素存在于s
print("a" not in s) # 判断元素不存在于s
print("b" in s)
print(1.0 in s) # 这边不判断int float类型不同
print("1" in s) # 这边的1为字符串
a = [1,2]
b = [2,1,0]
print(a+b) # 序列相加
print(a*3) # 序列乘法
s = [0,1.0,2,3,4,5,6,7,8]
print(s[0],s[2],s[3]) # 通过下标来取出对应的元素
print(type(s[0]))
print(type(s[1]))
print(s[2:4]) # 取出一段list
print(s[2:7:2]) # 根据步长取出一段list
print(len(s)) # list长度,即包含几个元素
sum = 0
for i in range(0,len(s)): # 使用for循环来取出list的每个元素
print(s[i])
sum += i # 赋值的简单表达式,相当于 sum = sum + i
print(sum) # 总和
print(min(s),max(s)) # 取最小/最大;注意元素类型间若不可比较,会报错
s = [2,3,1,2,2,3]
print(s.index(2)) # 查找对应元素第一次出现的下标
# print(s.index(4)) # 不存在该元素会报错
print(s.index(2,3)) # 从下标为3的开始找起
print(s.index(2,3,4)) # 从下标为3到下标4的阶段内找
print(s.count(2)) # 输出为2的元素的个数
print(s.count("2")) # 找不到匹配元素,返回0
上述列出的通用操作方法对 tuple 同样适用,因为这些操作不修改序列内的元素。例如:
s = (2,3,1,2,2,3)
print(s[2],s[2:4],len(s),s.count(2)) # 对tuple均适用
list的修改操作与注意事项
修改序列内容的操作仅适用于 list;若对 tuple 执行此类操作,将引发错误。clear() 和 copy() 是 Python 3.3 版本新增的方法。
| Operation | Result |
|---|---|
| s[i] = x | item i of s is replaced by x |
| s[i:j] = t | slice of s from i to j is replaced by the contents of the iterable t |
| s[i:j:k] = t | the elements of s[i:j:k] are replaced by those of t |
| del s[i:j] | same as s[i:j] = [] |
| del s[i:j:k] | removes the elements of s[i:j:k] from the list |
| s.pop([i]) | retrieves the item at i and also removes it from s |
| s.remove(x) | remove the first item from s where s[i] == x |
| s.clear() | removes all items from s (same as del s[:]) |
| s.append(x) | appends x to the end of the sequence (same as s[len(s):len(s)] = [x]) |
| s.extend(t) | extends s with the contents of t (same as s[len(s):len(s)] = t) |
| s.insert(i, x) | inserts x into s at the index given by i (same as s[i:i] = [x]) |
| s.copy() | creates a shallow copy of s (same as s[:]) |
| s.reverse() | reverses the items of s in place |
list 的增、删、改操作在实际开发中极为常用,建议熟练掌握。
list 元素修改
可以通过不同的下标表示法对 list 进行操作。通常,list 下标操作用于修改单一元素,例如 s[0]=1;对多个元素的操作见下方示例(仅供参考)。
s = [0,1,2,3]
s[0] = "1"
print(s) # 对list的某一元素赋另外的值,类型也跟随改变
s[4] = 1 # 不可超过原list的长度,会报错
s[0:3] = [2,3,4] # 可对一段元素赋另外的值
print(s)
s[0:3] = ["x","x"] # 可缺少,元素个数也就相应的减少了
print(s)
s[0:2] = ["x","x","x","x"] # 可增加,元素个数也就相应的减加了
print(s)
s[0] = [0,0] # 单个元素注意,相当于赋值,把序列赋予该元素
print(s)
s[1:2] = [0,0]
print(s)
s = [0,1,2,3,4,5,6,7,8]
s[1:8:2] = ["x"]*4
# s[1:8:2] = ["x"]*3 # 这种表示方式元素个数一定需要相同,不然会报错
print(s)
list 元素删除
s = [0,1,2,3,4,5,6,7,8]
del s[0:4] # 删除对应的元素
print(s)
s = [0,1,2,3,4,5,6,7,8]
del s[1:8:2] # 做删除
print(s)
s = [0,1,2,3,4,5,6,7,8]
s.pop(3)
print(s.pop(3),s) # 做删除,并且返回该元素的值
print(s.pop(),s) # 默认删除最后一个
s = [2,"1",1.0,1,2,1]
s.remove(1) # 删除第一个值为 1 的元素
print(s)
s.clear() # 置空,Python3.3引入
print(s)
list 元素增加
s = [0,1,2,3,4]
s.append(5) # list 最后加一个元素
print(s)
s.extend([6,7]) # list 最后拼接序列
print(s)
s.extend(range(3))
print(s)
s.insert(1,["x"]) # 在1的位置插入["x"]
print(s)
其他辅助操作,如 reverse(反转)、copy(拷贝)等。
s = [1,2,3]
c = s.copy() # 相当于 c = s
print(c)
c.reverse()
print(c)
s = [2,3,1,4]
s.sort() # 排序
print(s)
# s = ["b",1,"a",True] # 报错,必须是可比较的类型
s = ["b","a"]
s.sort()
print(s)
更多关于Python相关内容感兴趣的读者可查看本站专题:《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》
希望本文所述对大家Python程序设计有所帮助。
免责声明:本文内容仅供参考,不代表本站观点。转载或引用请注明出处。









