6
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Pythonで総乗(functoolsを使って)

Posted at

たまに使いたくなって,いつも覚えてないのでメモ.

prod.py
>>> import functools
>>> import operator
>>> prod = functools.partial(functools.reduce, operator.mul)
>>> print(prod([1, 2, 3]))
6

Docstring抜粋.

  • partial(func, *args, **keywords) - new function with partial application of the given arguments and keywords.

  • reduce(function, sequence[, initial]) -> value

  • mul(a, b) -- Same as a * b.

functoolsのドキュメント
operatorのドキュメント

numpy使えば単純に

prod.py
>>> import numpy as np
>>> np.prod([1, 2, 3])
6
6
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?