在Haskell中实现类似以下内容的最惯用方法是什么:
foldl (+) 0 [1,2,3,4,5]
--> 15
或者它在Ruby中的等价物:
[1,2,3,4,5].inject(0) {|m,x| m + x}
#> 15
显然,Python提供了reduce
函数,这是fold的实现,完全如上所述,但是,我被告知'pythonic'编程方式是避免使用lambda
术语和高阶函数,在可能的情况下更喜欢列表推导。因此,有没有一种在Python中折叠列表或类似列表的结构的首选方法,而不是reduce
函数,还是reduce
实现这一目的的惯用方法?
Pythonic对数组求和的方法是sum
。出于其他目的,您有时可以使用reduce
和operator
模块的某种组合,例如:
def product(xs):
return reduce(operator.mul, xs, 1)
请注意,reduce
实际上是一个foldl
,用Haskell术语表示。没有特殊的语法来执行折叠,没有内置的foldr
,实际上使用reduce
和非关联运算符被认为是坏风格。
使用高阶函数是相当pythonic;它充分利用了Python的原则,即一切都是一个对象,包括函数和类。你是对的,一些Python会员不赞同lambdas,但主要是因为当它们变得复杂时它们往往不具有可读性。
哈斯克尔
foldl (+) 0 [1,2,3,4,5]
蟒蛇
reduce(lambda a,b: a+b, [1,2,3,4,5], 0)
显然,这是一个简单的例子来说明一个观点。在Python中你只会做sum([1,2,3,4,5])
甚至Haskell纯粹主义者通常更喜欢sum [1,2,3,4,5]
。
对于没有明显的便利功能的非平凡场景,惯用的pythonic方法是明确地写出for循环并使用可变变量赋值而不是使用reduce
或fold
。
这根本不是功能风格,但那是“pythonic”方式。 Python不是为功能纯粹主义者设计的。了解Python如何支持流控制的异常,以了解非功能性的idiomatic python是如何实现的。
在Python 3中,reduce
已被删除:Release notes。不过你可以使用functools module
import operator, functools
def product(xs):
return functools.reduce(operator.mul, xs, 1)
另一方面,文档表达了对for
-loop而不是reduce
的偏好,因此:
def product(xs):
result = 1
for i in xs:
result *= i
return result
你也可以重新发明轮子:
def fold(f, l, a):
"""
f: the function to apply
l: the list to fold
a: the accumulator, who is also the 'zero' on the first call
"""
return a if(len(l) == 0) else fold(f, l[1:], f(a, l[0]))
print "Sum:", fold(lambda x, y : x+y, [1,2,3,4,5], 0)
print "Any:", fold(lambda x, y : x or y, [False, True, False], False)
print "All:", fold(lambda x, y : x and y, [False, True, False], True)
# Prove that result can be of a different type of the list's elements
print "Count(x==True):",
print fold(lambda x, y : x+1 if(y) else x, [False, True, True], 0)
不是真的回答这个问题,而是用于foldl和foldr的单行:
a = [8,3,4]
## Foldl
reduce(lambda x,y: x**y, a)
#68719476736
## Foldr
reduce(lambda x,y: y**x, a[::-1])
#14134776518227074636666380005943348126619871175004951664972849610340958208L
这个(减少)问题的实际答案是:只需使用循环!
initial_value = 0
for x in the_list:
initial_value += x #or any function.
这将比减少更快,PyPy之类的东西可以像这样优化循环。
顺便说一句,总和案例应该用sum
函数来解决
我可能会很晚才参加派对,但我们可以使用简单的lambda演算和curried函数创建自定义foldr
。这是我在python中实现的foldr。
def foldr(func):
def accumulator(acc):
def listFunc(l):
if l:
x = l[0]
xs = l[1:]
return func(x)(foldr(func)(acc)(xs))
else:
return acc
return listFunc
return accumulator
def curried_add(x):
def inner(y):
return x + y
return inner
def curried_mult(x):
def inner(y):
return x * y
return inner
print foldr(curried_add)(0)(range(1, 6))
print foldr(curried_mult)(1)(range(1, 6))
即使实现是递归的(可能很慢),它也将分别打印值15
和120
启动Python 3.8
,并引入assignment expressions (PEP 572)(:=
运算符),它可以命名表达式的结果,我们可以使用列表理解来复制其他语言称为fold / foldleft / reduce操作的内容:
给定一个列表,一个reduce函数和一个累加器:
items = [1, 2, 3, 4, 5]
f = lambda acc, x: acc * x
accumulator = 1
我们可以用items
折叠f
以获得最终的accumulation
:
[accumulator := f(accumulator, x) for x in items]
# accumulator = 120
或以浓缩形式:
acc = 1; [acc := acc * x for x in [1, 2, 3, 4, 5]]
# acc = 120
请注意,这实际上也是一个“scanleft”操作,因为列表理解的结果表示每个步骤的累积状态:
acc = 1
scanned = [acc := acc * x for x in [1, 2, 3, 4, 5]]
# scanned = [1, 2, 6, 24, 120]
# acc = 120
我相信这个问题的一些受访者已经错过了fold
函数作为抽象工具的更广泛的含义。是的,sum
可以为整数列表做同样的事情,但这是一个微不足道的案例。 fold
更通用。当您拥有一系列不同形状的数据结构并希望干净地表达聚合时,它非常有用。因此,不必每次都使用聚合变量构建一个for
循环并手动重新计算它,fold
函数(或者reduce
似乎对应的Python版本)允许程序员更明确地表达聚合的意图只需提供两件事: