迭代从给定列表中的所有连续项。

时间:2022-08-15 19:23:48

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 some builtin python iterators? I'm sure the itertools module should have a solution, but I just can't figure it out...

我想用更紧凑的方式来做,比如在someiterator(l)中(x,y):…有没有一种方法可以使用一些builtin python迭代器来实现?我确定itertools模块应该有一个解决方案,但我就是搞不懂。

5 个解决方案

#1


64  

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.

正如建议的那样,您可以考虑在迭代工具中使用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#菜谱。

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


6  

I would create a generic grouper generator, like this

我会创建一个通用的grouper生成器,像这样。

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


64  

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.

正如建议的那样,您可以考虑在迭代工具中使用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#菜谱。

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


6  

I would create a generic grouper generator, like this

我会创建一个通用的grouper生成器,像这样。

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)