Python generator on dictionary view does not seem to be lazily evaluated -


It is my understanding that dragon generator expressions have returned the expression expressions of light, and their underlying dictionary changed But see the objects on the dictionaries of dictionaries. / P>

Looking at the following code (Windows Python 2.7.6):

  d = {} gt_three = (x for x in d.viewkeys ()) If x & Gt; 3) Print list (gt_three) D [1] = 'A' D [4] = 'Four' print list (gt_three)   

I think the output is expected as follows: Instead, I'm getting: > If I prepare the generator after adding it to the dictionary, or print the view itself, So I see the expected output. What I can not understand about the generator? I am new to Python.

A generator expression gives an Iterator, which you can only repeat once. After the first attempt to iterate over an iterator, it will always appear empty.

Comments

Popular posts from this blog

python - how we can use ajax() in views.py in django? -

matlab - Using loops to get multiple values into a cell -

python - Sequence Pattern recognition with Pybrain -