这是针对这种情况的非 itertools 方法。
首先想象有一个版本的functools.reduce 一次从一个可迭代对象中接收 3 个项目。我们称这个假设函数为reduce3。
如果存在,我们可以这样做:
reduce3(lambda a, b, c: (a+b)*c, numbers)
如果我们要查看这个操作的中间结果,我们会得到类似的结果:
1, 2, 3, 4, 5, 6 # Initial list
9, 4, 5, 6 # Step 1
65, 6 # Step 2
(65 + 6) * ?? # Step 3
所以这几乎就是我们想要的,除了在第 3 步中没有要乘以的第 3 项。事实上,这将发生在任何偶数长度的列表中。好吧,如果它的长度是偶数,我们就在列表中附加一个1:
if not len(numbers) % 2:
numbers.append(1)
在此之后,第三步将是:
(65 + 6)*1
正确答案为 71。
很遗憾,这个神奇的功能并不存在。但是,我们可以修改原始列表以模仿此功能。我们只需要获取数字列表并将连续的数字对(不包括第一个元素)分组为元组。另外,如果列表是偶数长度,我们需要在末尾添加元素1。
本质上,让我们编写一个函数preprocess() 将[1, 2, 3, 4, 5, 6] 变成[1, (2, 3), (4, 5), (6, 1)]。
def preprocess(myList):
my_output = [myList[0], *zip(numbers[1::2], numbers[2::2])]
if not len(myList) % 2:
my_output.append((myList[-1], 1))
return my_output
print(preprocess(numbers))
#[1, (2, 3), (4, 5), (6, 1)]
现在我们可以reduce处理列表:
from functools import reduce
result = reduce(lambda a, b: (a+b[0])*b[1], preprocess(numbers))
print(result)
#71
reducer 接受 2 个输入 - 一个数字和一个元组。它将数字添加到元组的第一个元素,并将结果乘以第二个元素。结果是另一个数字,然后将其传递给下一个reduce操作。
更新
这是reduceN 的一般实现。 N 是由传入函数的长度决定的,所以可以推广到任意数量的函数。
from itertools import islice # couldn't get away from itertools this time
def reduceN(functions, iterable, initializer=None):
it = iter(iterable)
n = len(functions)
if initializer is None:
value = next(it)
else:
value = initializer
elements = list(islice(it, n))
while(elements):
for fn, el in zip(functions, elements):
value = fn(value, el)
elements = list(islice(it, n))
return value
我们可以使用它来循环应用任意数量的函数。所以原来的例子:
from operator import add, mul
numbers = [1, 2, 3, 4, 5, 6]
functions = [add, mul]
print(reduceN(functions, numbers))
#71
如果我们从numbers 中删除最后一个元素:
print(reduceN(functions=functions, iterable=[1, 2, 3, 4, 5]))
#65