Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将元素从列表添加到队列中_Python_List_Deque - Fatal编程技术网

Python 将元素从列表添加到队列中

Python 将元素从列表添加到队列中,python,list,deque,Python,List,Deque,是否要将列表中的元素添加到deque列表中,然后使用队列并弹出其中的元素?假设我有清单: a = [4,-1,4,1,1] 我想按顺序添加它。首先将-1添加到队列中,然后通过其他方式运行它,弹出-1,然后添加1s运行它,然后弹出其中的元素,然后4s运行队列,然后弹出其中的元素 现在还不清楚你到底想做什么。您应该包含更多您尝试实现的代码 您可以对列表进行反向排序,并将其增量添加到deque from collections import deque a = [4, -1, 4, 1, 1] s

是否要将列表中的元素添加到deque列表中,然后使用队列并弹出其中的元素?假设我有清单:

a = [4,-1,4,1,1]

我想按顺序添加它。首先将-1添加到队列中,然后通过其他方式运行它,弹出-1,然后添加1s运行它,然后弹出其中的元素,然后4s运行队列,然后弹出其中的元素

现在还不清楚你到底想做什么。您应该包含更多您尝试实现的代码

您可以对
列表进行反向排序
,并将其增量添加到
deque

from collections import deque
a = [4, -1, 4, 1, 1]

sorted_a = sorted(a, reverse=True)

a_deque = deque()
a_deque.append(sorted_a.pop())
如果您想以上述方式使用
deque
,这可能就是您想要的:

# Reverse sorts a list so that calling pop()
# pop's elements "in order".
a_sorted_list = sorted(a, reverse=True)
a_reverse_sorted_deque = deque(a_sorted_list)

a_reverse_sorted_deque.pop()

# A regularly sorted list would require you to
# popleft
a_sorted_deque = deque(sorted(a))
a_sorted_deque.popleft()

从集合导入数据;b=deque(a)