리μ€νΈμ ννμ μ°¨μ΄μ λμ λ리μ μ μμ°¨μ΄λ₯Ό μμ보μ
a = []
a = list()
b = ()
b = tuple()
a = [1,2,3]
a.append(4)
>>> a=[1,2,3,4]
del a[-1]
>>> a=[1,2,3]
a = (1,2)
a.append(3)
>>> AttributeError: 'tuple' object has no attribute 'append'
del a[0]
>>> TypeError: 'tuple' object doesn't support item deletion
import time
big_list = [num for num in range(1000000)]
big_tuple = tuple([num for num in range(1000000)])
start = time.time()
for num in big_list:
pass
print("list iteration: {}".format(time.time() - start))
start = time.time()
for num in big_tuple:
pass
print("tuple iteration: {}".format(time.time() - start))
>>> list iteration: 0.027995824813842773
>>> tuple iteration: 0.024797916412353516
a = [1,2,3]
b = list(a)
print(a is b)
>>> False
리μ€νΈλ κ°μ²΄λ₯Ό 볡μ¬νκ² λλ©΄ λ€λ₯Έ κ°μ²΄κ° μμ± λκΈ° λλ¬Έμ aμ bμ μμ΄λκ°μ΄ λ€λ₯΄κ² λλ€.
a = (1,2,3)
b = tuple(a)
print(a is b)
>>> True
ννμ κ°μ²΄λ₯Ό 볡μ¬νλ©΄ κ°μ²΄λ₯Ό μ°Έμ‘°νλ μ£Όμλ₯Ό ν λΉνκΈ° λλ¬Έμ μμ΄λ κ°μ΄ λκ°κ² λλ€.
ννμ ν λ² μμ±λλ©΄ μμκ° λ°λμΌμ΄ μκΈ° λλ¬Έμ 볡μ¬λ₯Ό ν΄λ κ°μ²΄λ₯Ό νλ λ μμ±ν΄μ λ©λͺ¨λ¦¬ λλΉλ₯Ό μ€μ΄κ³ μ£Όμλ₯Ό μ°Έμ‘°νλ λ°©μμ μ·¨νλ κ² κ°λ€.
a = dict()
a = {"a": 1}
b = set()
b = {1,2,3}
setκ³Ό dictionaryλͺ¨λ {} μ€κ΄νΈλ₯Ό μ¬μ©νλ€.
νμ§λ§ dictionaryλ key : valueννλ‘ λμ΄μκ³ setμ μλλ€.
setμ dictionaryλ μ€κ΄νΈλ₯Ό μ΄λ€λκ±° λΉΌκ³ λ λΉμ·νμ μ΄ κ±°μ μλ€.
dictionaryλ key: valueμμ μμλ‘ κ°λλ€.
a = {"a" : 1, "b" : 2}
setμ μ€λ³΅μ΄ μλ μμλ€λ‘λ§ μ΄λ£¨μ΄μ Έμλ€.
a = {3,3,2,2,1,1,}
print(a)
>>> {1,2,3}
dictionary ν€λ₯Ό κ°μ§κ³ μ‘°νν μ μλ€.
a = {"a" : 1, "b" : 2}
print(a["a"])
>>> 1
print(a["b"])
>>> 2
ν€λ‘ in μ°μ°μ ν μ μλ€.
a = {"a" : 1, "b" : 2}
print("a" in a)
>>> True
setμ λμ λ리μ²λΌ μΈλ±μ± μ‘°νλ μλκ³ μ€λ‘μ§ inμ°μ°μΌλ‘ ν¬ν¨ μ¬λΆλ§ μ‘°νν μ μλ€.
a = {1,2,3}
print(1 in a)
>>> True
print(4 in a)
>>> False
μ½μ λν keyλ₯Ό κ°μ§κ³ νλ€.
a = {"a" : 1, "b" : 2}
a["c"] = 3
print(a)
>>> a = {"a" : 1, "b" : 2, "c" : 3}
setdefault() ν¨μλ‘λ μΆκ°ν μ μλ€.
ν€ κ°μ΄ μ€λ³΅λλ©΄ μ μ©λμ§ μλλ€.
a = {"a" : 1, "b" : 2}
a.setdefault("c" , 3)
print(a)
>>> a = {"a" : 1, "b" : 2, "c" : 3}
a.setdefault("a" , 2)
>>> a = {"a" : 1, "b" : 2, "c" : 3} -> μ μ©μλ¨
update() ν¨μλ λΉμ·ν κΈ°λ₯μ νμ§λ§ ν€κ°μ΄ μ€λ³΅ λλ©΄ μμ λλ€.
μΈμμ (a=3) μ΄λ κ² μ§μ νλ€. key=value ν€κ°μ λ μ΄νλ₯Ό λΆμ΄μ§ μμλλλ€.
a = {"a" : 1, "b" : 2}
a.update(a=3)
print(a)
>>> a = {"a" : 3, "b" : 2}
μμ λ keyλ₯Ό κ°μ§κ³ νλ€.
a = {"a" : 1, "b" : 2, "c" : 3}
del a["c"]
print(a)
>>> a = {"a" : 1, "b" : 2}
λλ
a = {"a" : 1, "b" : 2, "c" : 3}
pop(a["c"])
print(a)
>>> a = {"a" : 1, "b" : 2}
add(κ°) - μ§ν©μ μλ‘μ΄ κ°μ μΆκ°νλ€. (μ€λ³΅λ κ°μ 무μ)
a = {1,2,3}
a.add(4)
print(a)
>>> {1, 2, 3, 4}
remove(κ°) - μ λ¬λ°μ κ°μ μμ (μμ λ μλ¬ λ©μμ§λ₯Ό μΆλ ₯)
discard(κ°) - μ λ¬λ°μ κ°μ μμ (μμ λ κ·Έλ₯ 무μ)
pop() - μμμ κ°μ 리ν΄νκ³ μμ
clear() - μ§νμ μλ λͺ¨λ κ°μ μμ
copy() - μ§ν©μ 볡μ νμ¬ λ¦¬ν΄