我正在尝试在 python 中使用 deque 来创建队列。
我不断收到的错误是索引超出范围
perf_his[b][c] = 0
IndexError:双端队列索引超出范围
这是我实现的代码的一个小原型。
import collections
apps = [1,2,3]
num_thrs = len(apps)
perf_his = []
for a in range(num_thrs):
perf_his += [collections.deque(maxlen=1)]
for b in range(num_thrs):
for c in range(0, 1):
perf_his[b][c] = 0
为了检查我是否正确理解了双端队列,我实现了以下代码:
#!/usr/bin/env python
from collections import deque
something = ["foo","bar","baz"]
output = []
diff = 0
d = deque()
for i in something:
d.append(i)
print("-> %s" % i)
for i in xrange(len(d)):
print(d[i])
output.append(d[i])
for i in xrange(len(something)):
if output[i] != something[i]:
diff += 1
print(something,output,diff)
我一直在尝试在 2 天内修复该错误,但我似乎不明白这个问题。有人可以阐明一下吗?