Mathlife
Mathlife의 학습 노트
Mathlife
전체 방문자
오늘
어제
  • 분류 전체보기
    • CS
      • 알고리즘
      • 자료구조
      • 운영체제
      • 네트워크
      • 데이터베이스
    • 프로그래밍 언어
      • Java
      • JavaScript
      • C·C++
      • Python
    • Backend
      • Spring
    • Frontend
      • HTML
      • CSS
    • Math
      • Linear Algebra
      • Calculus
    • AI
      • ML
      • DL
      • RL
    • Git

블로그 메뉴

  • 홈
  • 관리
  • 글쓰기
  • 태그
  • 방명록

공지사항

인기 글

태그

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
Mathlife

Mathlife의 학습 노트

파이썬 concatenation
프로그래밍 언어/Python

파이썬 concatenation

2022. 6. 13. 15:01

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
    '프로그래밍 언어/Python' 카테고리의 다른 글
    • 파이썬 re
    • 파이썬 reduce
    • 파이썬 cmp_to_key
    • 파이썬 any, all
    Mathlife
    Mathlife

    티스토리툴바