*Programming/[ Py ] Python
[Py - N05] Set (중복제거)
사2다
2022. 8. 28. 19:16
728x90
반응형
개요
- Set은 순서 없는 자료형이고, 중복 없음
- Set은 원소가 존재 유무 검사 or 중복 제거 시 사용
type_set = {'불', '비행', '비행', '비행', '에스퍼', '독', '불', '격투', '땅'}
print(type_set)
#{'격투', '땅', '불', '에스퍼', '독', '비행'}
## check
print('독' in type_set)
#True
print('전기' in type_set)
#False
my_set = {'불', '전기', '비행'}
print(my_set)
#{'전기', '비행', '불'}
print("> OR: ", type_set | my_set)
#> OR: {'격투', '땅', '전기', '에스퍼', '불', '독', '비행'}
print("> AND: ", type_set & my_set)
#> AND: {'비행', '불'}
print("> XOR: ", type_set ^ my_set)
#> XOR: {'격투', '땅', '전기', '에스퍼', '독'}
### 중복제거
## 1. list -> set
test_list = ['에스퍼', '독', '불', '불', '불', '불', '격투', '비행', '땅']
test_set = set(test_list)
print(type(test_set))
# <class 'set'>
print(test_set)
#{'독', '격투', '비행', '에스퍼', '불', '땅'}
## 2. set -> list
test_list = list(test_set)
print(type(test_list))
# <class 'list'>
print(test_list)
#['독', '격투', '비행', '에스퍼', '불', '땅']
728x90