2011-04-01 82 views
13

我得到錯誤:ValueError:需要超過2個值來解壓 當我現在運行單元測試,所以2個故障和一個跳過 現在遠正如我已經讀到ValueError:需要超過2個值來解壓Python 2.6.6

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

線源142是具有的代碼行的方法

 
for test, err, capt in errors: 

 
count = 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 = fix(1,2) 
d,e,f = fix(1,2,3,4) 

但是,該功能將不知道左側 序列的長度,所以它將不得不作爲額外參數傳入或硬編碼。

所以

 
count = get_error_count(i) 
uses only one variable, where as 
def get_error_count(lookup, index): 
takes on 2 

我應該作爲第二個變量有什麼用?解決這個問題?

謝謝, -Kamal。

-------------------- >> begin capture stdout < < ------------------- -

\ test_many_errors.test_assert_one ... FAIL test_many_errors.test_one ... OK test_many_errors.test_assert_two ...錯誤 test_many_errors.test_two ... OK test_many_errors.test_value_one ...錯誤 test_many_errors。 test_value_two ...跳過:(,ValueError(),) test_many_errors.test_good_one ... ok test_many_errors.test_good_two ... ok

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 

/

--------------------- >>結束時捕獲的stdout < < ----------- -----------


ran 3 test in 1。263S

回答

19

而是在你的任務拆包:

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] 
1

所以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 
1

你可以寫一個實用功能,使您的結果一致:

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) 
1

我建議補充名單到需要的長度與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] 
相關問題