리μ€νΈ μμ±νλ λ² λΆν° μμ , μμ , μ°μ° λ±μ νκΈ° μν΄μ
μμ£Ό! μ¬μ©νλ ν¨μλ€μ λͺ¨λ μ 리ν΄μ μ λ‘λνλ€.
π μμ±
"""
1μ°¨μ 리μ€νΈ μμ±
"""
# λΉμ΄μλ 리μ€νΈ μμ±
a = []
b = list()
print(a, b) # [] []
# λ°λ³΅λ κ° μ μ₯νμ¬ μμ±
c = [0] * 10
print(c) # [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
# λ€μν νμ
μ ν 리μ€νΈμ μ μ₯νλ κ²λ κ°λ₯
d = ['a', 0, True]
# λ²μ μ§μ ν΄μ μμ±
e = list(range(5))
f = list(range(1, 5))
g = list(range(1, 5, 2))
print(e, f, g) # [0, 1, 2, 3, 4] [1, 2, 3, 4] [1, 3]
# map ν¨μ, lambda μ μ¬μ©νμ¬ μμ±
h = list(map(lambda x: x ** 2, range(5)))
print(h) # [0, 1, 4, 9, 16]
"""
λ€μ°¨μ 리μ€νΈ μμ±
"""
# 2μ°¨μ 리μ€νΈ μμ±
a_2 = [[0 for j in range(2)] for i in range(3)]
b_2 = [[0] * 2 for i in range(3)]
print(a_2) # [[0, 0], [0, 0], [0, 0]]
print(b_2) # [[0, 0], [0, 0], [0, 0]]
# 3μ°¨μ 리μ€νΈ μμ±
a_3 = [[[0 for k in range(2)] for j in range(3)] for i in range(2)]
b_3 = [[[0] * 2 for i in range(3)] for i in range(2)]
print(a_3) # [[[0, 0], [0, 0], [0, 0]], [[0, 0], [0, 0], [0, 0]]]
print(b_3) # [[[0, 0], [0, 0], [0, 0]], [[0, 0], [0, 0], [0, 0]]]
"""
ν±λν 리μ€νΈ μμ±
"""
jagged = [[0] * i for i in [1, 3, 2, 5]]
print(jagged) # [[0], [0, 0, 0], [0, 0], [0, 0, 0, 0, 0]]
π μ½μ , μμ , μμ
"""
리μ€νΈ κ° μ½μ
, μμ , μμ (append, insert, extend, remove, map λ±)
"""
a = ['a', 'b']
b = [1, 2, 3]
# 맨 λ€μ μΆκ°
a.append('c')
print(a) # ['a', 'b', 'c']
# λνκΈ° μ°μ°μΌλ‘ 맨 λ€μ μΆκ° (리μ€νΈ νν, κ° νν λͺ¨λ κ°λ₯νλ©°, λ°λλ‘ -μ°μ°μ λΆκ°)
a += ['d']
a += 'e'
print(a) # ['a', 'b', 'c', 'd', 'e']
# μνλ μΈλ±μ€μ κ° μΆκ°
a.insert(0, 'zero')
print(a) # ['zero', 'a', 'b', 'c', 'd', 'e']
# 맨 λ€μ 리μ€νΈ λνκΈ°
a.extend([1, 2])
print(a) # ['zero', 'a', 'b', 'c', 'd', 'e', 1, 2]
# μνλ μμ μ§μ°κΈ°
a.remove('zero')
print(a) # ['a', 'b', 'c', 'd', 'e', 1, 2]
# map ν¨μ, lambda μμ νμ©ν νλ³ν, κ° μμ
print(list(map(lambda x: str(x), b))) # ['1', '2', '3']
print(list(map(str, b))) # ['1', '2', '3']
print(list(map(lambda x: str(x) if x < 2 else x, b))) # ['1', 2, 3]
print(list(map(lambda x: x+1, b))) # [2, 3, 4]
print(list(map(lambda x: list(range(0, x)), b))) # [[0], [0, 1], [0, 1, 2]]
π μ λ ¬
"""
리μ€νΈ μ λ ¬ (sort, sorted, reverse, reversed, slicing)
"""
# sortedλ μ λ ¬λ κ°μ λ°ννμ§λ§, 리μ€νΈ μ체λ₯Ό μ λ ¬νμ§λ μμ
# sortλ μ λ ¬λ κ°μ λ°ννμ§ μμ§λ§, 리μ€νΈ μ체λ₯Ό μ λ ¬ν΄μ€
b = [2, 3, 1]
print(sorted(b)) # [1, 2, 3]
print(sorted(b, reverse = True)) # [3, 2, 1]
print(b) # [2, 3, 1]
print(b.sort()) # None
print(b) # [1, 2, 3]
b.sort(reverse = True)
print(b) # [3, 2, 1]
# reverseλ sortμ κ°μ΄ λ€μ§μ κ°μ λ°ννμ§λ μκ³ , λμ 리μ€νΈ μ체λ₯Ό λ€μ§μ΄μ€
# reversedλ reverseμλ λ¬λ¦¬ listμμ μ 곡νμ§ μλ λ΄μ₯ν¨μμ
# => λ°λΌμ list(reversed(c))μ κ°μ ννλ‘ μ¬μ©ν΄μ£Όμ΄μΌ ν¨ (sortedμ λ§μ°¬κ°μ§λ‘, λ°νλ§ ν λΏ λ¦¬μ€νΈ μ체λ₯Ό μμ νμ§λ μμ)
c = [1, 2, 2, 3]
print(reversed(c)) # <list_reverseiterator object at 0x10b4c6880>
print(list(reversed(c))) # [3, 2, 2, 1]
print(c.reverse()) # None
print(c) # [3, 2, 2, 1]
# μ¬λΌμ΄μ±μ νμ©νλ©΄ list(reversed(c))μ κ°μ ν¨κ³Όλ₯Ό λΌ μ μμ
print(c[::-1]) # [1, 2, 2, 3]
# key ν€μλ
# sort, sorted ν¨μ λͺ¨λ reverse λΏλ§ μλλΌ key 맀κ°λ³μλ₯Ό κ°μ§λ€.
# keyλ μ λ ¬μ λͺ©μ μΌλ‘ νλ ν¨μλ₯Ό κ°μΌλ‘ λ£λλ°, lambdaλ₯Ό μ΄μ©ν μ μλ€.
# keyκ°μ κΈ°μ€μΌλ‘ μ λ ¬λκ³ , κΈ°λ³Έκ°μ μ€λ¦μ°¨μμ΄λ€.
d = ['apple', 'hi', 'hello', 'e']
e = [(2, 'a'), (3, 'b'), (1, 'd'), (1, 'c')]
d.sort(key = lambda x: len(x)) # κ° μμμ κΈΈμ΄λ₯Ό κΈ°μ€μΌλ‘ μ λ ¬
e.sort(key = lambda x: (x[0], x[1])) # ννμ μ μμ κΈ°μ€μΌλ‘ μ λ ¬, κ°μ κ²½μ° λ· μμ κΈ°μ€ μ λ ¬
print(d) # ['e', 'hi', 'apple', 'hello']
print(e) # [(1, 'c'), (1, 'd'), (2, 'a'), (3, 'b')]
π κ²μ, μ°μ°
"""
리μ€νΈ κ²μ, μ°μ° (slicing, all, any, max, min, enumerate, filter, sum, reduce, zip, len λ±)
"""
list_a = [1, 2, 3]
list_b = [1, 0, 0]
list_c = ['a', 'b', 'c']
# νμ΄μ¬μμλ μΈλ±μ€λ‘ -1μ μ
λ ₯νλ©΄ κ°μ₯ λ§μ§λ§ μμλ₯Ό κ°λ¦¬ν΄
print(list_a[-1]) # 3
# μ¬λΌμ΄μ±μ νμ©νλ©΄ 리μ€νΈ μ체λ κ·Έλλ‘ λκ³ , μνλ λ²μμ κ°λ€μ λ°νν΄μ€
print(list_a[0:2]) # [1, 2]
print(list_a[:2]) # [1, 2]
print(list_a[2:]) # [3]
# allμ νλλΌλ 0(False)κ° μμΌλ©΄ Falseλ₯Ό λ°νν΄μ€ (μ λΆ λ€ Trueμ¬μΌ Trueλ‘ μΈμ )
# anyλ νλλΌλ 1(True)κ° μμΌλ©΄ Trueλ₯Ό λ°νν΄μ€ (λͺ κ°λ§ Trueμ¬λ Trueλ‘ μΈμ )
print(all(list_a)) # True
print(all(list_b)) # False
print(any(list_a)) # True
print(any(list_b)) # True
# μ΅λ, μ΅μ
print(max(list_a)) # 3
print(min(list_a)) # 1
# μ΄κ±°νλ ν¨μλ‘, μμκ° μλ μλ£νμ μ
λ ₯μΌλ‘ λ°μμ μΈλ±μ€ κ°μ ν¬ν¨νλ enumerate κ°μ²΄λ₯Ό λ°ν (λ³΄ν΅ for λ¬ΈμΌλ‘ λ§μ΄ μ¬μ©)
for i, name in enumerate(list_c):
print(i, name)
# 0 a
# 1 b
# 2 c
# filter ν¨μ, lambda μμ νμ©ν κ° κ²μ
print(list(filter(lambda x: x == 0, list_b))) # [0, 0]
# λͺ¨λ μμμ ν©
print(sum(list_a)) # 6
# λμ μ°μ° (νμ΄μ¬3μμλ reduce μ¬μ©νλ €λ©΄ μλμκ°μ μ μΈ νμ)
from functools import reduce
print(reduce(lambda x, y: x + y, list_a)) # 6
# λ¬Άμ΄μ£ΌκΈ° (κ°μ μΈλ±μ€ μμλΌλ¦¬ λ¬Άμ΄μ€)
print(list(zip(list_a, list_b))) # [(1, 1), (2, 0), (3, 0)]
print(list(zip(list_a, list_b, list_c))) # [(1, 1, 'a'), (2, 0, 'b'), (3, 0, 'c')]
# κΈΈμ΄
print(len(list_a)) # 3
λκΈ