///
Search

๋ฆฌ์ŠคํŠธ ์‚ฌ์šฉ

๋ถ„๋ฅ˜
์†์„ฑ
์ƒ์„ฑ์ผ
2022/04/04 09:53
์ตœ์ข… ์ˆ˜์ •
2022/04/04 09:54
# 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
๋ณต์‚ฌ