How can I evaluate a value in a Python generator at the time I create the generator, not at the time I iterate it?

Chris

The following is my code:

import itertools

i = itertools.chain()
for a in [1, 2, 3]:
    i = itertools.chain(i, (a for _ in range(2)))

print(list(i))
[3, 3, 3, 3, 3, 3]

Is there a way I can access the value of a when creating the generator, rather than when I iterate it in the print statement?

I'd like the output to be [1,1,2,2,3,3], ie, the value of a when the generator was created.

This is a trivial problem, but in my case I am iterating 1,000,000 rows in the outer loop, then in the inner loop generating 8 rows for each of those million, so I'm keen to keep it a generator.

Nb. The use case is I'm iterating a table in the outer loop, creating sub-objects for each row, passing the primary key to the sub-objects. The numbers are pretty large, so I want to build up the generator, then bulk insert after the loop (using Django's Model.objects.bulk_create(generator)). But by the time I call bulk_create the primary key is always set to the last row in the outer loop.

gen = itertools.chain()
for id in ParentModel.objects.all().value_list('id', flat=True)):
    gen = itertools.chain(gen, (InnerModel(fk=id) for i in range(10000)))
InnerModel.objects.bulk_create(gen)

All the generated InnerModels point to the last OuterModel in the list.

Cychih

If you don't mind wrapping the tuple into a lambda:

>>> import itertools
>>> i = itertools.chain()
>>> for a in [1, 2, 3]:
>>>     i = itertools.chain(i, (lambda x: (x for _ in range(2)))(a))
>>> print(list(i))
[1, 1, 2, 2, 3, 3]

The idea is to copy the value of a of each iteration. lambda's argument can do that. In each iteration, a local variable x is created and assigned with a.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I create a generator within a generator- Python

From Dev

Can I await an enumerable I create with a generator?

From Dev

Can I await an enumerable I create with a generator?

From Dev

How can I yield a value from within a callback to a calling generator

From Dev

How can I make a generator prepare the next value in advance?

From Dev

How can I yield a value from within a callback to a calling generator

From Dev

App Generator: How can I declare a variable for use in the entire generator?

From Dev

How can I convert this async callback to a generator?

From Dev

How can I write a generator in a JavaScript class?

From Dev

How can I extend the JOOQ code generator?

From Dev

How can I write a code generator in Ceylon

From Dev

How can I get the following generator to work?

From Java

"RuntimeError: generator raised StopIteration" every time I try to run app

From Dev

"RuntimeError: generator raised StopIteration" every time I try to run app

From Dev

"RuntimeError: generator raised StopIteration" every time I try to run app

From Dev

How can I iterate through 2 divs in the same time?

From Dev

How can I iterate each time the user presses a button?

From Dev

How can I pass a callback to a generator that I pass to the "vo" library?

From Dev

How can i get the time interval in python

From Dev

I have a generator in my Python function; how can I return a modified list?

From Dev

Why can't I create a generic bubbleSort with generator? (Java)

From Dev

What should I use between generator and function with return for resume parsing in python where I need to process lots of resume at a time?

From Dev

How do I create a deterministic Random number generator with numpy seed?

From Dev

How do I create a javascript elite planet name generator?

From Dev

How do I create a biased random number generator?

From Dev

Python -- generator interspersing value between iterator -- am I doing this correctly?

From Dev

How can I make my number generator include max value? c#

From Dev

How can I time a pipe?

From Java

Why do I keep getting the same first digit while I've seeded a generator with time?

Related Related

  1. 1

    How do I create a generator within a generator- Python

  2. 2

    Can I await an enumerable I create with a generator?

  3. 3

    Can I await an enumerable I create with a generator?

  4. 4

    How can I yield a value from within a callback to a calling generator

  5. 5

    How can I make a generator prepare the next value in advance?

  6. 6

    How can I yield a value from within a callback to a calling generator

  7. 7

    App Generator: How can I declare a variable for use in the entire generator?

  8. 8

    How can I convert this async callback to a generator?

  9. 9

    How can I write a generator in a JavaScript class?

  10. 10

    How can I extend the JOOQ code generator?

  11. 11

    How can I write a code generator in Ceylon

  12. 12

    How can I get the following generator to work?

  13. 13

    "RuntimeError: generator raised StopIteration" every time I try to run app

  14. 14

    "RuntimeError: generator raised StopIteration" every time I try to run app

  15. 15

    "RuntimeError: generator raised StopIteration" every time I try to run app

  16. 16

    How can I iterate through 2 divs in the same time?

  17. 17

    How can I iterate each time the user presses a button?

  18. 18

    How can I pass a callback to a generator that I pass to the "vo" library?

  19. 19

    How can i get the time interval in python

  20. 20

    I have a generator in my Python function; how can I return a modified list?

  21. 21

    Why can't I create a generic bubbleSort with generator? (Java)

  22. 22

    What should I use between generator and function with return for resume parsing in python where I need to process lots of resume at a time?

  23. 23

    How do I create a deterministic Random number generator with numpy seed?

  24. 24

    How do I create a javascript elite planet name generator?

  25. 25

    How do I create a biased random number generator?

  26. 26

    Python -- generator interspersing value between iterator -- am I doing this correctly?

  27. 27

    How can I make my number generator include max value? c#

  28. 28

    How can I time a pipe?

  29. 29

    Why do I keep getting the same first digit while I've seeded a generator with time?

HotTag

Archive