Is there a value in using map() vs for?

前端 未结 8 2857
误落风尘
误落风尘 2021-02-20 17:03

Does map() iterate through the list like \"for\" would? Is there a value in using map vs for?

If so, right now my code looks like this:

for item in item         


        
8条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-20 17:35

    There is a slight semantic difference, which is probably closed in python language spec. The map is explicitly parallelizable, while for only in special situations. Code can break out from for, but only escape with exception from map.

    In my opinion map shouldn't also guarantee order of function application while for must. AFAIK no python implementation is currently able to do this auto-parallelization.

提交回复
热议问题