Python中的序列
什么是序列?
列表、元组和字符串都是序列。为了让我们方便的在序列中抓取一个特定的项目,具备这样的特点:
1、成员检验
2、索引操作符
Python在不断进步,这种项目不断增加, 见https://docs.python.org/2.7/library/stdtypes.html#typesseq
Sequence Types — str
, unicode
, list
, tuple
, bytearray
, buffer
, xrange
一些基本操作:
Operation |
Result |
Notes |
---|---|---|
|
|
|
|
|
|
|
the concatenation of s and t |
|
|
equivalent to adding s to itself n times |
|
|
ith item of s, origin 0 |
|
|
slice of s from i to j |
|
|
slice of s from i to j with step k |
|
|
length of s |
|
|
smallest item of s |
|
|
largest item of s |
|
|
index of the first occurrence of x in s |
|
|
total number of occurrences of x in s
|
下标操作:
使用索引来取得序列中的单个项目
shoplist = ['apple', 'mango', 'carrot', 'banana'] name = 'Piayie' print('Item 0 is', shoplist[0]) print('Item 1 is', shoplist[1]) print('Item 2 is', shoplist[2]) print('Item 3 is', shoplist[3]) print('Item -1 is', shoplist[-1]) print('Item -2 is', shoplist[-2]) print('Character 0 is', name[0])
输出:
Item 0 is apple Item 1 is mango Item 2 is carrot Item 3 is banana Item -1 is banana Item -2 is carrot Character 0 is P
当用方括号中的一个数来指定一个序列时候,Python会为我们抓取序列对应位置的项目。注意:Python从0开始计数。
当索引标是负数时候,位置是从列尾开始计算的,shoplist[-1]表示最后一个元素。
切片操作
切片操作符是序列名后跟一个方括号,方括号中有一对可选的数字,并用冒号分割。注意这与你使用的索引操作符十分相似。记住数是可选的,而冒号是必须的。
切片操作符中的第一个数表示切片开始的位置,第二个表示切片结束的位置。
如果不指定第一个数,默认从序列首部开始。
如果不指定第二个数,Python会在系列尾部停止。
可以用负数做切片,得到从序列尾[-1]开始计算的位置。
特别的,返回的序列是从指定开始位置起始,在指定结束位置之前终止,也就是说切出的序列不包括尾部项目。
print('Item 1 to 3 is', shoplist[1:3]) print('Item 2 to end is', shoplist[2:]) print('Item 1 to -1 is', shoplist[1:-1]) print('Item start to end is', shoplist[:]) print('characters 1 to 3 is', name[1:3]) print('characters 2 to end is', name[2:]) print('characters 1 to -1 is', name[1:-1]) print('characters start to end is', name[:])
输出:
Item 1 to 3 is ['mango', 'carrot'] Item 2 to end is ['carrot', 'banana'] Item 1 to -1 is ['mango', 'carrot'] Item start to end is ['apple', 'mango', 'carrot', 'banana'] characters 1 to 3 is ia characters 2 to end is ayie characters 1 to -1 is iayi characters start to end is Piayie
我们必须使得这个切片是正向的,也就是说起始位置不能落后于结束位置,否则就会引发SyntaxError
print('characters -1 to 2 is', name[1-:2])
输出:
File "d:\PiaYie\VScode\python学习笔记\Subscription.py", line 20 print('characters -1 to 2 is', name[1-:2]) ^ SyntaxError: invalid syntax D:\PiaYie\VScode\python学习笔记>
和大部分的pick方法一样,切片操作符还有第三个参数,我们把它叫做步长。
print('characters -1 to 2 is', name[::2])
输出:
characters begin to end via step = 2 is Pai
特别的,我们应该注意切片操作是一种创建副本的operation
若要在循环内部修改正在遍历的序列(例如复制某些元素),首先应该制作副本。
在序列上循环不会隐式地创建副本。切片表示法使这尤其方便:
>>> words = ['cat', 'window', 'defenestrate'] >>> for w in words[:]: # Loop over a slice copy of the entire list. ... if len(w) > 6: ... words.insert(0, w) ... >>> words ['defenestrate', 'cat', 'window', 'defenestrate']
序列拆封
>>> t = 12345, 54321, 'hello!' >>> x, y, z = t >>> x 12345 >>> y 54321 >>> z 'hello!'
这个调用等号右边可以是任何线性序列,称之为 序列拆封 非常恰当。序列拆封要求左侧的变量数目与序列的元素个数相同。
要注意的是可变参数(multiple assignment)其实只是元组封装和序列拆封的一个结合。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~