셋
- 중복을 허용하지 않고
- 인덱싱과 슬라이싱 불가능
- 집합연산 용이
생성
set1 = {'a','b','c','d','e'}
print(set1)
set2 = {'a','b','c','d','e','a','a','g','k'}
print(set2)
{'c', 'b', 'a', 'd', 'e'}
{'c', 'g', 'b', 'a', 'd', 'k', 'e'}
{} 로 생성한다. 셋을 생성하면 중복된 원소들은 제거된다.
인덱스 슬라이스 불가
set1[0]
set1[:2]
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-6-c38563f1af7a> in <module>
----> 1 set1[0]
TypeError: 'set' object is not subscriptable
불가능하다.
집합연산
set1 = {'a','b','c','d','e'}
set2= {'c','d','e','f','g'}
print('set1',set1)
print('set2',set2)
print('교집합 ',set1&set2)
print('합집합 ',set1|set2)
print('차집합 ',set2-set1)
print('합집합 - 교집합 ',set1^set2)
{'c', 'b', 'a', 'd', 'e'}
{'c', 'g', 'd', 'f', 'e'}
set1 {'c', 'b', 'a', 'd', 'e'}
set2 {'c', 'g', 'd', 'f', 'e'}
교집합 {'c', 'e', 'd'}
합집합 {'c', 'g', 'b', 'a', 'd', 'f', 'e'}
차집합 {'g', 'f'}
합집합 - 교집합 {'g', 'f', 'b', 'a'}
보통 중복을 없애는데 사용한다
'python 기초' 카테고리의 다른 글
[python기초] tqdm for문 예상속도, time (0) | 2021.12.06 |
---|---|
[ python 기초 ] lambda, map, filter (0) | 2021.08.27 |
[ python 기초 ] 딕셔너리 dictionary (2) | 2021.08.19 |
[ python 기초 ] 튜플 tuple (0) | 2021.08.19 |
[ python 기초 ] 리스트 list (1) | 2021.08.19 |