λ°μν
π accumulate
λ»μΌλ‘λ μΆμ νλ€?
νλ§λλ‘ λμ λ ν©μ λ½μμ£Όλ λ μ
π΅ μ¬μ©λ²
from itertools import accumulate
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
b = list(accumulate(a))
print(a) # [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(b) # [1, 3, 6, 10, 15, 21, 28, 36, 45, 55]
κ°μ΄ μ μ λ‘ λμ λλ κ²μ λ³Ό μ μλ€.
μ΄κ²μ μ μ¬μ©νλλνλ©΄
μ¬μ€ forλ¬Έ λ§μΌλ‘λ κ°μ λμ ν 리μ€νΈλ₯Ό λ½μ μ μμ§λ§ μλλ©΄μμ ν° μ°¨μ΄κ° λλ€.( νΉν κ°μ΄ 컀μ§λ©΄ 컀μ§μλ‘ )
π΅ μλ μΈ‘μ
1λ² forλ¬Έ
a = [x+1 for x in range(1000000)]
for i in range(1, len(a)):
a[i] += a[i-1]
2λ² accumulate
a = [x+1 for x in range(1000000)]
b = list(accumulate(a))
μ€ν μ½λ | κ° | μλ |
forλ¬Έ | 10λ§ | 0.01894974708557129 |
accumulate | 10λ§ | 0.004986286163330078 |
forλ¬Έ | 1000λ§ | 1.868004560470581 |
accumulate | 1000λ§ | 0.766948938369751 |
accumulateκ° νμ€ν λΉ λ₯Έκ²μ λ³Ό μ μλ€. λ§μ΄ μ¬μ©ν μΌμ μκ² μ§λ§ μμλλ©΄ μ’μ κ² κ°λ€.
λ°μν
'Programming language > Python' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[python] F-String (0) | 2020.09.06 |
---|---|
[python] filter (2) | 2020.08.31 |
[python] defaultdict (0) | 2020.08.29 |
[python] pow, μ κ³±, κ±°λμ κ³±κ³Ό λλ¨Έμ§ (0) | 2020.08.28 |
[python] input, sys.stdin.readline (0) | 2020.08.27 |