Non-lazy evaluation version of map in Python3?

后端 未结 6 1729
醉话见心
醉话见心 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:10

    You can set up a zero length deque to do this:

    with open("output.csv", "w") as f:
        writer = csv.writer(f)
        collections.deque(map(writer.writerow, data),0)
    

    This is the same way that itertools.consume(iterator, None) recipe works. It functionally will exhaust the iterator without building the list.

    You can also just use the consume recipe from itertools.

    But a loop is more readable and Pythonic to me, but YMMV.

提交回复
热议问题