ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • set을 이용한 교집합, 차집합, 합집합
    Programing/Python 2019. 6. 7. 05:47

    이번에는 set 모듈에서 사용할 수 있는 기능 중 하나인 교집합, 차집합 그리고 합집합에 대해서 알아보겠습니다.

    union, difference 그리고 intersection 함수를 이용해서 쉽게 구할 수도 있고, ^, -, | operation으로도 해당 집합을 구할 수 있습니다.

    단, set 모듈의 특성상 대상 집합들은 중복되는 element는 가질 수 없습니다.

     
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    >>> a = {1234}
    >>> b = {3456}
    >>> a^b
    {1256}
    >>> a-b
    {12}
    >>> a|b
    {123456}
    >>> a.union(b)
    {123456}
    >>> a.difference(b)
    {12}
    >>> a.intersection(b)
    {34}

    'Programing > Python' 카테고리의 다른 글

    [python] nosetests framework  (0) 2020.06.17
    hex2string  (0) 2020.06.02
    itertools  (0) 2019.06.06
    print each element in a list  (0) 2019.06.06
    [python] string center  (0) 2019.05.09

    댓글

Designed by Tistory.