How to concatenate element-wise two lists in Python?

后端 未结 8 2509
春和景丽
春和景丽 2020-11-29 02:24

I have two lists and I want to concatenate them element-wise. One of the list is subjected to string-formatting before concatenation.

For example :

         


        
8条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-29 02:44

    Than can be done elegantly with map and zip:

    map(lambda (x,y): x+y, zip(list1, list2))
    

    Example:

    In [1]: map(lambda (x,y): x+y, zip([1,2,3,4],[4,5,6,7]))
    Out[1]: [5, 7, 9, 11]
    

提交回复
热议问题