简体   繁体   English

带有列表的Python Deque appendleft

[英]Python Deque appendleft with list

I am currently creating my deque object using the following, 我目前正在使用以下方法创建我的deque对象,

self.CommandList = deque((['S', False, 60],['c'],['g16'],['i50'],['r30', True],['u320'],['o5000'],['b1'],['B4500'],['W1'],['l5154'],['!10'],['p2', True, 10],['e1'],['K20'],['U0'],['Y0']))

But I wish to add a similar list to the queue later but using appendleft , so it can jump ahead of the list. 但我希望稍后在队列中添加类似的列表,但是使用appendleft ,因此它可以跳到列表之前。 I assumed the following, but had no luck. 我假设如下,但没有运气。

NewList = (['S'],['c'],['!10'],['p4'],['o1200'],['i50'],['r30'],['b10'],['d1'],['A', True, 163])
self.CommandList.appendleft(NewList)

Is this even possible with appendleft ? 这有可能与appendleft

I think you want .extendleft here. 我想你想要.extendleft This will "extend the list" instead of just appending the list as one element. 这将“扩展列表”而不是仅将列表作为一个元素附加。

z = collections.deque([1,2,3,4])   # [1, 2, 3, 4]

z.appendleft(['bad', 'news'])   # [ ['bad', 'news'], 1, 2, 3, 4 ]
z.extendleft(['good', 'news'])  # [ 'good', 'news', ['bad', 'news'], 1, 2, 3, 4 ]

If they are getting inserted in reverse, the quick fix is to just reverse the list: 如果它们反向插入,快速修复就是反转列表:

z.extendleft(reversed(['good', 'news']))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM