starsoi
2014-08-24 19:48:12 +08:00
b=iter(a)
[(x, b.next()) for x in b]
速度比上面的zip快,尤其是当a很大的时候
Python 2.7.3
>>> timeit('zip(a[::2], a[1::2])', setup='a=[1,2,3,4,5,6,7,8]', number=10000)
0.00456690788269043
>>> timeit('[(x, b.next()) for x in b]', setup='a=[1,2,3,4,5,6,7,8];b=iter(a)', number=10000)
0.0014100074768066406
>>> timeit('zip(a[::2], a[1::2])', setup='a=range(100000)', number=10000)
21.61440110206604
>>> timeit('[(x, b.next()) for x in b]', setup='a=range(100000);b=iter(a)', number=10000)
0.022505998611450195