Deque objects support the following methods: append(x)¶ Add x to the right side of the deque. appendleft(x) Add x to the left side of the deque. clear() Remove all elements from the deque leaving it with length 0. copy() Create a shallow copy of the
from collections import deque mydquene = deque() mylist = [0,1,1,2,2,3,3,3,3,4,5,6,7,7,8,8,9,10,10,11,22,33,22] mydquene.extend(mylist) for i in set(mylist): n = mydquene.count(i) if n>1: print("其中元素{}出现了{}次\r\n".format(i,n))
collections模块中的deque对象是一个队列,它有着正常队列的先进先出原则.我们可以利用这个对象来实现数据的保存功能. 例如,现有一个猜数字大小的游戏,系统开始会随机roll点一个0-100的整数,然后键盘开始接收你的输入,每次输入的数字系统会告诉你比这个随机数大还是小,直到你猜对了程序才会结束.如下图这样: 代码见: from collections import deque from random import randint N = randint(0, 100) history