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

前端 未结 10 1079
太阳男子
太阳男子 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:45

    Call me crazy, but why is using itertools thought to be necessary? What's wrong with:

    def perform_func_on_each_object_in_each_of_multiple_containers(func, containers):
        for container in containers:
            for obj in container:
                func(obj)
    
    perform_func_on_each_object_in_each_of_multiple_containers(some_action, (deque1, deque2, deque3)
    

    Even crazier: you probably are going to use this once. Why not just do:

    for d in (deque1, deque2, deque3):
        for obj in d:
            some_action(obj)
    

    What's going on there is immediately obvious without having to look at the code/docs for the long-name function or having to look up the docs for itertools.something()

    0 讨论(0)
  • 2020-12-28 13:47

    Depending on what order you want to process the items:

    import itertools
    
    for items in itertools.izip(deque1, deque2, deque3):
        for item in items:
            some_action(item)
    
    for item in itertools.chain(deque1, deque2, deque3):
        some_action(item)
    

    I'd recommend doing this to avoid hard-coding the actual deques or number of deques:

    deques = [deque1, deque2, deque3]
    for item in itertools.chain(*deques):
        some_action(item)
    

    To demonstrate the difference in order of the above methods:

    >>> a = range(5)
    >>> b = range(5)
    >>> c = range(5)
    >>> d = [a, b, c]
    >>>
    >>> for items in itertools.izip(*d):
    ...     for item in items:
    ...         print item,
    ...
    0 0 0 1 1 1 2 2 2 3 3 3 4 4 4
    >>>
    >>> for item in itertools.chain(*d):
    ...     print item,
    ...
    0 1 2 3 4 0 1 2 3 4 0 1 2 3 4
    >>>
    
    0 讨论(0)
  • 2020-12-28 13:47

    If I understand your question correctly, then you can use map with the first argument set to None, and all the other arguments as your lists to iterate over.

    E.g (from an iPython prompt, but you get the idea):

    In [85]: p = [1,2,3,4]
    
    In [86]: q = ['a','b','c','d']
    
    In [87]: f = ['Hi', 'there', 'world', '.']
    
    In [88]: for i,j,k in map(None, p,q,f):
       ....:     print i,j,k
       ....:
       ....:
    1 a Hi
    2 b there
    3 c world
    4 d .
    
    0 讨论(0)
  • 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."

    0 讨论(0)
  • 2020-12-28 13:51

    Accepts a bunch of iterables, and yields the contents for each of them in sequence.

    def XXX(*lists):
       for aList in lists:
          for item in aList:
             yield item
    
    
    
    l1 = [1, 2, 3, 4]
    l2 = ['a', 'b', 'c']
    l3 = [1.0, 1.1, 1.2]
    
    for item in XXX(l1, l2, l3):
       print item
    
    1
    2
    3
    4
    a
    b
    c
    1.0
    1.1
    1.2
    
    0 讨论(0)
  • 2020-12-28 13:54

    I would simply do this :

    for obj in deque1 + deque2 + deque3:  
    some_action(obj)  
    
    0 讨论(0)
提交回复
热议问题