Element-wise addition of 2 lists?

后端 未结 16 1419
感动是毒
感动是毒 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:42

    This is simple with numpy.add()

    import numpy
    
    list1 = numpy.array([1, 2, 3])
    list2 = numpy.array([4, 5, 6])
    result = numpy.add(list1, list2) # result receive element-wise addition of list1 and list2
    print(result)
    array([5, 7, 9])
    

    See doc here

    If you want to receiver a python list:

    result.tolist()
    

提交回复
热议问题