집합 자료형 함수들

지애·2022년 10월 12일
0
s1 = set([1, 2, 3, 4, 5, 6])
s2 = set([4, 5, 6, 7, 8, 9])

s1. intersection(s2) #교집합
#{4, 5, 6}

s1.union(s2) #합집합
#{1, 2, 3, 4, 5, 6, 7, 8, 9}

s1.difference(s2) #차집합
#{1, 2, 3}

s1.add(7) #값 1개 추가
print(s1)
#{1, 2, 3, 4, 5, 6, 7}

s1.update([6, 7, 8, 9]) #값 여러 개 추가
print(s1)
#{1, 2, 3, 4, 5, 6, 7, 8, 9}

s1.remove(2) #특정 값 제거
print(s1)
#{1, 3, 4, 5, 6, 7, 8, 9}
profile
차근차근

0개의 댓글