Skip to main content
3 de 5
se añadieron 756 caracteres en el cuerpo

¿Como sumar arrays de tuplas en Python en respecto a index?

Quiero sumar dos arrays , follow_dismiss y displayde tamaño differente que vienene de consultas SQL que tienen un valor y un index sobre los index que vienene de una consulta sql :

cur.execute("""-- nombre de fois ou l'utilisateur fait ce qu'on attend de lui par utilisateur
SELECT COUNT (swipe.state),swipe.subscriber_id FROM swipe
  WHERE swipe.state= 2 OR swipe.state = 3 or swipe.state=5
  OR swipe.state =6 or swipe.state=8 or swipe.state=9
    GROUP BY swipe.subscriber_id
      ORDER BY swipe.subscriber_id;""")
follow_dismiss = cur.fetchall()
print "follow_dismiss"

y

    cur.execute("""-- nombre de fois ou l'on propose une pub a l'utilisateur par utilisateur
SELECT COUNT (swipe.state),swipe.subscriber_id FROM swipe
  WHERE swipe.state= 1 OR swipe.state = 4 or swipe.state=7
    GROUP BY swipe.subscriber_id
      ORDER BY swipe.subscriber_id;""")

dan :

follow_dismiss
[(409L, 13), (14L, 54), (59L, 68), (6L, 150), (32L, 152), (62L, 156), (59L, 158), (120L, 160), (53L, 161), (150L, 162), (3L, 169), (1L, 171), (60L, 172), (1L, 177), (126L, 179), (41L, 185), (239L, 189), (163L, 190), (11L, 216), (42L, 223), (1L, 272), (2L, 286), (5L, 289), (1L, 292), (2L, 294), (4L, 296)]
display
[(384L, 13), (6L, 54), (300L, 68), (1L, 150), (8L, 152), (33L, 156), (30L, 158), (24L, 160), (5L, 161), (233L, 162), (1L, 171), (65L, 172), (21L, 179), (20L, 185), (86L, 189), (171L, 190), (2L, 216), (29L, 223), (2L, 276), (3L, 286), (1L, 296)]

Quiero obtener :

[(793,13), (20,54),...]

A qui esta lo que he intentado :

follow_dismiss=[(409L, 13), (14L, 54), (59L, 68), (6L, 150), (32L, 152), (62L, 156), (59L, 158), (120L, 160), (53L, 161), (150L, 162), (3L, 169), (1L, 171), (60L, 172), (1L, 177), (126L, 179), (41L, 185), (239L, 189), (163L, 190), (11L, 216), (42L, 223), (1L, 272), (2L, 286), (5L, 289), (1L, 292), (2L, 294), (4L, 296)]
display=[(384L, 13), (6L, 54), (300L, 68), (1L, 150), (8L, 152), (33L, 156), (30L, 158), (24L, 160), (5L, 161), (233L, 162), (1L, 171), (65L, 172), (21L, 179), (20L, 185), (86L, 189), (171L, 190), (2L, 216), (29L, 223), (2L, 276), (3L, 286), (1L, 296)]

def column_sums(a, b):
    result = []
    for i in range(len(a)):
        for j in range(len(b)):  
        if a[i][2]=b[j][2]
            result[i][1] =a[i][1]+b[j][1] 
        else if a[i][2]<b[j][2]
            result[i][1]=a[i][1]
            result[i][2]= a[j][2]
            j = j-1 # para quedar
        else if a[i][2]>b[j][2]
            result[i][1]= b[j][1]
            result[i][2]= b[j][2]
            i = i-1 # para quedar

a = colums_sums(follow_dismiss,display)
print a 

Pero me dice :

./testPython.py: line 1: syntax error near unexpected token `('
./testPython.py: line 1: `follow_dismiss=[(409L, 13), (14L, 54), (59L, 68), (6L, 150), (32L, 152), (62L, 156), (59L, 158), (120L, 160), (53L, 161), (150L, 162), (3L, 169), (1L, 171), (60L, 172), (1L, 177), (126L, 179), (41L, 185), (239L, 189), (163L, 190), (11L, 216), (42L, 223), (1L, 272), (2L, 286), (5L, 289), (1L, 292), (2L, 294), (4L, 296)]'