【问题标题】:Python web app getting valueerror 0 not in listPython Web 应用程序获取 valueerror 0 不在列表中
【发布时间】:2016-01-25 00:38:00
【问题描述】:
@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,它们都是包含一组整数的列表。我不明白为什么它不起作用,因为它试图在列表排序后查找数字的实际索引。它也适用于数字全为零的情况,只有在我更改另一段代码中的数字后才会遇到问题。

【问题讨论】:

  • 您的问题行不在您指出的位置:错误显示tpopShipped...,在您评论的行下方几行。请更仔细地阅读,并且(可能)更仔细地复制粘贴。您可能希望将 actual 错误行中的 popDloadsOrd[i] 更改为 popShippedOrd[i]

标签: python-3.x flask


【解决方案1】:

错误告诉您为什么您的代码无法正常工作。 0 不在列表中 tpopShipped

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-23
    • 2018-08-04
    • 1970-01-01
    • 2011-07-29
    • 1970-01-01
    • 2021-12-15
    • 1970-01-01
    • 2019-07-16
    相关资源
    最近更新 更多