Home  >  Article  >  Backend Development  >  javascript - 如何Python双阶乘

javascript - 如何Python双阶乘

WBOY
WBOYOriginal
2016-06-06 20:24:411749browse

<code class="php">5! = 5*4*3*2*1 = 120;
print reduce(lambda x,y:x*y,range(1,6))#120
5!! = 5*3*1 = 15;
//这个怎么处理?
</code>

回复内容:

<code class="php">5! = 5*4*3*2*1 = 120;
print reduce(lambda x,y:x*y,range(1,6))#120
5!! = 5*3*1 = 15;
//这个怎么处理?
</code>

<code class="python3">import operator
reduce(operator.mul,range(5,0,-2))</code>

或者 你如你坚持自己写乘法,

<code class="python3">reduce(lambda x,y:x*y,range(5,0,-2))</code>

另外 如果你不想import

<code class="python3">reduce(int.__mul__,range(5,0,-2))</code>

不知道双阶乘的意思 不过你看这个能用吗

<code class="python">reduce(lambda x,y:x*y,range(1,6, 2))</code>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn