<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Mon, Aug 7, 2017 at 4:14 PM, Chris Angelico <span dir="ltr"><<a href="mailto:rosuav@gmail.com" target="_blank">rosuav@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On Tue, Aug 8, 2017 at 5:30 AM, Soni L. <<a href="mailto:fakedme%2Bpy@gmail.com">fakedme+py@gmail.com</a>> wrote:<br>
> The generator syntax, (x for x in i if c), currently always creates a new<br>
> generator.</span></blockquote><div><br></div><div>that's what it's for -- I'm confused as to what the problem is.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">> {x for x in integers if 1000 <= x < 1000000} # never completes, because it's<br>
> trying to iterate over all integers<br></span></blockquote><div><br></div><div>this is a set comprehension -- but what is "integers"? is it a generator?</div><div><br></div><div>in which case, it should take an argument so it knows when to end. Or if it's really that symple, that's what range() is for.</div><div><br></div><div><br></div><div>However, similarly, I find that sometimes I want to iterate over a slice of a sequence, but do'nt want to actually make the slice first.</div><div><br></div><div>So there is itertools.islice()</div><div><br></div><div>If "integers" is a sequence:</div><div><br></div><div>{x for x in integers[1000:10000]}</div><div><br></div><div>makes an unneeded copy of that slice.</div><div><br></div><div>{x for x in itertools.islice(integers, 1000, 10000)}</div><div><br></div><div>will iterate on the fly, and not make any extra copies.</div><div><br></div><div>It would be nice to have an easier access to an "slice iterator" though -- one of these days I may write up a proposal for that.</div><div><br></div><div>-CHB</div><div><br></div><div>-- <br></div></div><div class="gmail_signature" data-smartmail="gmail_signature"><br>Christopher Barker, Ph.D.<br>Oceanographer<br><br>Emergency Response Division<br>NOAA/NOS/OR&R (206) 526-6959 voice<br>7600 Sand Point Way NE (206) 526-6329 fax<br>Seattle, WA 98115 (206) 526-6317 main reception<br><br><a href="mailto:Chris.Barker@noaa.gov" target="_blank">Chris.Barker@noaa.gov</a></div>
</div></div>