How do I multiply lists together using a function?

前端 未结 6 679
孤城傲影
孤城傲影 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)]
    

提交回复
热议问题