序列元组赋值

​ 元组或者序列中的值,可以直接赋给变量,但是数量要相等。

​ 字符串也可以直接赋值。

​ 对于不需要的值,可以用空变量获取,然后舍弃不要。

代码实例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
>>> p = (2,3)
>>> x,y=p
>>> x
2
>>> y
3
>>> s='hallo'
>>> a,d,f,g,h=s
>>> a
'h'
>>> d

解压可迭代对象赋值给多个变量

1
2
3
4
>>> record=('hi','buhai','haibuhai')
>>> name,*aorther =record
>>> aorther
['buhai', 'haibuhai']

星号可以获取其余所有对象,返回结果总是列表。

Collection.deque() 返回一个两头队列deque(double ended queue)

可以用来保留最后N和元素

实例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
from collections import deque

q=deque(maxlen=3)
q.append(1)
q.append(2)
q.append(3)
q.append(4)
print(q)
###########################
输出:
deque([2, 3, 4], maxlen=3)