Можно использовать fopen()
на php://stdin
:
$f = fopen('php://stdin', 'r');
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
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
Например , 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
"""
Тогда вывод будет:
before skip 5 after skip 6
i = 0
while i < 100:
if i == 50:
i += 10
print i
i += 1
Как @ [Glenn Maynard] указал в ] комментарий , если вам нужно сделать очень большие прыжки, такие как i + = 100000000, тогда вы должны использовать явный цикл while
вместо того, чтобы просто пропускать шаги в цикле for
.
Вот пример, в котором используется явный цикл while
вместо islice
:
Если вы делаете это с числами, понимание списка может работать:
for i in [x for x in range(0, 99) if x < 50 and x > 59]:
print i
Однако перемещение итератора вперед немного сложнее. Я бы посоветовал настроить свой список заранее, если вы не хотите использовать встречный подход, возможно, разделив cdata, затем разработав индексы соответствующей строки и удалив эту строку и следующие. Кроме того, вы придерживаетесь встречного подхода, который не так неприятен, как вы его себе представляете, если честно.
Другой вариант:
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
Не совсем уверен, что слежу за вашим мыслительным процессом, но здесь есть чем питаться ..
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
Не уверен, что вам на самом деле нужно, но диапазон (len (..)) должен вам помочь .
Вы можете удалить значения из итератора
def dropvalues(iterator, vals):
for i in xrange(vals): iterator.next()
Теперь просто убедитесь, что у вас есть объект-итератор для работы с lines = iter (cdata.split ('\ n'))
; и перебрать его.
Может быть, с помощью genxps. Не очень красиво, но ...
Что-то вроде этого:
>>> 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
Единственная проблема - убедиться, что gx может быть next () - ed. В приведенном выше примере специально создается исключение из-за последнего x.
для вашего примера, поскольку вы работаете со списками (индексируемыми последовательностями), а не с итераторами, я бы рекомендовал следующее:
lines = cdata.split("\n")
for line in lines[:50]+lines[60:]:
print line
это не самый эффективный вариант, поскольку он потенциально создает 3 новых списки (но если пропущенная часть больше, чем обрабатываемая, она могла бы быть более эффективной, чем другие варианты), но она довольно чистая и явная.
Если вы не против использовать модуль itertools, вы можете преобразовать списки в последовательности легко:
from itertools import chain, islice
for line in chain(islice(lines, None, 50), islice(lines, 60,None)):
print line
Я не могу хорошо разобрать вопрос, потому что там есть блок запутанного и нерелевантного кода C. Пожалуйста, удалите его.
Сосредоточение внимания только на коде Python и вопросе о том, как пропустить 5 строк ...
lineIter= iter( cdata.splitlines() )
for line in lineIter:
if exp.match(line):
for count in range(5):
line = lineIter.next()
print line