0

我有一个deque最大长度为 10 的。可以.extendleft添加一个元组。例如:

from collections import deque

x = deque(maxlen = 10)
z = [(1,4),(1,3),(2,5),(7,1),(6,4),(8,2),(9,1),(9,5),(7,3),(8,3)] 
for y in z:
    x.append(y)
print x
x.extendleft((6,7))
print x

回报:

deque([(1, 4), (1, 3), (2, 5), (7, 1), (6, 4), (8, 2), (9, 1), (9, 5), (7, 3), (8, 3)], maxlen=10)
deque([7, 6, (1, 4), (1, 3), (2, 5), (7, 1), (6, 4), (8, 2), (9, 1), (9, 5)], maxlen=10)

我试过:pair = (6,7) x.extendleft(pair) 结果相同。

4

1 回答 1

3
In [7]: x.extendleft([(6,7)])

In [8]: x
Out[8]: deque([(6, 7), (1, 4), (1, 3), (2, 5), (7, 1), (6, 4), (8, 2), (9, 1), (9, 5), (7, 3)], maxlen=10)

或者,更简单:

In [10]: x.appendleft((6, 7))
于 2013-01-24T18:31:03.867 回答