python - Is there a counterpart of next()? -


when reading through file, know it's possible next line via next(), there function like, say, prev() go previous line?

in similar vein, when looping through file, possible second line after target line? next() return line after string.

no, there no counterpart. can store previous lines in variable:

prev = none in an_iterator:     # prev     prev = 

for multiple items use collections.deque() object:

from collections import deque  five_prev = deque(an_iterator, maxlen=5) in an_iterator:     five_lines_back = five_prev.popleft()     # ...     five_prev.append(something) 

you cannot know comes next until retrieve iterable, can track came previously.

you not limited calling next() once; if need retrieve 2 lines iterable, call next() twice, or use itertools.islice() next() retrieve multiple items.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -