자료구조 : Set

최조니·2022년 3월 26일
0

Python

목록 보기
6/12

set

  • set
    SET can't have duplicate values
    SET elements don't have index(order)

  • set 생성
    using set() function

    Ex.

    ```
    my_set = set()
    my_set = set([1, 2, 3, 4, 5, 1, 2, 3, 4, 5])
    my_set = set((1, 2, 3, 4, 5, 1, 2, 3, 4, 5))
    # my_set = {1, 2, 3, 4, 5}
    ```

  • 값 추가
    - add(obj) : add an element whose value is 'obj' in that set
    - update({obj, ...}) : add {obj, ...} in that set

    Ex.

    ```
    my_set = set()
    my_set.add(1)
    my_set.add(2)
    my_set.add(3)
    # my_set = {1, 2, 3}
    ##################################3
    my_set.update({4, 5, 6})
    # my_set = {1, 2, 3, 4, 5, 6}
    ```

  • 값 삭제
    - remove(obj) : remove an element whose value is obj in that set (SET doesn't have index, so you can't use del(), pop() function)

    Ex.

    ```
    my_set.remove(5)
    # my_set = {1, 2, 3, 4, 6}
    ```

  • 집합 함수
    - 교집합 : intersection() / &
    - 합집합 : union() / |
    - 차집합 : difference() / -

    Ex.

    ```
    a = {1, 2, 3, 4, 5}
    b = {3, 4, 5, 6, 7}
    # 교집합
    print(a & b)
    print(a.intersection(b))
    #합집합
    print(a | b)
    print(a.union(b))
    #차집합
    print(a - b)
    print(a.difference(b))
    ```

    ㄴ> 출력 값

    ```
    {3, 4, 5}
    {3, 4, 5}
    {1, 2, 3, 4, 5, 6, 7}
    {1, 2, 3, 4, 5, 6, 7}
    {1, 2}
    {1, 2}
    ```
profile
Hello zoni-World ! (◍ᐡ₃ᐡ◍)

0개의 댓글