我是 Python 的新手,正在学习语法和各种功能。我想知道是否
x=reduce((lambda x,y: x*y) , [x for x in range(5) if x > 0])
是计算数字阶乘的正确函数吗?
亲切的问候
最佳答案
类似于 http://www.willamette.edu/~fruehr/haskell/evolution.html 的内容
# beginner
def fac(n):
f = 1
i = 1
while i <= n:
f *= i
i += 1
return f
# advanced beginner
def fac(n):
return n * fac(n - 1) if n > 1 else 1
# intermediate
def fac(n):
return reduce(lambda x, y: x * y, range(1, n + 1))
# advanced intermediate
import operator
def fac(n):
return reduce(operator.mul, xrange(1, n + 1))
# professional
import math
print math.factorial(5)
# guru
import scipy.misc as sc
print sc.factorial(5, exact=True)
关于python - Python 中的阶乘函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19491295/