[python] list - flatten

2024. 1. 2. 20:21·python

알고리즘 문제를 접하다가, python list 중 3차원 리스트를 2차원 리스트로 변환할 경우가 생겼다

 

어찌저찌 구현은 했는데, 좀 더 쉬운 방법을 발견해 정리하고자 작성한다.

(1) 함수 sum (간단하게 사용 가능)

(2) list comprehension 

 

 


 

[1] 목표 설정

# case 1
[[1], [2, 3]] -> [1, 2, 3]

# case 2
[[[(1, 1), (2, 2)], [(3, 3), (4, 4)]]] -> [[(4, 4), (1, 1), (3, 3), (2, 2)]]

# case 3
([1], [2, 3]) -> [1, 2, 3]

# case 4
[([(1, 1), (2, 2)], [(3, 3), (4, 4)])] -> [[(4, 4), (1, 1), (3, 3), (2, 2)]]

 

# case 1:

   2중 리스트 -> 1중 리스트

 

# case 2: 

    3중 리스트 -> 2중 리스트 

 

# case 3:

    ()를 []로 치환하고 생각했을 때 2중 리스트 -> 1중 리스트

    () = tuple 원소를 풀고 싶을 때

 

# case 4:

    ()를 []로 치환하고 생각했을 때 3중 리스트 -> 2중 리스트

    () = tuple원소를 풀고 싶을 때 

    

 

(tuple을 푸는 것이 중요한 포인트가 아님

<- list, tuple 모두 iterable 객체이며,  iterable객체에 대해 적용 가능한 solution을 살펴볼 것임)

 

 


[2] 코드 작성

(1) 함수 sum

sum(iterable객체, start=0)

 

- iterable 자료형 (list, tuple, dictionary)를 인자로 받고, 해당 객체의 내부 원소들의 합을 반환

- start값 (기본값=0)에서 sum결과값을 더한다

 

iterable 객체가 한 번 unpacking -> sum -> unpack된 자료형으로 반환 함을 주의하자

또한, [1] + [2, 3] = [1, 2, 3] 임에 주의하자 

 

이러한 sum 함수를 응용하여,

sum(리스트, [])를 수행하여 결과를 도출하는 기법을 사용한다.

 

 

# case 1
combinations = [[1], [2, 3]]

print(sum(combinations, []))

# 결과: [1, 2, 3]

 

- [[1], [2, 3]] -> [1], [2, 3] 로 언패킹

- [1], [2, 3]과 []의 sum 진행 -> [1] + [2, 3] + [] = [1, 2, 3]

 

# case 2
combinations = [[[(1, 1), (2, 2)], [(3, 3), (4, 4)]]]

print(sum(sum(combinations, []), [])) 

# 결과: [(1, 1), (2, 2), (3, 3), (4, 4)]

 

- [[[(1, 1), (2, 2)], [(3, 3), (4, 4)]]] -> [[(1, 1), (2, 2)], [(3, 3), (4, 4)]]로 언패킹

- [[(1, 1), (2, 2)], [(3, 3), (4, 4)]] 과 []의 sum 진행 ->

                              [[(1, 1), (2, 2)], [(3, 3), (4, 4)]] + []  = [[(1, 1), (2, 2)], [(3, 3), (4, 4)]]

 

- [[(1, 1), (2, 2)], [(3, 3), (4, 4)]] -> [(1, 1), (2, 2)], [(3, 3), (4, 4)]로 언패킹

- [(1, 1), (2, 2)], [(3, 3), (4, 4)]과 []의 sum 진행 ->

                              [(1, 1), (2, 2)] + [(3, 3), (4, 4)] + [] = [(1, 1), (2, 2), (3, 3), (4, 4)]

 

 

# case 3
combinations = ([1], [2, 3])

print(sum(combinations, []))

# 결과: [1, 2, 3]

 

- ([1], [2, 3]) -> [1], [2, 3] 로 언패킹

- [1], [2, 3] 과 []의 sum 진행 -> [1] + [2, 3]+ []  =  [1, 2, 3]

 

# case 4
combinations = [([(1, 1), (2, 2)], [(3, 3), (4, 4)])]

print(sum(sum(combinations, ()), []))

# 결과: [(1, 1), (2, 2), (3, 3), (4, 4)]

 

- [([(1, 1), (2, 2)], [(3, 3), (4, 4)])] -> ([(1, 1), (2, 2)], [(3, 3), (4, 4)])로 언패킹

- ([(1, 1), (2, 2)], [(3, 3), (4, 4)]) 과 ()의 sum 진행 ->

                               ([(1, 1), (2, 2)], [(3, 3), (4, 4)]) + () =  ([(1, 1), (2, 2)], [(3, 3), (4, 4)])

 

