合并两个双端队列的最快方法

7
有比这更快的合并两个双端队列的方法吗?
# a, b are two deques. The maximum length 
# of a is greater than the current length 
# of a plus the current length of b

while len(b):
  a.append(b.popleft())

请注意,我不关心保留输入deque,我只关心尽快获得合并后的deque。
1个回答

10

不需要逐个元素添加,只需使用+=即可:

from collections import deque

a = deque([1, 2, 3])
b = deque([4, 5, 6])

a += b

print(a)

deque([1, 2, 3, 4, 5, 6])

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接