1. Set1.1 add1.2 update1.3 remove1.4 discard1.5 pop1.6 교집합1.7 합집합1.8 차집합1.9 대칭차집합1.10 issubset1.11 isdisjoint1.12 Symmetric_difference_update
1. Set
집합 자료형은 중복을 허용하지 않으며, 순서가 없는 자료형입니다.
입력
number_set = set([1, 1, 2, 2, 3, 3, 4]) print(number_set)
출력
{1, 2, 3, 4}
위의 예제를 보면 같은 숫자들은 다 제거되고 하나만 남은 것을 볼 수 있습니다.
입력
number_set = set('apple coffee') print(number_set)
출력
{'o', 'e', 'c', ' ', 'l', 'f', 'p', 'a'}
문자열을 넣었을 경우 실행할 때마다 순서가 계속 달라지는 것을 볼 수 있습니다.
1.1 add
집합 자료형에 값을 추가할 때는
add
를 사용합니다.입력
menu = set(['americano', 'cafe latte']) menu.add('ice coffee') print(menu)
출력
{'cafe latte', 'ice coffee', 'americano'}
1.2 update
값을 여러 개를 추가할 때에는
update
를 사용합니다.입력
menu = set(['americano', 'cafe latte']) menu.update(['ice coffee', 'ice tea', 'green tea']) print(menu)
출력
{'ice coffee', 'green tea', 'americano', 'ice tea', 'cafe latte'}
1.3 remove
특정 값을 제거하고 싶을 때에는
remove
를 사용합니다.입력
menu = set(['americano', 'cafe latte']) menu.remove('cafe latte') print(menu)
출력
{'americano'}
1.4 discard
remove처럼 특정 값을 제거하고 싶을 때 사용하는 함수인
discard
도 있습니다.입력
s = {2, 3, 4, 5, 6} s.discard(1)
출력
{2, 3, 4, 5, 6}
discard는 지우려는 값이 없으면 오류를 출력하지 않으며, remove는 오류를 출력합니다. 두 개의 함수를 구분해서 사용해주세요.
1.5 pop
값을 순서대로 하나씩 지우고 싶을 때
pop
을 사용합니다.입력
s = {3, 4, 5, 6, 7, 8} s.pop() s s.pop s s.pop s
출력
{3, 4, 5, 6, 7, 8} {4, 5, 6, 7, 8} {5, 6, 7, 8}
1.6 교집합
집합 자료형은 집합이라는 이름에 맞게 교집합, 합집합, 차집합을 구할 수 있습니다.
교집합
은 &
혹은 intersection 함수
를 사용하여 구합니다.입력
my_cafe = set(['americano', 'water', 'yogurt strawberry blended', 'cafe latte']) other_cafe = set(['americano', 'water', 'green tea']) print(my_cafe & other_cafe) print(my_cafe.intersection(other_cafe))
출력
{'water', 'americano'} {'water', 'americano'}
1.7 합집합
합집합
은 |
또는 union 함수
를 사용하여 구합니다.입력
my_cafe = set(['americano', 'water', 'yogurt strawberry blended', 'cafe latte']) other_cafe = set(['americano', 'water', 'green tea']) print(my_cafe | other_cafe) print(my_cafe.union(other_cafe))
출력
{'americano', 'green tea', 'cafe latte', 'yogurt strawberry blended', 'water'} {'americano', 'green tea', 'cafe latte', 'yogurt strawberry blended', 'water'}
1.8 차집합
차집합
은 -
또는 difference 함수
를 사용하여 구합니다.입력
my_cafe = set(['americano', 'water', 'yogurt strawberry blended', 'cafe latte']) other_cafe = set(['americano', 'water', 'green tea']) print(my_cafe - other_cafe) print(my_cafe.difference(other_cafe))
출력
{'yogurt strawberry blended', 'cafe latte'} {'yogurt strawberry blended', 'cafe latte'}
1.9 대칭차집합
두 개의 집합이 있을 때 공통된 부분을 제외한 나머지 부분을
대칭차집합
이라고 하며, ^
또는 symmetric_difference
함수를 사용합니다.입력
s = {1, 2, 3, 4} ss = {3, 4, 5, 6} s^ss set.symmetric_difference(s, ss)
출력
{1, 2, 5, 6} {1, 2, 5, 6}
1.10 issubset
새로운 값에 원래의 값이 속하는지 확인하고 싶을 때는
issubset 함수
를 사용합니다.입력
s = {1, 2, 3, 4} ss = {3, 4, 5, 6} s.issubset({1, 2}) s.issubset({1, 2, 3, 4, 5, 6}) s < {1, 2, 3, 4, 5, 6} #진부분집합(<->진상위집합) s <= {1, 2, 3, 4, 5, 6} #부분집합(<->상위집합)
출력
False True True True
1.11 isdisjoint
데이터가 서로 겹치는 부분이 있는지 확인하고 싶을 때는
isdisjoint 함수
를 사용합니다.입력
s = {1, 2, 3, 4} ss = {3, 4, 5, 6} s.isdisjoint(ss)
출력
False #겹치는 부분이 있으면 False, 없으면 True가 출력됩니다.
입력
s = {1, 2} ss = {3, 4} s.isdisjoint(ss) 1 in s len(s) #s의 길이를 출력합니다.
출력
True True 2s, ss
1.12 Symmetric_difference_update
지금 셋과 비교할 다른 셋들 중에서 겹치지 않는 요소만 출력하고 싶을 때
symmetric_difference_update함수
를 사용합니다.입력
s = {1, 2, 3} ss = {3, 4, 5} s.symmetric_difference_update(ss) s
출력
{1, 2, 4, 5} #겹치는 요소인 3은 제외된 것을 알 수 있습니다.