728x90
ํ์ด์ฌ์ ๋ฆฌ์คํธ๋ ์ซ์๋ ๋ฌธ์๋ ์ฌ๋ฌ ํญ๋ชฉ์ ์์๋๋ก ๊ด๋ฆฌํ๋๋ฐ ์ ์ฉํ ์๋ฃํ์ด๋ค.
๋ฆฌ์คํธ๋ ๋ค๋ชจ ๊ดํธ ์์ ํญ๋ชฉ๋ค์ ๋ฃ์ด ์์ฑํ๋ค. ์ซ์, ๋ฌธ์, ๋ค๋ฅธ ์๋ฃํ ๋ฑ์ ๋ฃ์ ์ ์์ผ๋ฉฐ ์์ด์๋ ์ฌ์ฉ์ด ๊ฐ๋ฅํ๋ค.
# ๋ฆฌ์คํธ๋ ๋ค๋ชจ ๊ดํธ๋ก ํ์ฑํ๋ค
colors = ['red', 'orange', 'yellow', 'green']
๋ฆฌ์คํธ์๋ ์ฌ๋ฌ๊ฐ์ง ๋ด์ ๋ ํจ์๊ฐ ์๋ค.
# ๋ฆฌ์คํธ ๋์ ์ถ๊ฐํ๊ธฐ (1๊ฐ๋ง ๊ฐ๋ฅ)
colors.append('blue') # ['red', 'orange', 'yellow', 'green', 'blue']
# ๋ฆฌ์คํธ ๋์ ์ถ๊ฐํ๊ธฐ (1๊ฐ ์ด์ ๊ฐ๋ฅ)
colors.extend(['blue', 'purple']) # ['red', 'orange', 'yellow', 'green', 'blue', 'purple']
# ๋ฆฌ์คํธ ์ ์ฒด ์ง์ฐ๊ธฐ
colors.clear() # []
# ๋ฆฌ์คํธ ๋ค๋ฅธ ๋ณ์์ ๋ณต์ฌํ๊ธฐ
colors_copy = colors.copy()
# ๋ฆฌ์คํธ์ ํน์ ๊ฐ์ด ๋ช ๊ฐ ์๋์ง ๊ฒ์ํ๊ธฐ
colors.count('red') # 1
# ๋ฆฌ์คํธ์ ํน์ ๊ฐ์ด ๋ช๋ฒ์งธ ์ธ๋ฑ์ค์ ์๋์ง ํ์ธํ๊ธฐ
colors.index('red') # 0 --> ๋ฆฌ์คํธ์ ์ธ๋ฑ์ค๋ 0๋ถํฐ ์์ํ๋ค
# ๋ฆฌ์คํธ ์ง์ ํ ์ธ๋ฑ์ค์ ํน์ ๊ฐ์ ์ถ๊ฐํ๊ธฐ
colors.insert(3, 'light green') # ['red', 'orange', 'yellow', 'light green', 'green']
# ๋ฆฌ์คํธ ์ง์ ํ ์ธ๋ฑ์ค์ ์๋ ๊ฐ์ ์ ๊ฑฐํ๊ธฐ
colors.pop(1) # ['red', 'yellow', 'green']
# ์ธ๋ฑ์ค๋ฅผ ์ง์ ํ์ง ์์ผ๋ฉด ๋ํดํธ๋ก ๋ฆฌ์คํธ ๋์ ์๋ ๊ฐ์ ์ ๊ฑฐ
colors.pop() # ['red', 'orange', 'yellow']
# ๋ฆฌ์คํธ์์ ํน์ ๊ฐ์ ์ ๊ฑฐํ๊ธฐ. ๋ง์ฝ ์ค๋ณต๋๋ ๊ฐ์ด ์๋ค๋ฉด ์ธ๋ฑ์ค๊ฐ ๊ฐ์ฅ ๋ฎ์ ๊ฐ์ ์ ๊ฑฐ
colors.remove('yellow') # ['red', 'orange', 'green']
# ๋ฆฌ์คํธ ์์๋ฅผ ๋ค์ง๊ธฐ
colors.reverse() # ['green', 'yellow', 'orange', 'red']
# ๋ฆฌ์คํธ ์ ๋ ฌํ๊ธฐ
colors.sort() # ['green', 'orange', 'red', 'yellow']
'๐ฑ CS Study > ํ์ด์ฌ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋จ๋ฝ ํ๊ฐ (short-circuit evaluation) (0) | 2022.07.26 |
---|---|
๋น๊ต ์ฐ์ฐ์ vs. ๋ ผ๋ฆฌ ์ฐ์ฐ์ (Comparison vs. Logical operator) (0) | 2022.07.25 |
์์ ๋ณต์ฌ vs. ๊น์ ๋ณต์ฌ (shallow copy vs. deep copy) (0) | 2022.07.18 |
๋์ ๋๋ฆฌ (dictionary) ํจ์ (0) | 2022.07.17 |
๋๊ธ