κ΅μ§ν©, ν©μ§ν©, μ°¨μ§ν© μ°μ°λ²
setμ μν μκ°μ λ°°μ λ κ΅μ§ν©, ν©μ§ν©, μ°¨μ§ν©κ³Ό κ°μ μ§ν© μ°μ°μ μνν λ λ§€μ° μ μ©ν©λλ€.
μ΄λ¬ν μ°μ°λ€μ λ°μ΄ν° λΆμ, μκ³ λ¦¬μ¦ μ€κ³, λ‘μ§ κ΅¬ν λ± λ€μν λΆμΌμ μ¬μ©λ©λλ€.
κ΅μ§ν©(Intersection)
λ setμ κ΅μ§ν©μ λ setμ 곡ν΅μΌλ‘ μ‘΄μ¬νλ μμλ€μ μ§ν©μ
λλ€. intersection()
λ©μλλ &
μ°μ°μλ₯Ό μ¬μ©ν μ μμ΅λλ€.
set_a = {1, 2, 3} set_b = {3, 4, 5} intersection = set_a.intersection(set_b) # λλ set_a & set_b print("intersection:", intersection) # {3}
ν©μ§ν©(Union)
λ setμ ν©μ§ν©μ λ setμ λͺ¨λ μμλ₯Ό ν¬ν¨νλ μ§ν©μ
λλ€. union()
λ©μλλ |
μ°μ°μλ₯Ό μ¬μ©ν©λλ€.
set_a = {1, 2, 3} set_b = {3, 4, 5} union = set_a.union(set_b) # λλ set_a | set_b print("union:", union) # {1, 2, 3, 4, 5}
μ°¨μ§ν©(Difference)
νμ΄μ¬μμ μ°¨μ§ν©μ λ μ§ν© μ€ μ²« λ²μ§Έ μ§ν©μλ μνμ§λ§ λ λ²μ§Έ μ§ν©μλ μνμ§ μλ μμλ€μ μ§ν©μ λ°ννλ μ°μ°μ λλ€.
μ°¨μ§ν©μ difference()
λ©μλλ -
μ°μ°μλ₯Ό μ¬μ©ν©λλ€.
set_a = {1, 2, 3} set_b = {3, 4, 5} difference = set_a.difference(set_b) # λλ set_a - set_b print("difference:", difference) # {1, 2}
Guidelines
AI Tutor
Publish
Design
Upload
Notes
Favorites
Help
Code Editor
Execution Result