How to conditionally skip number of iteration steps in a for loop in python?

后端 未结 6 901

We have a list item_list,

item_list = [\"a\", \"b\", \"XYZ\", \"c\", \"d\", \"e\", \"f\", \"g\"]

We iterate over its items with a

相关标签:
6条回答
  • 2021-01-25 00:58
    list_iter = iter(item_list)
    
    for item in list_iter:
        if item == "XYZ":
            do_something()
            for _ in range(3):   # skip next 3 items
                next(list_iter, None)
    
    # etc.
    

    Basically, rather than iterating over the list directly, you create an abstraction for it called an iterator and iterate over that. You can tell the iterator to advance to the next item by calling next(...) which we do three times to skip the next three items. The next time through the loop, it picks up at the next item after that.

    0 讨论(0)
  • 2021-01-25 01:15

    Use an iterator:

    $ cat t.py 
    item_list = ["a", "b", "XYZ", "c", "d", "e", "f", "g"]
    
    it = iter(item_list)
    for item in it:
        if item == "XYZ":
           print item
           for _ in range(3):
               next(it, None)
        else:
            print item
    

    This gives:

    $ python t.py 
    a
    b
    XYZ
    f
    g
    
    0 讨论(0)
  • 2021-01-25 01:16

    Since nobody has mentioned a while loop, I will:

    item_list = ["a", "b", "XYZ", "c", "d", "e", "f", "g"]
    i = 0
    while i < len(item_list):
        item = item_list[i]
        if item == "XYZ":
            do_something()
            i += 3
        else:
            do_something_else()
        i += 1
    
    0 讨论(0)
  • 2021-01-25 01:22

    I would split the processing it two parts for sake of readability

    >>> def foo(item_list,key = "XYZ", skip = 3):
        from itertools import takewhile, islice
        def do_something():
            return "do_something()"
        def do_something_else():
            return "do_something_else()"
        it = iter(item_list)
        for items in takewhile(lambda e: e != key, it):
            print items, do_something_else()
        print do_something()
        it = islice(it,skip, None)
        for items in it:
            print items, do_something_else()
    
    
    >>> foo(item_list)
    a do_something_else()
    b do_something_else()
    do_something()
    f do_something_else()
    g do_something_else()
    
    0 讨论(0)
  • 2021-01-25 01:22

    I'd store a counter that handles skipping items.

    def skipper(item_list):
        skip_count = 0
        for item in item_list:
            if item == "XYZ":
                skip_count = 3
            else:
                if skip_count:
                    skip_count -= 1
                else:
                    # do_something()
                    print item,
    

    Example:

    In [23]: item_list
    Out[23]: ['a', 'b', 'XYZ', 'c', 'd', 'e', 'f', 'g']
    
    In [24]: skipper(item_list)
    a b f g
    
    0 讨论(0)
  • 2021-01-25 01:22

    I'd probably write it out like this, personally:

    xyz_i = item_list.index('XYZ')
    
    do_something('XYZ') #or do_something(item_list[xyz_i]) but.. just save yourself the list lookup
    
    for x in item_list[xyz_i+4:]:
        do_something_else(x)
    
    0 讨论(0)
提交回复
热议问题