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

后端 未结 5 1235
暖寄归人
暖寄归人 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:15

    If you wanted to do it without using zip at all, you would have to do something like this:

    [ [cities[i],airports[i]] for i in xrange(min(len(cities), len(airports))) ]
    

    but there is no reason to do that other than an intellectual exercise.

    Using map(list, zip(cities, airports)) is shorter, simpler and will almost certainly run faster.

提交回复
热议问题