[python] 파이썬 리스트에서 원소들의 곱을 구하는 방법은?
아래는 파이썬 3에서 리스트의 모든 원소를 곱하는 예제 코드입니다.
import functools
my_list = [1, 2, 3, 4, 5]
product = functools.reduce(lambda x, y: x * y, my_list)
print(product)
위 코드를 실행하면 1부터 5까지의 원소를 곱한 결과인 120이 출력됩니다.
참고 문헌:
- 파이썬 공식 문서: https://docs.python.org/3/library/functools.html#functools.reduce