아래 코드는 어떤 문제가 생길까
data = [1, 2, 3]
double_data = map(lambda x: 2*x, data)
min_value = min(double_data)
max_value = max(double_data)
double_data가 빈 리스트란다
ValueError: max() arg is an empty sequence
이와 같은 실수는 map, filter와 같이 generator함수를 사용하는 경우에 발생할 수 있다. generator는 단방향이다. 현재 계산되는 값만 내보내주기 때문에 인덱스로 참조도 불가능하다. 따라서 한 번 순회하고 나면 더 이상 값을 얻어낼 수 없다. 따라서 재사용이 필요하다면 list와 같은 자료구조 형태로 메모리에 저장해두도록 하자.
data = [1, 2, 3]
double_data = list(map(lambda x: 2*x, data))
min_value = min(double_data)
max_value = max(double_data)
'trouble-shooting' 카테고리의 다른 글
TypeError: can't subtract offset-naive and offset-aware datetimes (0) | 2022.11.22 |
---|---|
유니코드 정규화 문제 (NFC, NFD) (0) | 2022.11.21 |
TypeError: Object of type datetime is not JSON serializable (0) | 2022.11.11 |
json.dumps 한글 (0) | 2022.11.09 |
IndexError: list index out of range (0) | 2022.11.08 |