05 & 06. Python Basics - Set Types(set), 자료 구조의 변경
5. Set Types
5-(1) set(집합)
<<<set>>>
myset = {"apple", "banana", "cherry"}
중복 안됨, 순서 없음
- 여러 개의 데이터를 집합의 형태로 넣을 수 있음
- set 자료형의 핵심은 요소들의 중복을 허용하지 않음
- 데이터 간 순서가 없기때문에 리스트나 튜플처럼 인덱싱이나 슬라이싱을 이용하여 값을 추출해내는 방법을 사용할 수 없음
A set is a collection which is unordered, unchangeable, and unindexed.
- Unordered:
The items in a set do not have a defined order.
Set items can appear in a different order every time you use them, and cannot be referred to by index or key.
- Unchangeable:
Once a set is created, you cannot change its items, but you can remove items and add new items.
- Duplicates Not Allowed:
Sets cannot have two items with the same value.
thisset = {"apple", "banana", "cherry", "apple", "cherry"}
print(thisset) # {'cherry', 'banana', 'apple'}
5-(2) set의 선언방법
1. my_set = {1, 2, 3}
2. my_set = set([1, 2])
5-(3) set의 연산
java = {"유재석", "김태호", "양세형"}
python = set(["유재석", "박명수"])
▶︎ 교집합
print(java & pyhton)
print(java.intersection(python))
# {"유재석"}
▶︎ 합집합
print(java | python)
print(java.union(python))
# {"김태호", "박명수", "유재석", "양세형"}
▶︎ 차집합
print(java - python)
print(java.difference(python))
# {"김태호", "양세형"}
▶︎ 세트에 하나의 값 추가
python.add("김태호")
print(python)
# {"박명수", "김태호", "유재석"}
▶︎ 세트에 여러 값 추가
python.update(["김태호", "하하", "노홍철"])
print(python)
# {'박명수', '노홍철', '유재석', '김태호', '하하'}
# **update()함수 괄호 안에 [ ]을 사용해서 값을 묶어줘야함**
▶︎ 세트에서 값 제거
java.remove("김태호")
print(java)
# {"양세형", "유재석"}
6. 자료 구조의 변경
LIST -> [ ]
SET -> { }
TUPLE -> ( )
menu = {"coffee", "milk", "juice"}
print(menu, type(menu))
#{'milk', 'coffee', 'juice'} <class 'set'>
menu = list(menu)
print(menu, type(menu))
#['milk', 'coffee', 'juice'] <class 'list'>
menu = tuple(menu)
print(menu, type(menu))
#('milk', 'coffee', 'juice') <class 'tuple'>
menu = set(menu)
print(menu, type(menu))
#{'milk', 'coffee', 'juice'} <class 'set'>
Author And Source
이 문제에 관하여(05 & 06. Python Basics - Set Types(set), 자료 구조의 변경), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@jhwang/05-06.-Python-Basics-Set-Typesset-자료-구조의-변경저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)