Python How to pair two list by lambda and map

后端 未结 5 1412
北海茫月
北海茫月 2021-01-17 13:56

For example, I have following two lists

listA=[\'one\', \'two\' , \'three\'] listB=[\'apple\',\'cherry\',\'watermelon\']

How can I pair those two lists to

5条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-17 14:32

    The easiest solution would be to simply use zip as in:

    >>> listA=['one', 'two' , 'three']
    >>> listB=['apple','cherry','watermelon']
    >>> list(zip(listA, listB))
    [('one', 'apple'), ('two', 'cherry'), ('three', 'watermelon')]
    

    I guess it would be possible to use map and lambdas, but that would just needlessly complicate things as this is really the ideal case for zip.

提交回复
热议问题