[Python] 06. 집합(set) 자료형

2023. 11. 5. 00:26·Programming Language(Sub)/Python

집합(set) 자료형


  • 집합(set)이란?

    • 집합에 관련된 것을 쉽게 처리하기 위해 만든 자료형

    • 순서에 상관없이 임의의 객체들을 담은 집합

    • 순서가 없기 때문에 인덱싱으로 값을 얻을 수 없다.

      • 인덱싱으로 접근하고 싶으면 리스트로 형변환을 해야함
    • 중복을 허용하지 않음

      prof_set = {'Kim', 109, True} # 집합 자료
      prof_set == {'Kim', True, 109} # True(unordered)
      prof_set == {'Kim', 109, True, True} # True(unique)
  • 집합(set)의 생성 방법

    • set() 생성자 함수 사용

      s1 = set([1,2,3])
      s1
      res : {1, 2, 3}
      
      s2 = set("Hello")
      s2
      res : {'e', 'H', 'l', 'o'} # 순서 무관, 중복 허용 X
    • {}를 이용한 기본적인 생성 방법

      s3 = {1, 2, 3, 4, 'q', 'w', True}
      s4 = set() # 빈 set 자료
  • 교집합, 합집합, 차집합 구하기

    • 교집합(intersection)

      s1 = set([1, 2, 3, 4, 5, 6])
      s2 = set([4, 5, 6, 7, 8, 9])
      
      s1 & s2
      res : {4, 5, 6}
      
      s1.intersection(s2)
      res : {4, 5, 6}
    • 합집합(union)

      s1 = set([1, 2, 3, 4, 5, 6])
      s2 = set([4, 5, 6, 7, 8, 9])
      
      s1 | s2
      res : {1, 2, 3, 4, 5, 6, 7, 8. 9}
      
      s1.union(s2)
      res : {1, 2, 3, 4, 5, 6, 7, 8. 9}
    • 차집합(difference)

      s1 = set([1, 2, 3, 4, 5, 6])
      s2 = set([4, 5, 6, 7, 8, 9])
      
      s1 - s2
      res : {1, 2, 3}
      
      s1.difference(s2)
      res : {1, 2, 3}
      
      s2 - s1
      res : {8, 9, 7} # s2 - s1과 s1 - s2의 결과는 다르
      
      s2.difference(s1)
      res : {8, 9, 7}
  • 집합 자료형과 관련된 메서드

    • 값 1개 추가하기(add)

      s1 = set([1, 2, 3])
      s1.add(4)
      s1
      res : {1, 2, 3, 4}
    • 값 여러개 추가하기(update)

      s1 = set([1, 2, 3])
      s1.update([4, 5, 6])
      s1
      res : {1, 2, 3, 4, 5, 6}
    • 특정 값 제거하기(remove)

      s1 = set([1, 2, 3])
      s1.remove(2)
      s1
      res : {1, 3}
    • 집합 복사하기(copy)

      s1 = set([1, 2, 3])
      s2 = s1.copy()
      s2
      res : {1, 2, 3}
    • 집합의 모든 요소 삭제하기(clear)

      s1 = set([1, 2, 3])
      s1.clear()
      print(s1)
      res : set() # s1은 비어있는 set 자료형 변수

참조) Do it! 점프 투 파이썬 (박응용 지음), https://wikidocs.net (파이썬 계단밟기)

'Programming Language(Sub) > Python' 카테고리의 다른 글

[Python] 08. 불(bool) 자료형  (0) 2023.11.05
[Python] 07. 딕셔너리(Dictionary) 자료형  (0) 2023.11.05
[Python] 05. 튜플(Tuple) 자료형  (0) 2023.11.04
[Python] 04. 리스트(list) 자료형  (0) 2023.11.04
[Python] 02. 변수의 선언과 자료형, 연산  (0) 2023.11.04
'Programming Language(Sub)/Python' 카테고리의 다른 글
  • [Python] 08. 불(bool) 자료형
  • [Python] 07. 딕셔너리(Dictionary) 자료형
  • [Python] 05. 튜플(Tuple) 자료형
  • [Python] 04. 리스트(list) 자료형
lumana
lumana
배움을 나누는 공간 https://github.com/bebeis
  • lumana
    Brute force Study
    lumana
  • 전체
    오늘
    어제
    • 분류 전체보기 (456)
      • Software Development (27)
        • Performance (0)
        • TroubleShooting (1)
        • Refactoring (0)
        • Test (8)
        • Code Style, Convetion (0)
        • DDD (0)
        • Software Engineering (18)
      • Java (71)
        • Basic (5)
        • Core (21)
        • Collection (7)
        • 멀티스레드&동시성 (13)
        • IO, Network (8)
        • Reflection, Annotation (3)
        • Modern Java(8~) (12)
        • JVM (2)
      • Spring (53)
        • Framework (12)
        • MVC (23)
        • Transaction (3)
        • AOP (11)
        • Boot (0)
        • AI (0)
      • DB Access (1)
        • Jdbc (1)
        • JdbcTemplate (0)
        • JPA (14)
        • Spring Data JPA (0)
        • QueryDSL (0)
      • Computer Science (129)
        • Data Structure (27)
        • OS (14)
        • Database (10)
        • Network (21)
        • 컴퓨터구조 (5)
        • 시스템 프로그래밍 (23)
        • Algorithm (29)
      • HTTP (8)
      • Infra (1)
        • Docker (1)
      • 프로그래밍언어론 (15)
      • Programming Language(Sub) (77)
        • Kotlin (1)
        • Python (25)
        • C++ (51)
        • JavaScript (0)
      • FE (11)
        • HTML (1)
        • CSS (9)
        • React (0)
        • Application (1)
      • Unix_Linux (0)
        • Common (0)
      • PS (13)
        • BOJ (7)
        • Tip (3)
        • 프로그래머스 (0)
        • CodeForce (0)
      • Book Review (4)
        • Clean Code (4)
      • Math (3)
        • Linear Algebra (3)
      • AI (7)
        • DL (0)
        • ML (0)
        • DA (0)
        • Concepts (7)
      • 프리코스 (4)
      • Project Review (6)
      • LegacyPosts (11)
      • 모니터 (0)
      • Diary (0)
  • 블로그 메뉴

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

  • 공지사항

  • 인기 글

  • 태그

  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.4
lumana
[Python] 06. 집합(set) 자료형
상단으로

티스토리툴바