How to add arrays of tuples in Python?

0

I want to add two arrays, follow_dismiss and display that have only one value:

follow_dismiss
[(408L,), (14L,), (59L,), (6L,), (32L,), (62L,), (59L,), (120L,), (53L,), (150L,), (3L,), (1L,), (60L,), (1L,), (126L,), (41L,), (239L,), (163L,), (11L,), (42L,), (1L,), (2L,), (5L,), (1L,), (2L,), (4L,)]
display
[(382L,), (6L,), (295L,), (1L,), (8L,), (33L,), (30L,), (24L,), (5L,), (233L,), (1L,), (65L,), (21L,), (20L,), (86L,), (171L,), (2L,), (29L,), (2L,), (3L,), (1L,)]

They come from SQL queries, for example:

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

To get:

[790, 20, 9, ...]

Another example:

[(1,2), (3, 4)] + [(2, 3), (1, 3)] = [(3,5), (4, 7)]

Not even that works:

SUM_follow_dismiss_display_i = [x + y for x, y in zip(follow_dismiss, display)]

Because it gives:

[(408L, 381L), (14L, 6L), (59L, 294L), (6L, 1L), (32L, 8L), (62L, 33L), (59L, 30L), (120L, 24L), (53L, 5L), (150L, 233L), (3L, 1L), (1L, 65L), (60L, 21L), (1L, 20L), (126L, 86L), (41L, 171L), (239L, 2L), (163L, 29L), (11L, 2L), (42L, 3L), (1L, 1L)]

Not even that:

SUM_follow_dismiss_display_i = map(add,follow_dismiss,display)

What it gives:

SUM_follow_dismiss_display_i = map(add,follow_dismiss,display)
TypeError: can only concatenate tuple (not "NoneType") to tuple

He did it because I want to divide each square with its equivalent of the other array in a last array, result matrix. Without emargo map (truediv, arry1, arry2) it does not work because it tells me that:

print follow_dismiss
print SUM_follow_dismiss_display_i
m_i = map(truediv, SUM_follow_dismiss, SUM_follow_dismiss_display_i)

[(408L,), (14L,), (59L,), (6L,), (32L,), (62L,), (59L,), (120L,), (53L,), (150L,), (3L,), (1L,), (60L,), (1L,), (126L,), (41L,), (239L,), (163L,), (11L,), (42L,), (1L,), (2L,), (5L,), (1L,), (2L,), (4L,)]
[(408L, 381L), (14L, 6L), (59L, 294L), (6L, 1L), (32L, 8L), (62L, 33L), (59L, 30L), (120L, 24L), (53L, 5L), (150L, 233L), (3L, 1L), (1L, 65L), (60L, 21L), (1L, 20L), (126L, 86L), (41L, 171L), (239L, 2L), (163L, 29L), (11L, 2L), (42L, 3L), (1L, 1L)]
Traceback (most recent call last):
  File "./testPostreSQLPython.py", line 72, in <module>
    m_i = map(truediv, SUM_follow_dismiss, SUM_follow_dismiss_display_i)
TypeError: argument 2 to map() must support iteration
    
asked by ThePassenger 22.05.2017 в 12:31
source

2 answers

1

If you can use NumPy since your structure is basically an array, simply concatenate:

import numpy as np

a = np.array([(408,), (14,), (59,), (6,)])
b = np.array([(382,), (6,), (295,), (1,)])

c = a + b

The output is:

array([[790],
       [ 20],
       [354],
       [  7]])

Another option without using Numpy could be zip or itertools.izip in Python 2:

  • Python 2.x:

    from itertools import izip
    
    a = [(408,), (14,), (59,), (6,)]
    b = [(382,), (6,), (295,), (1,)]
    c = [[c+d for c, d in izip(row1, row2)] for row1, row2 in izip(a, b)]
    
  • Python 3.x:

    a = [(408,), (14,), (59,), (6,)]
    b = [(382,), (6,), (295,), (1,)]
    c = [[c+d for c, d in zip(row1, row2)] for row1, row2 in zip(a, b)]
    

In Python 2 it is also valid to use zip , the difference is that zip in Python 2 returns a list and not an iterator as it does in Python 3. In any case, the use of NumPy is the best option both in efficiency as in simplicity.

    
answered by 22.05.2017 / 12:57
source
0

Having two array of values, you can help with the zip function:

uno= [1,2,3,4,5]
dos = [6,7,8,9,10]

[x + y for x, y in zip(uno, dos )]

Result:

[7, 9, 11, 13, 15]
    
answered by 22.05.2017 в 12:52