views:

366

answers:

8

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!

+1  A: 

Hi jr0d,

Not exactly sure I follow your thought process but here is something to feed on..

for i in range(len(cdata.split('\n'))):
  if i in range(50,60): continue
  line = cdata[i]
  if exp.match(line):
    #increment the position of the iterator by 5?
    pass
  print line

Not sure what you are really after but the range(len(..)) should help you.

rh0dium
completely unpythonic!
fortran
(at least use enumerate :p)
fortran
Yeah - Enumerate was absolutely what I should have done. Point taken. I voted for the one above mine. Much better - still have a lot to learn.
rh0dium
+1  A: 

If you're doing it with numbers a list comprehension can work:

for i in [x for x in range(0, 99) if x < 50 and x > 59]:
    print i

Moving an iterator forward is a bit more difficult though. I'd suggest setting your list up beforehand if you don't want to do the counter approach, probably by splitting cdata, then working out the indexes of the matching line and removing that line and the following ones. Apart from that you're stuck with the counter approach which isn't nearly as unpleasant as you make it out to be to be honest.

Another option is this:

iterator = iter(cdata.split('\n'))
for line in iterator:
    if exp.match(line):
        for i in range(0, 5):
            try:
                iterator.next()
            except StopIteration:
                break
    else:
        print line
Benno
Shouldn't "if x < 50 and x > 59" be "if x < 50 or x > 59"?
Kylotan
To make this work with iterators, can you not just replace the list comprehension with a generator expression? That is, instead of [x for x in ...], do (x for x in ...). This will prevent another list being created.
SpoonMeiser
A: 

You can drop values from an iterator

def dropvalues(iterator, vals):
    for i in xrange(vals): iterator.next()

Now just make sure you have an iterator object to work on with lines = iter(cdata.split('\n')); and loop over it.

kaizer.se
A: 

Maybe with genexps. Not pretty but...

Something like that:

>>> gx = (line for line in '1 2 x 3 4 5 6 7 x 9 10 11 12 x 1'.split('\n'))
>>> for line in gx:
...   if line == 'x':
...      for i in range(2):
...          line = gx.next()
...   print line

The only problem is to ensure that gx can be next()-ed. The above example purposely generates an exception due to the last x.

mjv
+10  A: 

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(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
kRON
+1: for the Recipes reference.
J.F. Sebastian
Note that this is iterative, unlike the original code. You can see the difference if you use a huge range, eg. xrange(1000000000), and do very large jumps. Usually this won't matter, but in some algorithms it might (eg. if generating the item is expensive), so be aware of it. There's just no API to ask an iterator to skip items; it has to run it iteratively.
Glenn Maynard
A: 

I can't parse the question vary well because there's this block of confusing and irrelevant C code. Please delete it.

Focusing on just the Python code and the question about how to skip 5 lines...

lineIter= iter( cdata.splitlines() )
for line in lineIter:
  if exp.match(line):
    for count in range(5):
        line = lineIter.next()
  print line
S.Lott
It's straightforward, easy-to-read C code. I understood what he wanted to do instantly by glancing at it; code is almost always the clearest explanation. If you can't read C, that's your problem.
Glenn Maynard
It might have been c or pseudo-code. An example is worth it.
whatnick
I read C fine. I could not reconcile the C example with the problem statement. I found the C code perfectly readable, but irrelevant to the problem statement provided. I can -- and did -- understand the C code. I did not see the relationship between the C and the problem.
S.Lott
I was simply looking for a way arbitrarily minimize loop iteration in python. The c code is perfectly relevant to the query, and is actually the reason I submitted the query. Had I not known how to do such a thing in another language, I would have imported itertools and been done with it. To my original query, it seems there is not a way to accomplish the same or similar task using an equal number of iterations in python to that of c. This is why perl has both foreach and for statements, I would wager (not really). Thanks to all for the answers and feedback
jr0d
You can keep repeating that the code is perfectly relevant. However, I still cannot see any relevance at all. Fixing the question to show some logical connection between the block of clear but irrelevant C code and Python would be helpful to the stupid people (like me) who absolutely cannot see any relationship at all.
S.Lott
+3  A: 

itertools.islice:

lines = iter(cdata.splitlines())
for line in lines:
    if exp.match(line):
       #increment the position of the iterator by 5
       for _ in itertools.islice(lines, 4):
           pass
       continue # skip 1+4 lines
    print line

For example, if exp, cdata are:

exp = re.compile(r"skip5")
cdata = """
before skip
skip5
1 never see it
2 ditto
3 ..
4 ..
5 after skip
6 
"""

Then the output is:


before skip
5 after skip
6 

Python implementation of the C example

i = 0
while i < 100:
    if i == 50:
       i += 10
    print i
    i += 1

As @[Glenn Maynard] pointed out in the comment if you need to do a very large jumps such as i += 100000000 then you should use explicit while loop instead of just skipping steps in a for loop.

Here's the example that uses explicit while loop instead islice:

lines = cdata.splitlines()
i = 0
while i < len(lines):
    if exp.match(lines[i]):
       #increment the position of the iterator by 5
       i += 5
    else:
       print lines[i]
       i += 1

This example produces the same output as the above islice example.

J.F. Sebastian
A: 

for your example, as you're working with lists (indexable sequences) and not with iterators, I would recommend the following:

lines = cdata.split("\n")
for line in lines[:50]+lines[60:]:
  print line

that's not the most efficient since it potentially constructs 3 new lists (but if the skipped part is bigger that the processed part, it could be more efficient than the other options), but it's quite clean and explicit.

If you don't mind to use the itertools module, you can convert the lists to sequences easily:

from itertools import chain, islice
for line in chain(islice(lines, None, 50), islice(lines, 60,None)):
  print line
fortran