An elegant and fast way to consecutively iterate over two or more containers in Python?

前端 未结 10 1174
太阳男子
太阳男子 2020-12-28 13:29

I have three collection.deques and what I need to do is to iterate over each of them and perform the same action:

for obj in deque1:  
    some_action(obj)           


        
10条回答
  •  抹茶落季
    2020-12-28 13:49

    It looks like you want itertools.chain:

    "Make an iterator that returns elements from the first iterable until it is exhausted, then proceeds to the next iterable, until all of the iterables are exhausted. Used for treating consecutive sequences as a single sequence."

提交回复
热议问题