2016-11-09 44 views
0

我得到的Python 2.7以下錯誤「需大於0值解壓」當我執行這些行:Python的元組循環值誤差

for row, row2 in results, results2: 
    row = list(row) 
    row2 = list(row2) 
    row2[7] += row[7] 

的目標是與value0添加value0的結果在results2中,然後result1中的值爲1,result2中的值爲1,...我使用psycopg2模塊的「fetchall()」函數。

有人可以幫我嗎?

非常感謝

回答

0

我不知道我理解你的問題,但如果你想從第一元組與來自第二元組中的第一個元素添加的第一要素,你可以很容易地做到這一點是這樣的。

>>> tup = [(1,1),(2,2),(3,3)] 
>>> tup1 = [(4,4),(5,5),(6,6)] 
>>> tup 
[(1, 1), (2, 2), (3, 3)] 
>>> tup1 
[(4, 4), (5, 5), (6, 6)] 
>>> x1 = [x[0] for x in tup] 
>>> x2 = [x[0] for x in tup1] 
>>> x1 
[1, 2, 3] 
>>> x2 
[4, 5, 6] 
>>> list(zip(x1,x2)) #if you want to create another tuple 
[(1, 4), (2, 5), (3, 6)] 
>>> x1.extend(x2) #if you want to make a list 
>>> x1 
[1, 2, 3, 4, 5, 6] 

在這種情況下,元組的長度並不重要。

0

我會使用地圖和拉鍊做這項工作:

from operator import add 

list1 = [(1,2), (3,4), (5,6)] 
list2 = [(7,8), (9,10), (11,12)] 
list3 = [map(add, row1, row2) for row1 row2 in zip(list1, list2)]