Iterate a list as pair (current, next) in Python

前端 未结 10 1662
隐瞒了意图╮
隐瞒了意图╮ 2020-11-21 22:54

I sometimes need to iterate a list in Python looking at the \"current\" element and the \"next\" element. I have, till now, done so with code like:

for curre         


        
相关标签:
10条回答
  • 2020-11-21 22:59

    Pairs from a list using a list comprehension

    the_list = [1, 2, 3, 4]
    pairs = [[the_list[i], the_list[i + 1]] for i in range(len(the_list) - 1)]
    for [current_item, next_item] in pairs:
        print(current_item, next_item)
    

    Output:

    (1, 2)
    (2, 3)
    (3, 4)
    
    0 讨论(0)
  • 2020-11-21 23:01

    This is now a simple Import As of 16th May 2020

    from more_itertools import pairwise
    for current, next in pairwise(your_iterable):
      print(f'Current = {current}, next = {nxt}')
    

    Docs for more-itertools Under the hood this code is the same as that in the other answers, but I much prefer imports when available.

    If you don't already have it installed then: pip install more-itertools

    Example

    For instance if you had the fibbonnacci sequence, you could calculate the ratios of subsequent pairs as:

    from more_itertools import pairwise
    fib= [1,1,2,3,5,8,13]
    for current, nxt in pairwise(fib):
        ratio=current/nxt
        print(f'Curent = {current}, next = {nxt}, ratio = {ratio} ')
    
    0 讨论(0)
  • 2020-11-21 23:05

    Iterating by index can do the same thing:

    #!/usr/bin/python
    the_list = [1, 2, 3, 4]
    for i in xrange(len(the_list) - 1):
        current_item, next_item = the_list[i], the_list[i + 1]
        print(current_item, next_item)
    

    Output:

    (1, 2)
    (2, 3)
    (3, 4)
    
    0 讨论(0)
  • 2020-11-21 23:08

    I am really surprised nobody has mentioned the shorter, simpler and most importantly general solution:

    Python 3:

    from itertools import islice
    
    def n_wise(iterable, n):
        return zip(*(islice(iterable, i, None) for i in range(n)))
    

    Python 2:

    from itertools import izip, islice
    
    def n_wise(iterable, n):
        return izip(*(islice(iterable, i, None) for i in xrange(n)))
    

    It works for pairwise iteration by passing n=2, but can handle any higher number:

    >>> for a, b in n_wise('Hello!', 2):
    >>>     print(a, b)
    H e
    e l
    l l
    l o
    o !
    
    >>> for a, b, c, d in n_wise('Hello World!', 4):
    >>>     print(a, b, c, d)
    H e l l
    e l l o
    l l o
    l o   W
    o   W o
      W o r
    W o r l
    o r l d
    r l d !
    
    0 讨论(0)
  • 2020-11-21 23:11

    Since the_list[1:] actually creates a copy of the whole list (excluding its first element), and zip() creates a list of tuples immediately when called, in total three copies of your list are created. If your list is very large, you might prefer

    from itertools import izip, islice
    for current_item, next_item in izip(the_list, islice(the_list, 1, None)):
        print(current_item, next_item)
    

    which does not copy the list at all.

    0 讨论(0)
  • 2020-11-21 23:16

    A basic solution:

    def neighbors( list ):
      i = 0
      while i + 1 < len( list ):
        yield ( list[ i ], list[ i + 1 ] )
        i += 1
    
    for ( x, y ) in neighbors( list ):
      print( x, y )
    
    0 讨论(0)
提交回复
热议问题