Iterate
在python中,进行iterate(遍历)是非常遍历的,本文对python中与遍历有关的内容进行总结。
Python语言提供的service
Application
Iterating over every two elements in a list
Iterate a list as pair (current, next) in Python
answer中给出了如下代码:
Here's a relevant example from the itertools module docs:
import itertools
def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = itertools.tee(iterable)
next(b, None)
return zip(a, b)
For Python 2, you need itertools.izip
instead of zip
:
import itertools
def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = itertools.tee(iterable)
next(b, None)
return itertools.izip(a, b)