Cal*_*laf 4 python performance list python-2.7
运行
L = [1,2,3,4,5,6]
print zip(L,L[1:])[::2]
Run Code Online (Sandbox Code Playgroud)
[(1, 2), (3, 4), (5, 6)]
Run Code Online (Sandbox Code Playgroud)
相反,zip(或其他)语句会产生什么
[1, 2, None, 3, 4, None, 5, 6, None]
Run Code Online (Sandbox Code Playgroud)
?
更新
一开始就没关系
L = [(1,2),(3,4),(5,6)]
Run Code Online (Sandbox Code Playgroud)
只要声明仍然是(快速)单行.
UPDATE2
插入None的一个用例是快速绘制段.
你可以这样做:
>>> L = [1,2,3,4,5,6]
>>> it = zip(*[iter(L)] * 2)
>>> [y for x in it for y in x + (None,)]
[1, 2, None, 3, 4, None, 5, 6, None]
Run Code Online (Sandbox Code Playgroud)
性能和空间复杂性明智@ mgilson的方法如果稍微修改是最好的一个:
>>> from itertools import izip, chain
>>> L = [1,2,3,4,5,6]*10**5
>>> %timeit [y for x in zip(*[iter(L)] * 2) for y in x + (None, )]
10 loops, best of 3: 47.2 ms per loop
Run Code Online (Sandbox Code Playgroud)
如果我们删除列表理解并使用,itertools.chain.from_iterable
那么您可以看到有一个显着的改进:
>>> %timeit list(chain.from_iterable(x + (None,) for x in izip(*[iter(L)] * 2)))
10 loops, best of 3: 31.8 ms per loop
>>> %timeit list(insert_none_while(L)) # mgilson's approach
10 loops, best of 3: 50.7 ms per loop
>>> %timeit list(insert_none_for(L))
10 loops, best of 3: 32.6 ms per loop
Run Code Online (Sandbox Code Playgroud)
这insert_none_while
是@ mgilson的原始代码,insert_none_for
是:
def insert_none_for(iterable):
it = iter(iterable)
for x in it:
yield x
yield next(it)
yield None
Run Code Online (Sandbox Code Playgroud)
@Padraic Cunningham提出的解决方案的略微修改版本似乎是最快的(与@Jochen Ritzel解决方案相比,使用时只有略微的差距itertools.izip
):
>>> L = [1,2,3,4,5,6]*10**6
>>> %timeit [y for x in zip(*[iter(L)] * 2) for y in x + (None, )]
1 loops, best of 3: 541 ms per loop
>>> %timeit list(chain.from_iterable(x + (None,) for x in izip(*[iter(L)] * 2)))
1 loops, best of 3: 349 ms per loop
# Using while 1 and cached next function
>>> %timeit list(insert_none_while_one(L))
1 loops, best of 3: 470 ms per loop
# Cached next function
>>> %timeit list(insert_none_for(L))
1 loops, best of 3: 351 ms per loop
# Jochen Ritzel's original solutions
>>> %timeit it = iter(L); list(itertools.chain.from_iterable(zip(it, it, repeat(None))))
1 loops, best of 3: 352 ms per loop
# Jochen Ritzel's solutions using izip
>>> %timeit it = iter(L); list(itertools.chain.from_iterable(izip(it, it, repeat(None))))
10 loops, best of 3: 167 ms per loop
# Padraic Cunningham's solution using slicing
>>> %timeit list(chain.from_iterable(izip_longest(L[::2],L[1::2],[None])))
1 loops, best of 3: 236 ms per loop
# Padraic Cunningham's solution using iter
>>> %timeit it=iter(L); list(chain.from_iterable(izip_longest(it, it, [])))
10 loops, best of 3: 156 ms per loop
# Kasra
>>> %timeit list(chain(*[L[i:i+2]+[None] for i in range(0,len(L),2)]))
1 loops, best of 3: 1.43 s per loop
Run Code Online (Sandbox Code Playgroud)
还不够好吗?
考虑使用NumPy数组:
>>> arr = np.array(L, dtype=float)
>>> arr.size
6000000
>>> %timeit np.insert(arr.reshape(-1, 2), 2, None, axis=1).ravel()
10 loops, best of 3: 80.8 ms per loop
Run Code Online (Sandbox Code Playgroud)
相关:如何zip(*[iter(s)]*n)
在Python 中工作?
一个简单的生成器会做:
>>> def insert_none(iterable):
... itr = iter(iterable)
... while True:
... yield next(itr)
... yield next(itr)
... yield None
...
>>> list(insert_none([1, 2, 3, 4, 5, 6]))
[1, 2, None, 3, 4, None, 5, 6, None]
>>> list(insert_none([1, 2, 3, 4, 5]))
[1, 2, None, 3, 4, None, 5]
Run Code Online (Sandbox Code Playgroud)
zip
你需要尽可能多的args.itertools.repeat(None)
给你无限的东西:
import itertools
L = [1,2,3,4,5,6]
it = iter(L)
nons = itertools.repeat(None)
pairs = zip(it,it,nons)
Run Code Online (Sandbox Code Playgroud)
另一个开始很简单:
L = [(1,2),(3,4),(5,6)]
pairs = [(a,b,None) for a,b in L]
Run Code Online (Sandbox Code Playgroud)
要展平元组列表:
flat = itertools.chain.from_iterable(pairs)
Run Code Online (Sandbox Code Playgroud)