add
한 개의 자료형을 추가할 때
my_set = set(['a', 'b', 'c'])
my_set.add('d')
print(my_set) # {'a', 'b', 'c', 'd'}
update
여러 개를 한꺼번에 추가할 때
my_set = set(['a', 'b', 'c'])
my_set.update(['d', 'e', 'f'])
print(my_set) # {'a', 'b', 'c', 'd', 'e', 'f'}
remove
특정 값을 제거할 때
my_set = set(['a', 'b', 'c'])
my_set.remove('b')
print(my_set) # {'a', 'c'}
------------------------------------------------------------------------------------------------------------------------------
<집합 연산>
합집합 ( ' | ' , union )
my_set1 = {'a', 'b', 'c'}
my_set2 = {'d', 'e', 'f'}
my_set1 | my_set2
# {'a', 'b', 'c', 'd', 'e', 'f'}
my_set1.union(my_set2)
# {'a', 'b', 'c', 'd', 'e', 'f'}
교집합 ( ' & ' , intersection )
my_set1 = {'a', 'b', 'c', 'd'}
my_set2 = {'b', 'c', 'd', 'e', 'f'}
my_set1 & my_set2
# {'b', 'c', 'd'}
my_set1.intersection(my_set2)
# {'b', 'c', 'd'}
차집합 ( ' - ' , difference )
my_set1 = {'a', 'b', 'c', 'd'}
my_set2 = {'b', 'c', 'd', 'e', 'f'}
my_set1 - my_set2
# {'a'}
my_set1.difference(my_set2)
# {'a'}
my_set2 - my_set1
# {'e', 'f'}
my_set2.difference(my_set1)
# {'e', 'f'}
대칭차집합 ( ' ^ ' , symmetric_difference ) ( (A-B) | (B-A) )
my_set1 = {'a', 'b', 'c', 'd'}
my_set2 = {'b', 'c', 'd', 'e', 'f'}
my_set1 ^ my_set2
# {'a', 'e', 'f}
my_set1.symmetric_difference(my_set2)
# {'a', 'e', 'f}
부분집합 ( ' >= ' , ' <= ' )
my_set1 = {1, 2, 3}
my_set2 = {2, 3}
my_set1 >= my_set2
# True
728x90
'Python' 카테고리의 다른 글
[파이썬] 매개변수의 패킹과 언패킹 (0) | 2024.12.02 |
---|---|
[파이썬] 함수의 활용 (0) | 2024.12.02 |
[파이썬] 리스트 비교 (대소관계 비교) (0) | 2024.10.17 |
[파이썬] 시간 측정하기 (0) | 2024.09.21 |
[파이썬] 대량 입력 처리 방법 (2) | 2024.09.17 |