-
νμ΄μ¬ | #10 SetsμΉ΄ν κ³ λ¦¬ μμ 2023. 10. 20. 21:33
Set
set μμ μμμ λ΄€λ 리μ€νΈ, ννμ²λΌ μ¬λ¬κ°μ νλμ λ³μμ λ΄κ³ μΆμ λ μ¬μ©ν μ μλ μλ£κ΅¬μ‘°μ΄λ€.
CREATE
{} μ€κ΄νΈλ₯Ό μ¬μ©νμ¬ μμ±νλ€.
fruits = {"apple", "banana", "cherry"} print(fruits) #λλ set() μμ±μ μ¬μ© fruits = set(("apple", "banana", "cherry")) print(fruits)
Set μλ£κ΅¬μ‘°λ
1. unordered : μμκ° μλ€. μμκ° μκΈ° λλ¬Έμ index, key λ₯Ό μ΄μ©ν΄μ κ°μ μ½μ μ μλ€.
fruits = {"apple", "banana", "cherry"} print(fruits[0]) #typeError : 'set' object is not subscriptable
2. unchangeable : set μ체λ₯Ό μμ ν μ μλ€.
fruits = {"apple", "banana", "cherry"} fruits[0] = "banana" #TypeError: 'set' object does not support item assignment
3. μ€λ³΅λ κ°μ νμ©νμ§ μλλ€.
fruits = {"apple", "banana", "cherry", "apple"} print(fruits) #{'banana', 'cherry', 'apple'}
len()
fruits = {"apple", "banana", "cherry"} print(len(fruits))
type()
fruits = {"apple", "banana", "cherry"} print(type(fruits))