0. μ§ν© Set μ΄λ?
μ§ν©μ κ΄λ ¨λ κ²μ μ½κ² μ²λ¦¬νκΈ° μν΄ λ§λ μλ£ν
μ€λ³΅μ νμ©νμ§ μμΌλ©° μμκ° μλ€
μ§ν©μ μμλ‘ λ€μν κ°μ κ°μ§ μ μμ§λ§ κ°μ λ°κΏ μ μκΈ° λλ¬Έμ 리μ€νΈ, λμ λ리, μ§ν©μ μμλ‘ κ°μ§ μ μλ€
( set μμ±μ μμ 리μ€νΈλ₯Ό λ£μΌλ©΄ 리μ€νΈ μμ μμκ° μ§ν©μ μμκ° λλ€ )
μ€κ΄νΈλ₯Ό μ°λ κ²μ΄ λμ λ리λ λΉμ·νμ§λ§ keyκ°μ΄ μμ΄ κ°λ§ μ‘΄μ¬νλ€
# μ§ν©λͺ
= {μμ1, μμ2, μμ3, …}
# SetName = {1, 2, 3}
a = set()
b = {1, 2, 3}
c = {1, 'hi', (2, 5)}
print(c) # {1, (2, 5), 'hi'}
d = {[1, 2, 3, 4]} # μ€λ₯ TypeError: unhashable type: 'list'
d = set([1, 2, 3, 4])
print(d) # {1, 2, 3, 4}
e = set("hello")
print(e) # {'o', 'e', 'h', 'l'}
1. κ΅μ§ν©, ν©μ§ν©, μ°¨μ§ν©
κ΅μ§ν© : & or intersection()
ν©μ§ν© : | or union()
μ°¨μ§ν© : - or difference()
s1 = {1, 2, 3, 4, 5}
s2 = {3, 4, 5, 6, 7}
print(s1 & s2) # {3, 4, 5}
print(s1.intersection(s2)) # {3, 4, 5}
print(s2 & s1) # {3, 4, 5}
print(s2.intersection(s1)) # {3, 4, 5}
print(s1 | s2) # {1, 2, 3, 4, 5, 6, 7}
print(s1.union(s2)) # {1, 2, 3, 4, 5, 6, 7}
print(s2 | s1) # {1, 2, 3, 4, 5, 6, 7}
print(s2.union(s1)) # {1, 2, 3, 4, 5, 6, 7}
print(s1 - s2) # {1, 2}
print(s1.difference(s2)) # {1, 2}
print(s2 - s1) # {6, 7}
print(s2.difference(s1)) # {6, 7}