2016-11-09 41 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)]