Python 迭代列表中的所有连续项对
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21303224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Iterate over all pairs of consecutive items in a list
提问by flonk
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 itertoolsmodule should have a solution, but I just can't figure it out.
有没有办法使用内置的 Python 迭代器来做到这一点?我确信该itertools模块应该有一个解决方案,但我就是想不通。
采纳答案by sberry
Just use zip
只需使用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 izipfunction in itertoolsfor very long lists where you don't want to create a new list.
正如所建议的,您可以考虑izip在itertools不想创建新列表的非常长的列表中使用该函数。
import itertools
for first, second in itertools.izip(l, l[1:]):
...
回答by Bach
Look at pairwiseat itertools recipes: http://docs.python.org/2/library/itertools.html#recipes
查看pairwiseitertools 食谱: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)
回答by Noctua
You could use a zip.
你可以使用一个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
回答by thefourtheye
I would create a generic groupergenerator, 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
回答by Burak Cetin
If you wanted something inline but not terribly readable here's another solution that makes use of generators. I expect it's also not the best performance wise :-/
如果你想要一些内联但不太可读的东西,这里是另一个使用生成器的解决方案。我希望它也不是最好的表现:-/
Convert list into generator with a tweak to end before the last item:
将列表转换为生成器,并在最后一项之前结束:
gen = (x for x in l[:-1])
Convert it into pairs:
将其转换为对:
[(gen.next(), x) for x in l[1:]]
That's all you need.
这就是你所需要的。
回答by alancalvitti
Generalizing sberry's approach to nwise with comprehension:
概括 sberry 对 nwise 的理解方法:
def nwise(lst, k=2):
return list(zip(*[lst[i:] for i in range(k)]))
Eg
例如
nwise(list(range(10)),3)
[(0, 1, 2), (1, 2, 3), (2, 3, 4), (3, 4, 5), (4, 5, 6), (5, 6, 7), (6, 7, 8), (7, 8, 9)]
[(0, 1, 2), (1, 2, 3), (2, 3, 4), (3, 4, 5), (4, 5, 6), (5, 6, 7), ( 6, 7, 8), (7, 8, 9)]
回答by MisterMiyagi
A simple means to do this without unnecessary copying is a generator that stores the previous element.
无需进行不必要的复制就可以做到这一点的一种简单方法是存储前一个元素的生成器。
def pairs(iterable):
"""Yield elements pairwise from iterable as (i0, i1), (i1, i2), ..."""
it = iter(iterable)
try:
prev = next(it)
except StopIteration:
return
for item in it:
yield prev, item
prev = item
Unlike index-based solutions, this works on any iterable, including those for which indexing is not supported (e.g. generator) or slow (e.g. collections.deque).
与基于索引的解决方案不同,这适用于任何可迭代对象,包括那些不支持索引(例如生成器)或缓慢(例如collections.deque)的可迭代对象。

