List comprehension vs map

前端 未结 11 1842
长情又很酷
长情又很酷 2020-11-21 04:39

Is there a reason to prefer using map() over list comprehension or vice versa? Is either of them generally more efficient or considered generally more pythonic

11条回答
  •  难免孤独
    2020-11-21 05:09

    Here is one possible case:

    map(lambda op1,op2: op1*op2, list1, list2)
    

    versus:

    [op1*op2 for op1,op2 in zip(list1,list2)]
    

    I am guessing the zip() is an unfortunate and unnecessary overhead you need to indulge in if you insist on using list comprehensions instead of the map. Would be great if someone clarifies this whether affirmatively or negatively.

提交回复
热议问题