I have a python for loop, in which I need to look ahead one item to see if an action needs to be performed before processing.
for line in file:
if the start of the next line == "0":
perform pre-processing
...
continue with normal processing
...
Is there any easy way to do this in python? My current approach is to buffer the file to an array, however this is not ideal as the file is rather large.
Map() Function in Python The map() function is a replacement to a for a loop. It applies a function for each element of an iterable.
JavaScript, C, C++, Java, PHP, and a whole bunch of other programming languages all have this kind of for loop. But Python does not. Python does not have traditional C-style for loops. We do have something that we call a for loop in Python, but it works like a foreach loop.
To loop through a set of code a specified number of times, we can use the range() function, The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number.
The for loop in Python is used to iterate over a sequence, which could be a list, tuple, array, or string. The program operates as follows: We have assigned a variable, x, which is going to be a placeholder for every item in our iterable object.
you can get any iterable to prefetch next item with this recipe:
from itertools import tee, islice, izip_longest
def get_next(some_iterable, window=1):
items, nexts = tee(some_iterable, 2)
nexts = islice(nexts, window, None)
return izip_longest(items, nexts)
Example usage:
for line, next_line in get_next(myfile):
if next_line and next_line.startswith("0"):
... do stuff
The code allows you to pass the window
parameter as a larger value, if you want to look 2 or more lines ahead.
Along the lines of nosklo's answer, I tend to use the following pattern:
The function pairwise
from the excellent itertools recipes is ideal for this:
from itertools import tee
def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = tee(iterable)
next(b, None)
return izip(a, b)
Using it in your code gets us:
for line, next_line in pairwise(file):
if next_line.startswith("0"):
pass #perform pre-processing
#...
pass #continue with normal processing
Generally, for this type of processing (lookahead in the iterable), I tend to use a window function. Pairwise is a special case of a window of size 2.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With