from collections import deque q = deque(maxlen=5) for x in range(10): q.append(x) q maxlen:保留指定长度的元素 转载于:https://blog.51cto.com/liuzhengwei521/1895085