“For” loop first iteration

前端 未结 13 2355
渐次进展
渐次进展 2020-12-04 15:17

I would like to inquire if there is an elegant pythonic way of executing some function on the first loop iteration. The only possibility I can think of is:

f         


        
13条回答
  •  伪装坚强ぢ
    2020-12-04 15:52

    Here, I could come with a Pythonic idiom that can look "pertty". Although, most likely I'd use the form you suggested in asking the question, just for the code to remain more obvious, though less elegant.

    def copy_iter():
        yield root.copy
        while True:
            yield somewhereElse.copy
    
    for member, copy in zip(something.get(), copy_iter()):
        copy(member)
        foo(member)
    

    (sorry - the first I posted, before editing, form would not work, I had forgotten to actually get an iterator for the 'copy' object)

提交回复
热议问题