"본 글은 '2019-09-26' 최초 작성되었으며, 업데이트 발생 시 글 내용 일부가 수정될 수 있음" |
Step 1. 파이썬/Python 테스트 환경 조성
1.1. 파이썬/Python 프로그램 실행
1.2. 파이썬/Python 프로그램 실행 화면
Step 2. 소스 코드 작성
2.1. 소스 코드 내용
#2차원 리스트 생성
list_2d = [[1,2],[3,4],[5,6]]
#1차원 리스트로 변환
#방법1 - sum 함수
list_1d = sum(list_2d, [])
#출력 값
#[1, 2, 3, 4, 5, 6]
2.2. 소스 코드 실행 결과
2.2. 그 외 방법
#방법2 - itertools > chain > from_iterable
import itertools
list_1d = list(itertools.chain.from_iterable(list_2d))
#출력 값
#[1, 2, 3, 4, 5, 6]
#방법3 - itertools > chain > *
import itertools
list_1d = list(itertools.chain(*list_2d))
#출력 값
#[1, 2, 3, 4, 5, 6]
#방법4 - reduce 함수1
from functools import reduce
list_1d = list(reduce(lambda x, y: x+y, list_2d))
#출력 값
#[1, 2, 3, 4, 5, 6]
#방법5 - reduce & operator 함수2
from functools import reduce
import operator
list_1d = list(reduce(operator.add, list_2d))
#출력 값
#[1, 2, 3, 4, 5, 6]
마치며..
...더보기
이런 방식으로 앞으로도 "파이썬/Python" "리스트/List"가 내 앞 길을 방해할 시 이렇게 해결 할 수 있을 듯
이 글은 다른분들이 "저 같은 실수와 삽질을 반복하지 않기를 바라면서" 정성스럽게 작성함
삽질 히스토리..
...더보기
1. 왠일로 한 번에 해결했넴
결론..
기초가 중요한 법이여