p = (4,5)
x,y = p
str = "x = {x}, y = {y}"
print(str.format(x = x, y = y))
输出结果:
‘x = 4,y = 5’
p = (4,5,6,7,8,9,10)
x,y,*final = p
print(x)
print(y)
print(final)
result:
4
5
[6,7,8,9,10] #剩余的元素赋值给一个List
'''使用collections模块中的deque(队列)实现'''
from collections import deque
p = [1,2,3,4,5,6,7,8,9]
queue = deque(maxlen = 3) #保留3个元素
for x in p:
queue.append(x)
print(queue)
#result:
>'deque([8, 9, 10], maxlen=3)' #只保留了p中的最后3个元素,当向queue中增加元素时,若已满,则会清除已有的最老的元素。
import heapq
p = [12,231,143,121,5454,1,323,5454,11,33]
print(heapq.nlargest(3,p))
print(heapq.nsmallest(3,p))
# result:
> [5454, 5454, 323]
> [1, 11, 12]
> # heapq 是一个堆结构存储。同时能够进行设置key来用于复杂的数据结构.