- ([(1, 1), (2, 2)], [(3, 3), (4, 4)]) -> [(1, 1), (2, 2)], [(3, 3), (4, 4)]로 언패킹

- [(1, 1), (2, 2)], [(3, 3), (4, 4)]과 []의 sum 진행 ->

                               [(1, 1), (2, 2)] +  [(3, 3), (4, 4)]+ [] = [(1, 1), (2, 2), (3, 3), (4, 4)]

 

 

(2) list comprehension

# case 1
combinations = [[1], [2, 3]]

unique_elements = []
for combination in combinations:
    flattened_combination = [item for item in combination]
    unique_elements.extend(list(flattened_combination))
print(unique_elements)

# 결과: [1, 2, 3]

 

# case 2
combinations = [[[(1, 1), (2, 2)], [(3, 3), (4, 4)]]]

unique_elements = []
for combination in combinations:
    flattened_combination = [item for sublist in combination for item in sublist]
    unique_elements.extend(list(flattened_combination))
print(unique_elements)

# 결과: [(1, 1), (2, 2), (3, 3), (4, 4)]

 

- 순서 주의: (1) combination의 원소 sublist에 대해, (2) sublist의 원소 item에 대해, (3) item 반환

 

# case 3
combinations = ([1], [2, 3])

unique_elements = []
for combination in combinations:
    flattened_combination = [item for item in combination]
    unique_elements.extend(list(flattened_combination))
print(unique_elements)

# 결과: [1, 2, 3]

 

 

# case 4
combinations = [([(1, 1), (2, 2)], [(3, 3), (4, 4)])] 

unique_elements = []
for combination in combinations:
    flattened_combination = [item for sublist in combination for item in sublist]
    unique_elements.extend(list(flattened_combination))
print(unique_elements)

# 결과: [(1, 1), (2, 2), (3, 3), (4, 4)]

 

 


[3] 결론

 

list를 flattern하는 방법의 가장 중요한 점은 iterable 객체를 unpacking 하는 것이다.

 

(1) 이때 함수 sum은 함수 특성상 1회 자동 unpacking이 수행되고 + 연산을 진행한댜.

또한 list의 + 연산은 예를 들어 [1] + [2, 3] = [1, 2, 3]과 같이 하나의 리스트로 반환함을 기억해야 한다.

 

이것을 사용하면 간편하게 flatten을 수행할 수 있다.

 

(2) 또한 sum이 아니더라도, list comprehension을 통해 iterable객체의 원소를 하나씩 접근해 차원을 바꿀 수도 있다. 

 

 

 

 

'python' 카테고리의 다른 글

[python] 우선순위 큐 PriorityQueue VS heapq  (1) 2024.09.22
[python] list - 2중, 3중 리스트 생성  (2) 2024.01.02
'python' 카테고리의 다른 글
  • [python] 우선순위 큐 PriorityQueue VS heapq
  • [python] list - 2중, 3중 리스트 생성
KyuminKim
KyuminKim
컴퓨터공학과 학생의 이모저모 개발 일지 📝
  • KyuminKim
    이모저모
    KyuminKim
  • 전체
    오늘
    어제
    • 분류 전체보기 (53)
      • 프로젝트 (2)
        • first-blog (2)
      • 클라우드 (22)
        • 도커 (14)
        • 쿠버네티스 (5)
        • AWS (2)
      • 알고리즘 (5)
        • 코드트리 (0)
        • 프로그래머스 (5)
      • 백엔드 (8)
      • 프론트엔드 (2)
      • 보안 (3)
        • 드림핵 (2)
      • python (3)
      • 네트워크 (1)
      • 기타 (6)
        • 2025 프로펙트 부트캠프(1차) | 클라우드 엔.. (0)
        • OSSCA | 2024 오픈소스 컨트리뷰션 아카데.. (0)
        • WIK (0)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    코딩트리조별과제
    recover_your_data
    도커
    apiserver-runtime
    인코딩
    코드트리조별과제
    characterencoding
    티스토리챌린지
    진단평가
    docker
    고랭
    파이썬
    오블완
    쿠버네티스
    코딩테스트
    2024 당근 테크 밋업
    코드트리
    알고리즘
    cannot send an empty message
    도커파일
    DP
    DB
    주간레포트
    자료구조
    amazonlinux
    EC2
    탈퇴구현
    MySQL
    urf8
    character_set_server
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.2
KyuminKim
[python] list - flatten
상단으로

티스토리툴바