Development/Python & Django
[파이썬, Python] int형 list join 적용 - TypeError: sequence item 0: expected str instance, int found
컴공 K
2021. 9. 22. 00:01
728x90
int형 list join 적용
int형 리스트를 join을 통해 합치려고 할때, 문자열 리스트를 join할때처럼 아래와 같이 한다면 TypeError: sequence item 0: expected str instance, int found라는 에러가 발생할 것이다.
num = [1, 3, 5, 7]
num_str = ", ".join(num) # 에러 발생
리스트 타입이 숫자인 경우에는 아래와 같이 map(str, 리스트이름)을 사용해서 리스트를 문자열로 합칠 수 있다.
num = [1, 3, 5, 7]
num_str = ", ".join(map(str,num))
# 1, 3, 5, 7
https://hyesun03.github.io/2017/04/08/python_int_join/
python int형 리스트 join하기
output 출력 할 때 리스트를 적당히 다듬어서 출력할 일이 종종 있다. 계속 까먹어서 적어둠.
hyesun03.github.io
728x90
반응형