# 1. ๋ฆฌ์คํธ ๋ง๋ค๊ธฐ
# ๊ทธ๋ฅ ๋๊ดํธ๋ก ๋ง๋ค๊ธฐ
a = ["hi", 2, 3] # ['hi', 2, 3]
# list() ๋ก ๋ง๋ค๊ธฐ -> ๋น ๋ฆฌ์คํธ๋ง ์์ฑ๊ฐ๋ฅ
b = list() # []
# 2. ๋ฆฌ์คํธ ๋ง์
(๋ฆฌ์คํธ ์ด์ด๋ถํ๊ธฐ)
c = [1, 2, 3]
d = [4, 5]
print(c + d) # [1, 2, 3, 4, 5]
# 3. ๋ฆฌ์คํธ ๊ณฑ์
(๋ฆฌ์คํธ ๋ฐ๋ณต)
e = [6, 7, 8]
print(e * 3) # [6, 7, 8, 6, 7, 8, 6, 7, 8]
print(e * 0) # []
# 4. ์ฌ๋ฌ๊ฐ์ง ๋ฆฌ์คํธ ํจ์๋ค
# list.append(x) : ๋ฆฌ์คํธ์ ๊ฐ ์ถ๊ฐ
f = [2, 3, 4]
f.append(5)
print(f)
# list.insert(a,b) : ํน์ ์์น(a index)์ ๊ฐ ์ถ๊ฐ ( ํ์ฌ a index ์์น์ ๊ฐ์ ์ผ์ชฝ์ ์ฝ์๋จ)
g = [11, 22, 33, 44]
g.insert(2, "kkk")
print(g) # [11, 22, 'kkk', 33, 44]
# list.remove(x) : ํน์ ๊ฐ x ํ๋๋ฅผ ์ญ์ (์ฌ๋ฌ ๊ฐ ์กด์ฌํด๋ ํ๋๋ง ์ญ์ / ๊ฐ์ด ์์ผ๋ฉด ์๋ฌ)
h = [12, 34, 56, 78]
h.remove(34)
print(h) # [12, 56, 78]
# list.pop() : ๋ฆฌ์คํธ ๋ง์ง๋ง ๊ฐ ๋ฆฌํด ํ ์ญ์
i = [1, 2, 3, 4]
print(i.pop(), i) # 4 [1, 2, 3]
# list.extend(list2) : ๋ฆฌ์คํธ์ ๋ค๋ฅธ ๋ฆฌ์คํธ2 ์ฐ๊ฒฐ (ํ๋ผ๋ฏธํฐ ใ
ฃist2 ๋ ๋ฆฌ์คํธํ ์ด์ฌ์ผํ๋ค.)
j = ["a", "b", "c"]
k = ["d", "e"]
j.extend(k)
print(j) # ['a', 'b', 'c', 'd', 'e']
# extend vs (+) ์ฐ์ฐ
"""
extend : list1.extend(list2) ๋ list1 ๊ฐ์ฒด ์์ฒด๋ฅผ ์์ ํ๋ ๊ฒ์.
+ ์ฐ์ฐ : list1 = list2 + list3 ๋ ๊ธฐ์กด ๊ฐ์ฒด๋ฅผ ๋ณ๊ฒฝํ๋ ๊ฒ์ด ์๋๋ผ ๊ธฐ์กด ๊ฐ์ฒด๋ ๊ทธ๋๋ก ์๊ณ ์๋ก์ด ๊ฐ์ฒด๋ฅผ ์์ฑ
"""
# list.copy() : ๋ฆฌ์คํธ ๋ณต์ฌ ( ๊น์ ๋ณต์ฌ )
l = [100, 200, 300]
m = l.copy()
print(l) # [100, 200, 300]
print(m) # [100, 200, 300]
print(id(l)) # 140447714290816
print(id(m)) # 140447714291008 (id ๊ฐ์ด ์๋ก ๋ค๋ฆ => ์ฆ, l๊ณผ m ์ ์๋ก ๋ค๋ฅธ ๋ฆฌ์คํธ์ด๋ค.)
# ์์ ๋ณต์ฌ vs ๊น์ ๋ณต์ฌ
"""
์์ ๋ณต์ฌ : ๋จ์ '=' ์ฌ์ฉ (์ฐธ์กฐ๊ฐ๋
) => ๊ฐ์ ์ฃผ์๋ฅผ ๊ฐ๋ฆฌํค๋ ๋ณ์๊ฐ ํ๋ ์๊ธด ๊ฒ์ผ๋ฟ
๊น์ ๋ณต์ฌ : ์ ์ฒด ์ฌ๋ผ์ด์ฑ(list[:]) or copy() => ์ฃผ์๊ฐ์ด ์์ ๋ค๋ฅธ ๋ณ๊ฐ์ ๋ณ์๊ฐ ์์ฑ
"""
# list.reverse() : ๋ฆฌ์คํธ ๋ค์ง๊ธฐ
n = [1, 2, 3]
n.reverse() # n ์์ฒด๋ฅผ ๋ค์ง๋ ๊ฒ์ด๊ธฐ ๋๋ฌธ์ ๋ฆฌํด๊ฐ์ None ์
print(n) # [3, 2, 1]
# list.sort() : ์ ๋ ฌ(์ค๋ฆ์ฐจ์ ๊ธฐ๋ณธ)
l1 = [300, 200, 500]
l1.sort()
print(l1) # [200, 300, 500]
# ๋ฌธ์์ด๋ ์ฌ์ ์ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌ ๊ฐ๋ฅ
l2 = ["cd", "e", "a", "ab"]
l2.sort()
print(l2) # ['a', 'ab', 'cd', 'e']
# ํ์ง๋ง ์ซ์์ ๋ฌธ์์ด์ด ์์ฌ์์ผ๋ฉด(=๋ฐ์ดํฐ ํ์
์ด ์์ฌ์์ผ๋ฉด) ์ ๋ ฌ ๋ถ๊ฐ
# list.count(x) : x ์ ์ผ์นํ๋ ๊ฐ์ด ๋ช ๊ฐ์ธ์ง ์ธ๊ธฐ
l3 = [1, 1, 1, 1, 2, 1, 6, 9]
print(l3.count(1)) # 5
# list.index(x) : x ๊ฐ์ index ๋ฐํ (์ฌ๋ฌ๊ฐ ์๋ค๋ฉด ๋งจ ์ฒ์ index ๋ฐํ)
# list.clear() : ๋ฆฌ์คํธ์ ๋ชจ๋ ๊ฐ ์ญ์
Python
๋ณต์ฌ