Element-wise addition of 2 lists?

后端 未结 16 1200
感动是毒
感动是毒 2020-11-22 07:48

I have now:

list1 = [1, 2, 3]
list2 = [4, 5, 6]

I wish to have:

[1, 2, 3]
 +  +  +         


        
相关标签:
16条回答
  • 2020-11-22 08:22

    It's simpler to use numpy from my opinion:

    import numpy as np
    list1=[1,2,3]
    list2=[4,5,6]
    np.add(list1,list2)
    

    Results:

    For detailed parameter information, check here: numpy.add

    0 讨论(0)
  • 2020-11-22 08:22

    Perhaps this is pythonic and slightly useful if you have an unknown number of lists, and without importing anything.

    As long as the lists are of the same length, you can use the below function.

    Here the *args accepts a variable number of list arguments (but only sums the same number of elements in each).

    The * is used again in the returned list to unpack the elements in each of the lists.

    def sum_lists(*args):
        return list(map(sum, zip(*args)))
    
    a = [1,2,3]
    b = [1,2,3]  
    
    sum_lists(a,b)
    

    Output:

    [2, 4, 6]
    

    Or with 3 lists

    sum_lists([5,5,5,5,5], [10,10,10,10,10], [4,4,4,4,4])
    

    Output:

    [19, 19, 19, 19, 19]
    
    0 讨论(0)
  • 2020-11-22 08:22

    Although, the actual question does not want to iterate over the list to generate the result, but all the solutions that has been proposed does exactly that under-neath the hood!

    To refresh: You cannot add two vectors without looking into all the vector elements. So, the algorithmic complexity of most of these solutions are Big-O(n). Where n is the dimension of the vector.

    So, from an algorithmic point of view, using a for loop to iteratively generate the resulting list is logical and pythonic too. However, in addition, this method does not have the overhead of calling or importing any additional library.

    # Assumption: The lists are of equal length.
    resultList = [list1[i] + list2[i] for i in range(len(list1))]
    

    The timings that are being showed/discussed here are system and implementation dependent, and cannot be reliable measure to measure the efficiency of the operation. In any case, the big O complexity of the vector addition operation is linear, meaning O(n).

    0 讨论(0)
  • 2020-11-22 08:26
    [list1[i] + list2[i] for i in range(len(list1))]
    
    0 讨论(0)
  • 2020-11-22 08:30

    If you need to handle lists of different sizes, worry not! The wonderful itertools module has you covered:

    >>> from itertools import zip_longest
    >>> list1 = [1,2,1]
    >>> list2 = [2,1,2,3]
    >>> [sum(x) for x in zip_longest(list1, list2, fillvalue=0)]
    [3, 3, 3, 3]
    >>>
    

    In Python 2, zip_longest is called izip_longest.

    See also this relevant answer and comment on another question.

    0 讨论(0)
  • 2020-11-22 08:32

    Use map with operator.add:

    >>> from operator import add
    >>> list( map(add, list1, list2) )
    [5, 7, 9]
    

    or zip with a list comprehension:

    >>> [sum(x) for x in zip(list1, list2)]
    [5, 7, 9]
    

    Timing comparisons:

    >>> list2 = [4, 5, 6]*10**5
    >>> list1 = [1, 2, 3]*10**5
    >>> %timeit from operator import add;map(add, list1, list2)
    10 loops, best of 3: 44.6 ms per loop
    >>> %timeit from itertools import izip; [a + b for a, b in izip(list1, list2)]
    10 loops, best of 3: 71 ms per loop
    >>> %timeit [a + b for a, b in zip(list1, list2)]
    10 loops, best of 3: 112 ms per loop
    >>> %timeit from itertools import izip;[sum(x) for x in izip(list1, list2)]
    1 loops, best of 3: 139 ms per loop
    >>> %timeit [sum(x) for x in zip(list1, list2)]
    1 loops, best of 3: 177 ms per loop
    
    0 讨论(0)
提交回复
热议问题