Generator Expressions vs. List Comprehension

前端 未结 9 1821
梦如初夏
梦如初夏 2020-11-21 06:56

When should you use generator expressions and when should you use list comprehensions in Python?

# Generator expression
(x*2 for x in range(256))

# List com         


        
9条回答
  •  闹比i
    闹比i (楼主)
    2020-11-21 07:29

    I'm using the Hadoop Mincemeat module. I think this is a great example to take a note of:

    import mincemeat
    
    def mapfn(k,v):
        for w in v:
            yield 'sum',w
            #yield 'count',1
    
    
    def reducefn(k,v): 
        r1=sum(v)
        r2=len(v)
        print r2
        m=r1/r2
        std=0
        for i in range(r2):
           std+=pow(abs(v[i]-m),2)  
        res=pow((std/r2),0.5)
        return r1,r2,res
    

    Here the generator gets numbers out of a text file (as big as 15GB) and applies simple math on those numbers using Hadoop's map-reduce. If I had not used the yield function, but instead a list comprehension, it would have taken a much longer time calculating the sums and average (not to mention the space complexity).

    Hadoop is a great example for using all the advantages of Generators.

提交回复
热议问题