set
- dictionary에서 key만 활용하는 데이터 구조로 이해
- 수학에서의 집합과 동일한 개념->중복x, 순서x
- dict보다 많이쓰지는 않음
a = {1, 1, 2, 3, 3, 4, 1, 5}
print(a)
#중복 나오지 않음
#print(a[0]) <-오류발생. 순서가 없다는 의미!
결과:
{1, 2, 3, 4, 5}
a={}
print(type(a))
#type이 dict로.
#빈 set함수를 생성하려면
b=set()
print(type(b))
결과:
<class 'dict'>
<class 'set'>
- set()으로 집합으로 변환
a = [1, 1, 2, 3, 3, 4, 1, 5]
print(a) #리스트
b = set(a)
print(b) #set이니까 중복이 사라짐
결과:
[1, 1, 2, 3, 3, 4, 1, 5]
{1, 2, 3, 4, 5}
- set operations
- 수학 연산과 동일
- 교집합, 합집합, 차집합 등 지원
a = {1, 2, 3}
b = {2, 3, 4}
print(a.union(b)) # 합집합
print(a.intersection(b)) # 교집합
print(a.difference(b)) # 차집합 a-b
print(a.issubset(b)) #부분 집합 a는 b의 부분집합인가
결과:
{1, 2, 3, 4}
{2, 3}
{1}
False
'IT_Python' 카테고리의 다른 글
[파이썬기초] 반복문. while. break/continue/무한루프 (0) | 2021.12.14 |
---|---|
[파이썬기초] 조건문 if else elif / 중첩 조건문 (0) | 2021.12.13 |
[파이썬기초] 딕셔너리 dictionary. 추가/변경/병합/삭제/확인/조회/튜플 변경. update/del/pop/clear/in/get/items (0) | 2021.12.11 |
[파이썬기초] 튜플 (tuple). 정의/튜플 언패킹 tuple unpacking (0) | 2021.12.10 |
[파이썬기초] 리스트 ( list ) (2). 추가,삭제,검색,정렬. append/extend/insert/remove/pop/index/in/sort/sorted (0) | 2021.12.09 |