Non-lazy evaluation version of map in Python3?

后端 未结 6 1707
醉话见心
醉话见心 2020-12-07 00:23

I\'m trying to use map in Python3. Here\'s some code I\'m using:

import csv

data = [
    [1],
    [2],
    [3]
]

with open(\"output.csv\", \"w         


        
6条回答
  •  有刺的猬
    2020-12-07 01:04

    list(map(lambda x: do(x),y))
    

    will trigger an evaluation and stay in the nice, readable semantics that improve human runtime efficiency beyond the "for-loop (which is a map itself) plus new paragraph scope transition" semantics. ¯\(ツ)

    Note that there is no reason not to call it semantic sugar during an initial draft (in fact, for loops are usually easier since they are more modular: you may not know what your code needs to do during your first try at a problem), but when you are productionalizing or reverse engineering code that is in a working state, increasing semantic efficiency (or even merely rewriting in equivalently nice code) is a strong factor of success.

    Anyhow, if you want to flush the map stack, trigger it with a list type conversion.


    So:

    import csv
    
    data = [
        [1],
        [2],
        [3]
    ]
    
    with open("output.csv", "w") as f:
        writer = csv.writer(f)
        list(map(writer.writerow, data))
    

提交回复
热议问题