๐Ÿ“‚์›น ๊ฐœ๋ฐœ(Web)/๐ŸํŒŒ์ด์ฌ(Python)

Python Tuple ์ด์ •๋ฆฌ

๐Ÿ‘ฉ‍๐ŸŽ“์ธํ…”๋ฆฌ๊ฐ์ž๐Ÿฅ” 2023. 1. 17. 00:35

ํŠœํ”Œ
    ๋‹จ์ผ ๋ณ€์ˆ˜์— ์—ฌ๋Ÿฌ ํ•ญ๋ชฉ์„ ์ €์žฅํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋œ๋‹ค.
    ์ˆœ์„œ๊ฐ€ ์žˆ๊ณ , ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋Š” List
    ๋‘ฅ๊ทผ ๊ด„ํ˜ธ๋กœ ์ž‘์„ฑ๋œ๋‹ค.

1. ํ˜•์‹

thistuple = ("ํ”ผ์นด์ธ„", "๋ผ์ด์ธ„", "ํŒŒ์ด๋ฆฌ")  #ํ•œ ๋ฒˆ ์„ ์–ธํ•˜๋ฉด ๊ฐ’ ๋ณ€๊ฒฝ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค. ๋ฐ‘์— ํŽธ๋ฒ•์„ ์†Œ๊ฐœํ•  ๊ฑฐ๊ธด ํ•˜์ง€๋งŒ..
print(thistuple)


2. ํŠœํ”Œ ๊ธธ์ด
print(len(thistuple))


3. ํ•ญ๋ชฉ ์ ‘๊ทผ
print(thistuple[1])


print(thistuple[-1])


print(thistuple[1:3])



4. ํŠœํ”Œ๊ฐ’ ๋ณ€๊ฒฝ ๋ฐฉ๋ฒ• (์–ต์ง€๋กœ ๋ณ€๊ฒฝํ•˜๋Š” ๋ฒ•)
thistuple = ("ํ”ผ์นด์ธ„", "๋ผ์ด์ธ„", "ํŒŒ์ด๋ฆฌ")
thiscast = list(thistuple) #casting ๋˜๋Š” ํ˜•๋ณ€ํ™˜
thiscast[1] = "์ž ๋งŒ๋ณด"
thistuple = tuple(thiscast) #๋‹ค์‹œ ํŠœํ”Œ๋กœ ๋ฐ”๊พธ๊ธฐ
print(thistuple)

5. ํŠœํ”Œ ์••์ถ•ํ’€๊ธฐ
thistuple = ("ํ”ผ์นด์ธ„", "๋ผ์ด์ธ„", "ํŒŒ์ด๋ฆฌ", "๊ผฌ๋ถ€๊ธฐ")
(p1, p2, p3, p4) = thistuple
print(p1)
print(p2)
print(p3)
print(p4)

6. ๋‘ ๊ฐœ ํŠœํ”Œ ์กฐ์ธ
thistuple1 = ("ํ”ผ์นด์ธ„", "๋ผ์ด์ธ„", "ํŒŒ์ด๋ฆฌ", "๊ผฌ๋ถ€๊ธฐ")
thistuple2 = ("๋ฒ„ํ„ฐํ”Œ", "์•ผ๋„๋ž€", "ํŒŒ์ด๋ฆฌ", "๊ผฌ๋ถ€๊ธฐ")
thistuple3 = thistuple1 + thistuple2
print(thistuple3)