迭代列表中的所有连续项对

时间:2022-03-08 21:43:08

Given a list

给出一个清单

l = [1, 7, 3, 5]

I want to iterate over all pairs of consecutive list items (1,7), (7,3), (3,5), i.e.

我想迭代所有连续列表项(1,7),(7,3),(3,5),即

for i in xrange(len(l) - 1):
    x = l[i]
    y = l[i + 1]
    # do something

I would like to do this in a more compact way, like

我想以更紧凑的方式做到这一点,比如

for x, y in someiterator(l): ...

Is there a way to do do this using builtin Python iterators? I'm sure the itertools module should have a solution, but I just can't figure it out.

有没有办法使用内置的Python迭代器来做到这一点?我确定itertools模块应该有一个解决方案,但我无法弄明白。

5 个解决方案

#1


69  

Just use zip

只需使用拉链

>>> l = [1, 7, 3, 5]
>>> for first, second in zip(l, l[1:]):
...     print first, second
...
1 7
7 3
3 5

As suggested you might consider using the izip function in itertools for very long lists where you don't want to create a new list.

正如所建议的那样,您可以考虑在itertools中使用izip函数,用于您不想创建新列表的很长列表。

import itertools

for first, second in itertools.izip(l, l[1:]):
    ...

#2


26  

Look at pairwise at itertools recipes: http://docs.python.org/2/library/itertools.html#recipes

在itertools食谱中看看pairwise:http://docs.python.org/2/library/itertools.html#recipes

Quoting from there:

引自那里:

def pairwise(iterable):
    "s -> (s0,s1), (s1,s2), (s2, s3), ..."
    a, b = tee(iterable)
    next(b, None)
    return izip(a, b)

A General Version

一般版本

A general version, that yields tuples of any given positive natural size, may look like that:

产生任何给定正自然大小的元组的通用版本可能如下所示:

def nwise(iterable, n=2):                                                      
    iters = tee(iterable, n)                                                     
    for i, it in enumerate(iters):                                               
        next(islice(it, i, i), None)                                               
    return izip(*iters)   

#3


7  

I would create a generic grouper generator, like this

我会像这样创建一个通用的石斑鱼发生器

def grouper(input_list, n = 2):
    for i in xrange(len(input_list) - (n - 1)):
        yield input_list[i:i+n]

Sample run 1

样品运行1

for first, second in grouper([1, 7, 3, 5, 6, 8], 2):
    print first, second

Output

产量

1 7
7 3
3 5
5 6
6 8

Sample run 1

样品运行1

for first, second, third in grouper([1, 7, 3, 5, 6, 8], 3):
    print first, second, third

Output

产量

1 7 3
7 3 5
3 5 6
5 6 8

#4


0  

You could use a zip.

你可以使用拉链。

>>> list(zip(range(5), range(2, 6)))
[(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)]

Just like a zipper, it creates pairs. So, to to mix your two lists, you get:

就像拉链一样,它会产生成对。因此,要混合两个列表,您将得到:

>>> l = [1,7,3,5]
>>> list(zip(l[:-1], l[1:]))
[(1, 7), (7, 3), (3, 5)]

Then iterating goes like

然后迭代就像

for x, y in zip(l[:-1], l[1:]):
    pass

#5


-1  

What's below is very simple/readable and does this job, also likely the most efficient.

下面的内容非常简单/可读并且完成这项工作,也可能是最有效的。

Convert list into generator (or better start with an iterator to star with):

将列表转换为生成器(或者更好地从迭代器开始加注星标):

gen = (x for x in l)

Convert it into pairs:

将其转换为成对:

[(x, gen.next()) for x in gen]

That's all you need.

这就是你所需要的一切。

Of course, better make it a generator too and read from it as you need:

当然,最好把它变成一个发电机并根据需要从中读取:

( (x, gen.next()) for x in gen)

#1


69  

Just use zip

只需使用拉链

>>> l = [1, 7, 3, 5]
>>> for first, second in zip(l, l[1:]):
...     print first, second
...
1 7
7 3
3 5

As suggested you might consider using the izip function in itertools for very long lists where you don't want to create a new list.

正如所建议的那样,您可以考虑在itertools中使用izip函数,用于您不想创建新列表的很长列表。

import itertools

for first, second in itertools.izip(l, l[1:]):
    ...

#2


26  

Look at pairwise at itertools recipes: http://docs.python.org/2/library/itertools.html#recipes

在itertools食谱中看看pairwise:http://docs.python.org/2/library/itertools.html#recipes

Quoting from there:

引自那里:

def pairwise(iterable):
    "s -> (s0,s1), (s1,s2), (s2, s3), ..."
    a, b = tee(iterable)
    next(b, None)
    return izip(a, b)

A General Version

一般版本

A general version, that yields tuples of any given positive natural size, may look like that:

产生任何给定正自然大小的元组的通用版本可能如下所示:

def nwise(iterable, n=2):                                                      
    iters = tee(iterable, n)                                                     
    for i, it in enumerate(iters):                                               
        next(islice(it, i, i), None)                                               
    return izip(*iters)   

#3


7  

I would create a generic grouper generator, like this

我会像这样创建一个通用的石斑鱼发生器

def grouper(input_list, n = 2):
    for i in xrange(len(input_list) - (n - 1)):
        yield input_list[i:i+n]

Sample run 1

样品运行1

for first, second in grouper([1, 7, 3, 5, 6, 8], 2):
    print first, second

Output

产量

1 7
7 3
3 5
5 6
6 8

Sample run 1

样品运行1

for first, second, third in grouper([1, 7, 3, 5, 6, 8], 3):
    print first, second, third

Output

产量

1 7 3
7 3 5
3 5 6
5 6 8

#4


0  

You could use a zip.

你可以使用拉链。

>>> list(zip(range(5), range(2, 6)))
[(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)]

Just like a zipper, it creates pairs. So, to to mix your two lists, you get:

就像拉链一样,它会产生成对。因此,要混合两个列表,您将得到:

>>> l = [1,7,3,5]
>>> list(zip(l[:-1], l[1:]))
[(1, 7), (7, 3), (3, 5)]

Then iterating goes like

然后迭代就像

for x, y in zip(l[:-1], l[1:]):
    pass

#5


-1  

What's below is very simple/readable and does this job, also likely the most efficient.

下面的内容非常简单/可读并且完成这项工作,也可能是最有效的。

Convert list into generator (or better start with an iterator to star with):

将列表转换为生成器(或者更好地从迭代器开始加注星标):

gen = (x for x in l)

Convert it into pairs:

将其转换为成对:

[(x, gen.next()) for x in gen]

That's all you need.

这就是你所需要的一切。

Of course, better make it a generator too and read from it as you need:

当然,最好把它变成一个发电机并根据需要从中读取:

( (x, gen.next()) for x in gen)