Python中的序列


 

 

什么是序列?

  列表、元组和字符串都是序列。为了让我们方便的在序列中抓取一个特定的项目,具备这样的特点:

  1、成员检验

  2、索引操作符

Python在不断进步,这种项目不断增加, 见https://docs.python.org/2.7/library/stdtypes.html#typesseq

 

Sequence Types — strunicodelisttuplebytearraybufferxrange

一些基本操作:

Operation

Result

Notes

in s

True if an item of s is equal to x, else False

 

not in s

False if an item of s is equal to x, else True

 

t

the concatenation of s and t

 

n, s

equivalent to adding s to itself n times

 

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)

index of the first occurrence of x in s

 

s.count(x)

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)其实只是元组封装序列拆封的一个结合。

 

posted @ 2020-07-04 10:49  PiaYie  阅读(187)  评论(0编辑  收藏  举报