python - How to iterate over a generator? -
assume big list , want iterate on it.
>>> x=[] >>> in x: print(x)
but because list big, using generator best way:
>>> g=(i in range(10)) >>> g.__next__() 0
but there problem number of times iterate. because don't know how many times should use g.__next__()
, need more dynamic.
for example:
for in x:
it not important how long x loop iterate end.
how can iterate using generators?
you can use for
syntax generators:
>>> g=(i in range(10)) >>> g <generator object <genexpr> @ 0x7fd70d42b0f0> >>> in g: ... print ... 0 1 2 3 4 5 6 7 8 9
Comments
Post a Comment