【问题标题】:ValueError: need more than 2 values to unpack in Python 2.6.6ValueError:需要超过 2 个值才能在 Python 2.6.6 中解包
【发布时间】:2011-04-01 15:58:33
【问题描述】:

我收到错误:ValueError:需要超过 2 个值才能解压 当我现在运行单元测试时,有 2 次失败和 1 次跳过 现在据我所知

lambda i: get_error_count(self._error_lookup, i))

源码第142行是方法

对于测试,错误,错误捕获:

其中有一行代码:

计数 = get_error_count(i)

参考 Python 3.0 有点像这样。可以绑定多余的值 (作为列表)到最后一个变量:

a,b,*c = [1,2,3,4,5]

将导致 c 包含 [3,4,5]。

在 Python 2.x 中,你不能直接这样做,但你应该可以 创建一个函数来延长或缩短参数的输入元组 到正确的长度,以便您可以这样做:

a,c,b = 修复(1,2) d,e,f = 修复(1,2,3,4)

但是,该函数不知道左侧的长度 序列,因此它必须作为额外参数或硬参数传入 编码。

所以

计数 = get_error_count(i) 仅使用一个变量,其中 def get_error_count(查找,索引): 承担 2

我应该使用什么作为第二个变量?解决这个问题?

谢谢, -卡马尔。

-------------------- >> 开始捕获标准输出

\ test_many_errors.test_assert_one ...失败 test_many_errors.test_one ...好的 test_many_errors.test_assert_two ... 错误 test_many_errors.test_two ...好的 test_many_errors.test_value_one ...错误 test_many_errors.test_value_two ...跳过:(,ValueError(),) test_many_errors.test_good_one ...好的 test_many_errors.test_good_two ...好的

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/Current/bin/nosetests", line 10, in <module>
    sys.exit(run_exit())
  File "/Library/Frameworks/Python.framework/Versions/6.3/lib/python2.6/site-packages/nose/core.py", line 117, in __init__
    **extra_args)
  File "/Library/Frameworks/Python.framework/Versions/6.3/lib/python2.6/unittest.py", line 817, in __init__
    self.runTests()
  File "/Library/Frameworks/Python.framework/Versions/6.3/lib/python2.6/site-packages/nose/core.py", line 196, in runTests
    result = self.testRunner.run(self.test)
  File "/Library/Frameworks/Python.framework/Versions/6.3/lib/python2.6/site-packages/nose/core.py", line 63, in run
    result.printErrors()
  File "/NOSE_TRIM/nosetrim-read-only/nosetrim/nosetrim.py", line 136, in printErrors
    lambda i: get_error_count(self._error_lookup, i))
  File "/NOSE_TRIM/nosetrim-read-only/nosetrim/nosetrim.py", line 142, in printErrorList
    for test, err, capt in errors:
ValueError: need more than 2 values to unpack

/

--------------------- >> 结束捕获的标准输出


在 1.263 秒内运行 3 次测试

【问题讨论】:

    标签: python


    【解决方案1】:

    而不是在你的作业中拆包:

    a, b, c = do_something()
    

    尝试将结果分配给单个变量并测试其长度:

    t = do_something()
    # t is now a tuple (or list, or whatever was returned) of results
    if len(t) > 2:
        # Can use the third result!
        c = t[2]
    

    【讨论】:

      【解决方案2】:

      所以errors 是一个列表,其中包含长度为 2 或 3 的元组的项目。您需要一种在 for 循环中解压缩不同长度的元组的方法。正如您所指出的,在 Python2 中没有干净的方法可以做到这一点。与其想出一个聪明的方法来实现这种行为,我建议确保你的错误列表总是包含长度为 3 的元组。这可以在你每次向errors 添加项目时完成,或者在事后,像这样:

      errors = [(x[0], x[1], x[2]) if len(x) == 3 else (x[0], x[1], None) for x in errors]
      

      或者你可以制作一个生成器(这违背了我没有找到实现这种行为的聪明方法的建议):

      def widen_tuples(iter, width, default=None):
          for item in iter:
              if len(item) < width:
                  item = list(item)
                  while len(item) < width:
                      item.append(default)
                  item = tuple(item)
              yield item
      

      像这样使用它:

      >>> errors = [(1, 2), (1, 2, 3)] 
      >>> for a, b, c in widen_tuples(errors, 3):
      ...     print a, b, c
      1 2 None
      1 2 3
      

      【讨论】:

        【解决方案3】:

        你可以编写一个实用函数来使你的结果统一:

        def do_something2():
            return 1, 2
        
        def do_something3():
            return 1, 2, 3
        
        def do_something5():
            return 1, 2, 3, 4, 5
        
        def uniform_result(*args):
            return args[0], args[1], args[2:]
        
        a, b, c =  uniform_result(*do_something2())
        print a, b, c
        # 1 2 ()
        
        a, b, c =  uniform_result(*do_something3())
        print a, b, c
        # 1 2 (3,)
        
        a, b, c =  uniform_result(*do_something5())
        print a, b, c
        # 1 2 (3, 4, 5)
        

        【讨论】:

          【解决方案4】:

          我建议使用None 元素将列表补充到必要的长度:

          data = give_me_list()
          (val1, val2, val3) = data + (3 - len(data)) * [None]
          

          3 是左侧值的数量。如果列表可能包含过多元素,请使用保护措施:

          data = give_me_list()[:3]
          (val1, val2, val3) = data + (3 - len(data)) * [None]
          

          【讨论】:

            猜你喜欢
            • 2020-07-09
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-02-07
            • 2019-06-23
            • 2011-05-29
            相关资源
            最近更新 更多