List Concatenation
test = [[1, 2], [3, 4]]
# sum(test) <- TypeError
sum(test, [])
sum(test) 가 작동하지 않는 이유
sum(iterable = test, start = 0) 을 계산하기 때문에 int와 list를 더할 수 없다는 에러 발생!
대안 1. sum(test, [])
sum(iterable = test, start = []) 를 계산해서 해결할 수 있다.
대안 2. chain
from itertools import chain
test = [[1, 2], [3, 4]]
print(list(chain(*test)))
String Concatenation
test = ['1', '2']
# sum(test) <- TypeError
# sum(test, '') <- TypeError
''.join(test)
일단 sum(test) 는 같은 원리로 작동하지 않음
sum(test, '') 는 대놓고 ''.join 을 쓰라고 명시해줌
'프로그래밍 언어 > Python' 카테고리의 다른 글
파이썬 re (1) | 2022.06.20 |
---|---|
파이썬 reduce (0) | 2022.06.12 |
파이썬 cmp_to_key (0) | 2022.06.10 |
파이썬 any, all (0) | 2022.06.10 |
파이썬 진법 변환 (0) | 2022.06.09 |