Fastest way to merge two deques

冷暖自知 提交于 2020-05-15 02:49:22

问题


Exist a faster way to merge two deques than this?

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

Note that I'm not interested in preserving input deques, I'm only interested in having the merged one as fast as possible.


回答1:


There's no need for elementwise appending, you can just use +=:

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


来源:https://stackoverflow.com/questions/53139531/fastest-way-to-merge-two-deques

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!