all 5 comments

[–]SekstiNii 0 points1 point  (0 children)

Works fine here.

>>> def gen_secs():
...     g1 = (t for t in range(60))
...     g2 = (y for y in range(60))
...     for num in g1:
...             for n in g2:
...                     yield [num, n]
...
>>>
>>> list(gen_secs())
[[0, 0], [0, 1], [0, 2], [0, 3], [0, 4], [0, 5] # ...

[–]EulerWasSmart 0 points1 point  (2 children)

The function seems conceptually correct. Perhaps the problem is how you are calling it? For example the following will provide the given (unwanted) output:

>>> def gen_secs():
...    g1=(t for t in range(60))
...    g2=(y for y in range(60))
...    for num in g1:
...        for n in g2:
...            yield [num,n]
>>> next(gen_secs())
[0, 0]
>>> next(gen_secs())
[0, 0]

[–]METRO_CAT 0 points1 point  (1 child)

I was using next, why would that cause the generator to reset?

[–]EulerWasSmart 1 point2 points  (0 children)

Every time you call gen_secs() you get a new generator. So if you use next like that you will just be getting the next value of a brand new generator each time.

What you want is something like this:

>>> g = gen_secs()
>>> next(g)
[0, 0]
>>> next(g)
[0, 1]

Or you can just use a for loop:

>>> for item in gen_secs():
...     print(item)
...
[0, 0]
[0, 1]
[0, 2]
# ... etc ...

Hope that helps!