Force all iterations on an iterable

前端 未结 1 1168
一向
一向 2021-01-21 16:03

I\'ve written a for-loop using map, with a function that has a side-effect. Here\'s a minimal working example of what I mean:

def someFunc(t):
    n, d = t
    d         


        
相关标签:
1条回答
  • 2021-01-21 17:00

    You don't want to do this (run a map() just for the side effects), but there is a itertools consume recipe that applies here:

    from collections import deque
    
    deque(map(somefunc, ((i,d) for i in range(10**3))), maxlen=0)
    

    The collections.deque() object, configured to a maximum size of 0, consumes the map() iterable with no additional memory use. The deque object is specifically optimized for this use-case.

    0 讨论(0)
提交回复
热议问题