How do I multiply lists together using a function?

前端 未结 6 672
孤城傲影
孤城傲影 2020-12-07 05:01

how do I multiply lists together in python using a function? This is what I have:

    list = [1, 2, 3, 4]
    def list_multiplication(list, value):
                  


        
相关标签:
6条回答
  • 2020-12-07 05:07

    If you have two lists A and B of the same length, easiest is to zip them:

    >>> A = [1, 2, 3, 4]
    >>> B = [5, 6, 7, 8]
    >>> [a*b for a, b in zip(A, B)]
    [5, 12, 21, 32]
    

    Take a look at zip on its own to understand how that works:

    >>> zip(A, B)
    [(1, 5), (2, 6), (3, 7), (4, 8)]
    
    0 讨论(0)
  • 2020-12-07 05:08

    zip() would do:

    [a*b for a,b in zip(lista,listb)]
    
    0 讨论(0)
  • 2020-12-07 05:09

    Use numpy for this.

    >>> import numpy as np
    >>> list = [1,2,3,4]
    >>> np.multiply(list, list)
    array([ 1,  4,  9, 16])
    

    If you prefer python lists:

    >>> np.multiply(list, list).tolist()
    [1, 4, 9, 16]
    

    additionally, this also works for element-wise multiplication with a scalar.

    >>> np.multiply(list, 2)
    array([2, 4, 6, 8])
    
    0 讨论(0)
  • 2020-12-07 05:12

    My favorite way is mapping the mul operator over the two lists:

    from operator import mul
    
    mul(2, 5)
    #>>> 10
    
    mul(3, 6)
    #>>> 18
    
    map(mul, [1, 2, 3, 4, 5], [6, 7, 8, 9, 10])
    #>>> <map object at 0x7fc424916f50>
    

    map, at least in Python 3, returns a generator. Hence if you want a list you should cast it to one:

    list(map(mul, [1, 2, 3, 4, 5], [6, 7, 8, 9, 10]))
    #>>> [6, 14, 24, 36, 50]
    

    But by then it might make more sense to use a list comprehension over the zip'd lists.

    [a*b for a, b in zip([1, 2, 3, 4, 5], [6, 7, 8, 9, 10])]
    #>>> [6, 14, 24, 36, 50]
    

    To explain the last one, zip([a,b,c], [x,y,z]) gives (a generator that generates) [(a,x),(b,y),(c,z)].

    The for a, b in "unpacks" each (m,n) pair into the variables a and b, and a*b multiplies them.

    0 讨论(0)
  • 2020-12-07 05:24

    zip is probably the way to go, as suggested by the other answers. That said, here's an alternative beginner approach.

    # create data
    size = 20
    a = [i+1 for i in range(size)]
    b = [val*2 for val in a]
    
    a
    >> [ 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20] 
    b
    >> [ 2  4  6  8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40] 
    
    def multiply_list_elems(list_one, list_two):
        """ non-efficient method """
        res = [] # initialize empty list to append results
        if len(a) == len(b): # check that both lists have same number of elems (and idxs)
            print("\n list one: \n", list_one, "\n list two: \n", list_two, "\n")
            for idx in range(len(a)): # for each chronological element of a
                res.append(a[idx] * b[idx]) # multiply the ith a and ith b for all i
        return res
    
    def efficient_multiplier(list_one, list_two):
        """ efficient method """
        return [a[idx] * b[idx] for idx in range(len(a)) if len(a) == len(b)]
    
    print(multiply_list_elems(a, b))
    print(efficient_multiplier(a, b))
    

    both give:

    >> [2, 8, 18, 32, 50, 72, 98, 128, 162, 200, 242, 288, 338, 392, 450, 512, 578, 648, 722, 800]
    

    Yet another approach is using numpy, as suggested here.

    0 讨论(0)
  • 2020-12-07 05:30

    You can use a list comprehension:

    >>> t = [1, 2, 3, 4]
    >>> [i**2 for i in t]
    [1, 4, 9, 16]
    

    Note that 1*1, 2*2, etc is the same as squaring the number.


    If you need to multiply two lists, consider zip():

    >>> L1 = [1, 2, 3, 4]
    >>> L2 = [1, 2, 3, 4]
    >>> [i*j for i, j in zip(L1, L2)]
    [1, 4, 9, 16]
    
    0 讨论(0)
提交回复
热议问题