获取valueerror 0的Python Web应用程序不在列表中

时间:2016-01-25 00:38:00

标签: python-3.x flask

@app.route('/')
def index():
    tpopDloads = popDloads
    tpopShipped = popShipped
    locPopDload = []
    locPopShipped = []
    popDinfo = []
     popSinfo = []
    popDloadsOrd = sorted(tpopDloads, reverse=True)
    popShippedOrd = sorted(tpopShipped, reverse=True)
    for i in range(3):
        locPopDload.append(tpopDloads.index(popDloadsOrd[i]))
        popDinfo.append(dProducts[locPopDload[i]])
        tpopDloads[tpopDloads.index(popDloadsOrd[i])] = -1 #Problem line#

    for i in range(3):
        locPopShipped.append(tpopShipped.index(popShippedOrd[i]))
        popSinfo.append(sProducts[locPopShipped[i]])
        tpopShipped[tpopShipped.index(popDloadsOrd[i])] = -1

    return render_template('index.html', popDinfo=popDinfo, popSinfo=popSinfo)

我得到的错误是:

  File "/var/lib/openshift/5697165a0c1e66eb870000fb/app-root/runtime /repo/flaskapp.py", line 47, in index
tpopShipped[tpopShipped.index(popDloadsOrd[i])] = -1
ValueError: 0 is not in list

这是使用两个变量popDloads和popShipped,它们都是包含一组整数的列表。我不明白为什么它没有工作,因为在订购列表后它试图找到一个数字的实际索引。它也适用于数字全为零的情况,只有在我更改了另一段代码中的数字后才会出现问题。

1 个答案:

答案 0 :(得分:0)

错误告诉您代码无法正常工作的原因。 0不在tpopShipped列表中。{/ 1>

>>> [1,2,3].index(0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: 0 is not in list