길/Python

Generators

7he8oy 2021. 2. 12. 14:42
def numbers(x):
    for i in range(x):
        if i % 2 == 0:
            yield i

print(list(numbers(11)))

위 코드는 0 ~ 10 사이의 짝수를 리스트로 만들어 프린트하게 된다.

 

Generator의 이점은 메모리를 적게 차지하는 것이라고 한다.

 

' > Python' 카테고리의 다른 글

itertools  (0) 2021.02.12
decorator  (0) 2021.02.12
List Functions  (0) 2021.02.11
String Functions  (0) 2021.02.11
dic.get(key [,default])  (0) 2021.02.11