不应该在for循环中的值错误?

问题描述 投票:1回答:3

这是我的代码:

    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)
python for-loop valueerror
3个回答
2
投票

该声明:

for percent, fail, title in currenttime:

意味着将currenttime列表中的每个项目解压缩为一个序列,然而currenttime列表中的每个项目只是一个字符串,它解包为字符,其中第一个项目只有两个,导致“没有足够的值来解包(预期3,得到2)“错误。

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

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)

0
投票

以下行:

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

0
投票

for命令的源必须是可迭代的。你的是一个iterable,它在每次迭代时返回一个字符串。第一个元素返回"67",它只有两个要解包的元素。

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.