当不应该在 for 循环中时出现值错误?

Value error when there shouldn't be in for loop?

这是我的代码:

    while counter <= len(titles):
        currenttime = [perc[counter], fails[counter], titles[counter]]
        print(currenttime)
        for percent, fail, title in currenttime:

当我 运行 它时,我得到一个值错误显示

ValueError: not enough values to unpack (expected 3, got 2)

但是当我打印当前时间时,我得到

['67', '1', 'subsection']

对我来说,这看起来像3个值,但显然我错了,谁能告诉我吗?我环顾四周,但没有找到羊角号的好答案。 任何帮助将不胜感激。谢谢

代码上下文:

      n = 0
    perc = list()
    while n < len(piedata):
        perc.append(piedata[n+2])
        n += 3
    print (perc)

    n = 0
    fails = list()
    while n < len(piedata):
        fails.append(piedata[n+1])
        n += 3
    print(fails)

    n = 0
    titles = list()
    while n < len(piedata):
        titles.append(piedata[n])
        n += 3
    print(titles)

    counter = 0
    while counter <= len(titles):
        currenttime = [perc[counter], fails[counter], titles[counter]]
        print(currenttime)
        for percent, fail, title in currenttime:
            piedata = [percent, (100-percent)]

            fig = matplotlib.figure.Figure(figsize=(5, 5))
            ax = fig.add_subplot(111)
            ax.pie(piedata)  # this is the information that the circle diagram will be made out of
            ax.legend([('amount of attempts:', NOTT), ('amount of fails', fail)])

            circle = matplotlib.patches.Circle((0, 0), 0.7, color='white')
            ax.add_artist(circle)


            # this is the code for actually putting the circle diagram/pie chart on the screen
            canvas = FigureCanvasTkAgg(fig, master=window)
            canvas.get_tk_widget().pack()
            canvas.draw()

            Label(window, text=(title, title), bg='light blue').pack()
            counter += 1

            window.mainloop()
            print(percent)
            print(fail)

声明:

for percent, fail, title in currenttime:

表示将currenttime列表中的每一项解包为一个序列,而currenttime列表中的每一项只是一个字符串,解包为字符,其中第一项有只有两个,导致 "not enough values to unpack (expected 3, got 2)" 错误。

为了您的目的,您应该简单地压缩 3 个列表并迭代 zip 生成器,而不是带有计数器和内部 for 循环的 while 循环:

for percent, fail, title in zip(perc, fails, titles):
    piedata = [percent, (100 - percent)]

    fig = matplotlib.figure.Figure(figsize=(5, 5))
    ax = fig.add_subplot(111)
    ax.pie(piedata)  # this is the information that the circle diagram will be made out of
    ax.legend([('amount of attempts:', NOTT), ('amount of fails', fail)])

    circle = matplotlib.patches.Circle((0, 0), 0.7, color='white')
    ax.add_artist(circle)

    # this is the code for actually putting the circle diagram/pie chart on the screen
    canvas = FigureCanvasTkAgg(fig, master=window)
    canvas.get_tk_widget().pack()
    canvas.draw()

    Label(window, text=(title, title), bg='light blue').pack()

    window.mainloop()
    print(percent)
    print(fail)

以下几行:

for percent, fail, title in currenttime:

暗示 currenttime 是一个元组列表,当它在您的示例中是一个列表时,它转换为:

for (percent, fail, title) in currenttime:

如果您想获得 currenttime 的 3 个元素,您应该怎么做:

percent = currenttime[0] 
fail = currenttime[1] 
title = currenttime[2]

或使currenttime成为一个元组:

currenttime = (perc[counter], fails[counter], titles[counter])
percent, fail, title = currenttime

for 命令的源必须是可迭代的。你的是一个迭代器,每次迭代 returns 一个字符串。第一个元素returns"67",只有两个元素需要解包。

对于您想要的功能,currentime 每个 元素必须是三元组。例如:

currenttime = [
    ['67', '1', 'subsection'],
    ['18', '5', 'main branch'],
    ...
]

这种情况下,每次迭代都会产生三个要解包的值。