Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python arbitrarily incrementing an iterator inside a loop

I am probably going about this in the wrong manner, but I was wondering how to handle this in python.

First some c code:

int i;  for(i=0;i<100;i++){   if(i == 50)     i = i + 10;   printf("%i\n", i); } 

Ok so we never see the 50's...

My question is, how can I do something similar in python? For instance:

for line in cdata.split('\n'):   if exp.match(line):     #increment the position of the iterator by 5?     pass   print line 

With my limited experience in python, I only have one solution, introduce a counter and another if statement. break the loop until the counter reaches 5 after exp.match(line) is true.

There has got to be a better way to do this, hopefully one that does not involve importing another module.

Thanks in advance!

like image 672
jr0d Avatar asked Sep 24 '09 23:09

jr0d


People also ask

How do you increment a variable inside a loop Python?

In python, if you want to increment a variable we can use “+=” or we can simply reassign it “x=x+1” to increment a variable value by 1.

Can you increase i in a for loop?

Python3. Using While loop: We can't directly increase/decrease the iteration value inside the body of the for loop, we can use while loop for this purpose.

Can iterator be used in for loop?

An Iterator is an object that can be used to loop through collections, like ArrayList and HashSet. It is called an "iterator" because "iterating" is the technical term for looping. To use an Iterator, you must import it from the java.

Is there ITER loop in Python?

For example, open files in Python are iterable. As you will see soon in the tutorial on file I/O, iterating over an open file object reads data from the file. In fact, almost any object in Python can be made iterable. Even user-defined objects can be designed in such a way that they can be iterated over.


1 Answers

There is a fantastic package in Python called itertools.

But before I get into that, it'd serve well to explain how the iteration protocol is implemented in Python. When you want to provide iteration over your container, you specify the __iter__() class method that provides an iterator type. "Understanding Python's 'for' statement" is a nice article covering how the for-in statement actually works in Python and provides a nice overview on how the iterator types work.

Take a look at the following:

>>> sequence = [1, 2, 3, 4, 5] >>> iterator = sequence.__iter__() >>> iterator.next() 1 >>> iterator.next() 2 >>> for number in iterator:     print number  3 4 5 

Now back to itertools. The package contains functions for various iteration purposes. If you ever need to do special sequencing, this is the first place to look into.

At the bottom you can find the Recipes section that contain recipes for creating an extended toolset using the existing itertools as building blocks.

And there's an interesting function that does exactly what you need:

def consume(iterator, n):     '''Advance the iterator n-steps ahead. If n is none, consume entirely.'''     collections.deque(itertools.islice(iterator, n), maxlen=0) 

Here's a quick, readable, example on how it works (Python 2.5):

>>> import itertools, collections >>> def consume(iterator, n):     collections.deque(itertools.islice(iterator, n)) >>> iterator = range(1, 16).__iter__() >>> for number in iterator:     if (number == 5):         # Disregard 6, 7, 8, 9 (5 doesn't get printed just as well)         consume(iterator, 4)     else:         print number  1 2 3 4 10 11 12 13 14 15 
like image 172
Filip Dupanović Avatar answered Sep 18 '22 13:09

Filip Dupanović