Add two arrays without using for. Python

前端 未结 4 1071
后悔当初
后悔当初 2021-01-17 06:26

I have these two arrays:

import numpy as np
a = np.array([0, 10, 20])
b = np.array([20, 30, 40, 50])  

I´d like to add both in the followin

相关标签:
4条回答
  • 2021-01-17 07:07

    One possible and only memory not time intensive solution is using np.repeat and np.resize to repeat and resize the arrays a and b to the size of the resulting shape first and then simply add those two arrays.

    Code:

    import numpy as np
    a = np.array([0, 10, 20])
    b = np.array([20, 30, 40, 50])
    
    def extend_and_add(a, b):
        return np.repeat(a, len(b)) + np.resize(b, len(a)*len(b))
    

    So extend_and_add(a, b) returns:

    extend_and_add(a, b)
    > array([20, 30, 40, 50, 30, 40, 50, 60, 40, 50, 60, 70])
    

    Explanation:

    Basically np.repeat(a, len(b)) repeats:

    a
    > array([ 0, 10, 20])
    

    to

    np.repeat(a, len(b))
    > array([ 0,  0,  0,  0, 10, 10, 10, 10, 20, 20, 20, 20])
    

    after this you need the second array resized with np.resize:

    b
    > array([20, 30, 40, 50])
    

    is resized to:

    np.resize(b, len(a)*len(b))
    > array([20, 30, 40, 50, 20, 30, 40, 50, 20, 30, 40, 50])
    

    Now we can simply add the arrays:

    array([ 0,  0,  0,  0, 10, 10, 10, 10, 20, 20, 20, 20])
    +
    array([20, 30, 40, 50, 20, 30, 40, 50, 20, 30, 40, 50])
    

    returns:

    array([20, 30, 40, 50, 30, 40, 50, 60, 40, 50, 60, 70])
    
    0 讨论(0)
  • 2021-01-17 07:19

    Here you go:

    In [17]: a = np.array([0, 10, 20])
    
    In [18]: b = np.array([20, 30, 40, 50])  
    
    In [19]: (a.reshape(-1, 1) + b).ravel()
    Out[19]: array([20, 30, 40, 50, 30, 40, 50, 60, 40, 50, 60, 70])
    

    Here are the details.

    a.reshape(-1, 1) converts a to an array with shape (3, 1):

    In [20]: a.reshape(-1, 1)
    Out[20]: 
    array([[ 0],
           [10],
           [20]])
    

    When b is added to that, broadcasting applies, which in effect does an "outer sum" (i.e. adds all the pairwise combinations), forming an array with shape (3, 4):

    In [21]: a.reshape(-1, 1) + b
    Out[21]: 
    array([[20, 30, 40, 50],
           [30, 40, 50, 60],
           [40, 50, 60, 70]])
    

    The ravel() method flattens the result into a one-dimensional array:

    In [22]: (a.reshape(-1, 1) + b).ravel()
    Out[22]: array([20, 30, 40, 50, 30, 40, 50, 60, 40, 50, 60, 70])
    

    See @HYRY's answer for an even more concise version.

    0 讨论(0)
  • 2021-01-17 07:27

    you can use outer method of ufunc:

    np.add.outer(a, b).ravel()
    
    0 讨论(0)
  • 2021-01-17 07:28

    First you need to specify the array type, if you use a constructor like that. For instance for integers, use that:

    a = array("i",[0, 10, 20])       # signed int type
    b = array("i",[20, 30, 40, 50]) 
    

    Them you might want to use while loops with counters, it is more complex than for, but avoids the for loop.

    from array import array
    
    a = array("i",[0, 10, 20])  # signed int
    
    b = array("i",[20, 30, 40, 50]) 
    
    c = array("i",[])
    
    count1 = 0
    
    while count1 < len(a):
       count2 = 0
       while count2 < len(b):
           c.append(a[count1]+b[count2])
           count2 += 1
       count1 += 1
    
    print(c)
    
    0 讨论(0)
提交回复
热议问题