๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด/Python

ํŒŒ์ด์ฌ ํŠœํ”Œ์˜ ๋ชจ๋“  ๊ฒƒ

by ์„œ์•„๋ž‘๐Ÿ˜ 2023. 12. 3.

 

 

๋“ค์–ด๊ฐ€๋ฉฐ

ํŠœํ”Œ์„ ๋‹ค๋ฃจ๊ธฐ ์ „ ํŒŒ์ด์ฌ ๋ฆฌ์ŠคํŠธ(list)์˜ ๋ชจ๋“  ๊ฒƒ ํฌ์ŠคํŒ…์„ ๋ณด์‹œ๋ฉด ๋”์šฑ ๋„์›€์ด ๋ฉ๋‹ˆ๋‹ค.

ํŠœํ”Œ์€ ๊ฐ’์˜ ์ƒ์„ฑ, ์‚ญ์ œ, ์ˆ˜์ •์ด ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ์ ๋งŒ ์ œ์™ธํ•˜๋ฉด ๋ฆฌ์ŠคํŠธ์™€ ์™„์ „ํžˆ ๋™์ผํ•˜๋ฏ€๋กœ ๊ฐ„๋‹จํ•˜๊ฒŒ๋งŒ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

ํŠœํ”Œ ๋‹ค๋ฃจ๊ธฐ

# ์ธ๋ฑ์‹ฑ
t = (1, 2, 'a', 'b')
t[0]    # 1
t[3]    # 'b'

# ์Šฌ๋ผ์ด์‹ฑ
t[1:]    # (2, 'a', 'b')
p = (3, 4)

# ๋”ํ•˜๊ธฐ
t + p   # (1, 2, 'a', 'b', 3, 4)

# ๊ณฑํ•˜๊ธฐ
p * 3   # (3, 4, 3, 4, 3, 4)

# ๊ธธ์ด ๊ตฌํ•˜๊ธฐ
len(t)    # 4

# ํŠน์ • ๊ฐ’ ํฌํ•จ ์—ฌ๋ถ€
if 1 in t:
	print("1 is in the t")

# ํŠน์ • ๊ฐ’ ํ™•์ธ
t.count(1)    # 1(1๊ฐœ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—)

# ๊ด„ํ˜ธ ์ƒ๋žต
t2 = 5, 6, 7    # ๊ด„ํ˜ธ๋ฅผ ์ƒ๋žตํ•˜๋ฉด ํŒŒ์ด์ฌ์€ ํŠœํ”Œ๋กœ ์ƒ์„ฑ

# ๋ฆฌ์ŠคํŠธ ์ƒํ˜ธ๋ณ€ํ™˜
l1 = list(t2)   # [5,6,7] -> ํŠœํ”Œ๋กœ ๋ฆฌ์ŠคํŠธ ์ƒ์„ฑ
t3 = tuple(l1)   # (5,6,7) -> ๋ฆฌ์ŠคํŠธ๋กœ ํŠœํ”Œ ์ƒ์„ฑ

# ๋”•์…”๋„ˆ๋ฆฌ๋กœ ๋ณ€ํ™˜
t4 = ((1,'a'), (2, 'b'))
d1 = dict(t4)    # {1:'a', 2:'b'} -> ๋”•์…”๋„ˆ๋ฆฌ ์ƒ์„ฑ

 

 

๋Œ“๊ธ€