Using list comprehension in Python to do something similar to zip()?

后端 未结 5 1231
暖寄归人
暖寄归人 2021-02-04 14:05

I\'m a Python newbie and one of the things I am trying to do is wrap my head around list comprehension. I can see that it\'s a pretty powerful feature that\'s worth learning.

5条回答
  •  感情败类
    2021-02-04 14:29

    Something like this:

    [[c, a] for c, a in zip(cities, airports)]
    

    Alternately, the list constructor can convert tuples to lists:

    [list(x) for x in zip(cities, airports)]
    

    Or, the map function is slightly less verbose in this case:

    map(list, zip(cities, airports))
    

提交回复
热议问题