중복 안됨, 순서 없음 , 추가, 제거 가능

{” “} :중괄호 또는 set([” “]) 사용

 

my_set = {1,2,3,3,3}
print(my_set)    #{1,2,3} 중복안됨

java = {"유재석", "김태호", "양세형"}
python = set(["유재석","박명수"])

#교집합 (java와 python을 모두 할수 있는 개발자)
print(java & python)   #{'유재석'}
print(java.intersection(python))    #{'유재석'}

#합집합 (java 또는 python을 할 수 있는 개발자)
print(java | python)         #{'김태호', '유재석', '박명수', '양세형'}
print(java.union(python))    #순서는 없음 

#차집합 (java는 할 수 있지만 python은 할 줄 모르는 개발자)
print(java - python)       #{'김태호', '양세형'}
print(java.difference(python))

#python을 할 줄 하는 사람이 추가됨
python.add("김태호")
#java를 까먹음
java.remove("김태호")
반응형

'프로그래밍 > Python' 카테고리의 다른 글

16. shuffle , sample  (0) 2023.05.22
15. 자료구조의 변경  (2) 2023.05.22
13. 튜플  (0) 2023.05.22
12. 사전  (0) 2023.05.22
11. 리스트  (0) 2023.05.22

+ Recent posts