我想在Python(2.7)中的iterable上运行几个reduce函数。一个例子是在一个可迭代的整数上调用min
和max
。但是当然你不能在同一个迭代上调用reduce(min, it)
和reduce(max, it)
,因为它在第一次调用后已经耗尽。所以你可能会想做类似的事情:
reduce(lambda a, b: (min(a[0], b[0]), max(a[1], b[1])), ((x, x) for x in it))
你认为,嘿,这很漂亮,所以你把它推广成这样的东西:
from itertools import izip
def multireduce(iterable, *funcs):
""":Return: The tuple resulting from calling ``reduce(func, iterable)`` for each `func` in `funcs`."""
return reduce(lambda a, b: tuple(func(aa, bb) for func, aa, bb in izip(funcs, a, b)), ((item,) * len(funcs) for item in iterable))
(你喜欢单元测试,所以你包括这样的东西:)
import unittest
class TestMultireduce(unittest.TestCase):
def test_multireduce(self):
vecs = (
((1,), (min,), (1,)),
(xrange(10), (min, max), (0, 9)),
(xrange(101), (min, max, lambda x, y: x + y,), (0, 100, (100 * 101) // 2))
)
for iterable, funcs, expected in vecs:
self.assertSequenceEqual(tuple(multireduce(iterable, *funcs)), expected)
但是你尝试了,你意识到它真的慢:
%timeit reduce(min, xrange(1000000)) ; reduce(max, xrange(1000000))
10 loops, best of 3: 140 ms per loop
%timeit reduce(lambda a, b: (min(a[0], b[0]), max(a[1], b[1])), ((x, x) for x in xrange(1000000)))
1 loop, best of 3: 682 ms per loop
%timeit multireduce(xrange(1000000), min, max)
1 loop, best of 3: 1.99 s per loop
哎哟。那么你来Stack Overflow寻找Python优化智慧......
答案 0 :(得分:1)
嗯,就是这样,哪种方式会破坏迭代的重点......
def multireduce(iterable, *funcs):
""":Return: The tuple resulting from calling ``reduce(func, iterable)`` for each `func` in `funcs`."""
return tuple(imap(reduce, funcs, tee(iterable, len(funcs))))
但我的测试用例非常快:
%timeit multireduce(xrange(1000000), min, max)
10 loops, best of 3: 166 ms per loop