python - Indexing form the end of a generator -
say have generator in python , want iterate on in except first 10 iterations , last 10 iterations. itertools.islice supports first part of slicing operation, not second. there simple way accomplish this?
not there not simple way, there not way @ all, if want allow generator (or iterable). in general, there no way know when 10 items end of generator, or whether generator has end. generators give 1 item @ time, , tell nothing how many items "left". have iterate through entire generator, keeping temporary cache of recent 10 items, , yield when (or if!) generator terminates.
note "or if". generator need not finite. infinite generator, there no such thing "last" 10 elements.
Comments
Post a Comment