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

前端 未结 8 2856
误落风尘
误落风尘 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:31

    You can switch Your map to some cool threaded OR multiprocessing OR distributed computing framework if You need to. Disco is an example of distributed, resistant to failures erlang-and-python based framework. I configured it on 2 boxes of 8 cores and now my program runs 16 times faster, thanks to the Disco cluster, however I had to rewrite my program from list comprehensions and for loops to map/reduce.

    It's the same deal to write a program using for loops and list comprehensions and map/reduce, but when You need it to run on a cluster, You can do it almost for free if You used map/reduce. If You didn't, well, You will have to rewrite.

    Beware: as far as I know, python 2.x returns a list instead of an iterator from map. I've heard this can be bypassed by using iter.imap() (never used it though).

提交回复
热议问题