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

标签 python merge deque

是否存在比这更快的合并两个双端队列的方法?

# 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())

请注意,我对保留输入双端队列不感兴趣,我只对尽可能快地合并一个双端队列感兴趣。

最佳答案

不需要逐元素追加,你可以使用+=:

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])

关于python - 合并两个双端队列的最快方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53139531/

相关文章:

python - 软呢帽 "easy_install3": command not found

python - Pandas 使用列标题作为值将多个列转换/合并为单个列

C++ |使用 STL 双端队列和适配器设计实现堆栈

python - 格式化各行打印的输出?

python - 使用 queryset.extra() (django) 按评论数和日期对查询集进行排序和限制

git - 运行 Github Action 以使分支与主分支保持同步

python - 有没有不同的方法来检查 python 中的双端队列是否为空

c++ - "deque.h"中的 android-ndk-r7c 不合格 id 错误

Python Zeep - 多个 WSDL 文件

version-control - 如何中止 Mercurial 中的 merge ?