Python

python - set type

에이미103 2019. 7. 14. 23:24

# python set Type : set
  - set의 가장 큰 특징 : 중복이 없는 저장장소, 순서가 없는 저장구조

my_set = set([1,2,3]) #set생성 => {1,2,3}
print(my_set)
my_set = set("Hello") #text sequence => {"H","e","l","o"}
print(my_set)

# 기본적인 set 연산(교집합, 합집합, 차집합)

s1 = {1,2,3,4,5}
s2 = {4,5,6,7,8}
print(s1 & s2) #교집합(intersection)
print(s1 | s2) #합집합(union)
print(s1 - s2) #차집합(differences)

# 기타 사용가능한 method

my_set = {1,2,3,4,5}
#set에 새로운 요소를 추가
my_set.add(10)
#set에 여러개 추가
my_set.update([7,8,9])
#set에서 삭제
my_set.remove(1)
print(my_set)

 

'Python' 카테고리의 다른 글

python - if문  (0) 2019.07.14
python - Data Type - bool  (0) 2019.07.14
python - dict  (0) 2019.07.14
python - range  (0) 2019.07.14
python - Tuple  (0) 2019.07.14