【问题标题】:Value error when there shouldn't be in for loop?不应该出现在 for 循环中的值错误?
【发布时间】:2019-09-03 17:14:57
【问题描述】:

这是我的代码:

    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


    【解决方案1】:

    声明:

    for percent, fail, title in currenttime:
    

    意思是将currenttime列表中的每一项解包为一个序列,而currenttime列表中的每一项只是一个字符串,解包成字符,其中第一项只有两个,导致“没有足够的值来解包(预期 3,得到 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)
    

    【讨论】:

    • 啊,非常感谢您解释字符串的事情,我不知道。不幸的是,它确实需要是一个元组列表,因为其余代码的性质,你知道我可以得到这个的方法,并拥有它们当前的值。 EG:将每个转换为不同的类型?
    • 另外,已将代码的上下文添加到主 Q
    • 很高兴能提供帮助。现在我看到了您发布的代码的上下文,它重申了我的信念,即您根本不需要currenttime。请尝试我使用zip 提供的解决方案。它取代了您的 while 循环,因此您根本不需要内部 for 循环。
    • 不认为您有任何使用 matplotlib 和 tkinter 的经验吗?我希望它制作 2 个圆形图,但从这里开始,它只制作一个
    • 抱歉,我没有。如果您将其作为一个新问题发布,您将获得更好的答案,以便具有正确知识的人能够提供更多帮助。
    【解决方案2】:

    以下几行:

    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
    

    【讨论】:

      【解决方案3】:

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

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

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

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

      【讨论】:

      • 啊,我明白了,谢谢
      猜你喜欢
      • 2021-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多