Skip iterations in enumerated list object (python)

I have a code

for iline, line in enumerate(lines):
    ...
    if <condition>:
        <skip 5 iterations>

I would like, as you can read, for the for loop to skip 5 iterations if the condition is met. I can be sure that if the condition is met, 5 or more objects remain in the "lines" object.

there are lines from dictionaries that should be looped in order

+4
source share
5 answers
iline = 0
while iline < len(lines):
    line = lines[iline]
    if <condition>:
        place_where_skip_happened = iline
        iline += 5
    iline += 1

If you iterate over a file object, you can skip the lines using the following or make the lines an iterator:

lines = iter(range(20))

for l in lines:
    if l == 10:
        [next(lines) for _ in range(5)]
    print(l)
0
1
2
3
4
5
6
7
8
9
10
16
17
18
19

It really depends on what you do and what you want.

Using native code with iter and islice :

from itertools import islice


it = iter(enumerate(lines))

for iline, line in it:
    if <condition>:
        place_where_skip_happened = iline
        next(islice(it,5 ,5), None)
    print(line)
+8

, , (. itertools.)

:

from itertools import islice

lines = list("abcdefghij")

lit = iter(enumerate(lines))
for iline, line in lit:
    print(iline, line)
    if line == "c":
        # skip 3
        next(islice(lit, 3,3), None)

0 a
1 b
2 c
6 g
7 h
8 i
9 j
+6

Padraic Cunningham, while, if:

iline = 0
skip = {True:5, False:1}

while iline > len(lines):
    line = lines[iline]
    ...
    iline += skip[condition]
0

, , :

ignore = 0
for iline, line in enumerate(lines):
    if ignore > 0:
        ignore -= 1
        continue

    print(iline, line)

    if iline == 5:
        ignore = 5

5 :

enum_lines = enumerate(lines)
for iline, line in enum_lines:
    print(iline, line)

    if iline == 5:
        for _, _ in zip(range(5), enum_lines):
            pass

, Pythonic.

0

, for :

def my_function(iline, line, rest_of_lines, **other_args):
    do_some_side_effects(iline, line, **other_args)

    if rest_of_lines == []:
        return <some base case>

    increment = 5 if <condition> else 1
    return my_function(iline+increment, 
                       rest_of_lines[increment-1], 
                       rest_of_lines[increment:],
                       **other_args)

, , , None.

- :

other_args = get_other_args(...)

my_function(0, lines[0], lines[1:], **other_args)

, - , , , . do_some_side_effects , .

def my_function(iline, line, rest_of_lines, output, **other_args):
    some_value = do_some_side_effects(iline, line, **other_args)

    new_output = put_value_in_output(some_value, output)
    # could be as simple as appending to a list/inserting to a dict
    # or as complicated as you want.

    if rest_of_lines == []:
        return new_output

    increment = 5 if <condition> else 1
    return my_function(iline+increment, 
                       rest_of_lines[increment-1], 
                       rest_of_lines[increment:],
                       new_output,
                       **other_args)

other_args = get_other_args(...)

empty_output = get_initial_data_structure(...)

full_output = my_function(0, lines[0], lines[1:], empty_output, **other_args)

, Python - , , , - while.

My advice: use a loop while, although I would like to structure my projects and APIs so that using a recursive functional approach is still efficient and readable. I will also try to avoid side effects within the cycle.

0
source

All Articles