(DataStructure & Algorithm) Tuple

์ž„๊ฒฝ๋ฏผยท2023๋…„ 10์›” 1์ผ
1
post-thumbnail
post-custom-banner

๐Ÿ”‘Summarization


  • ํŠœํ”Œ(Tuple)์€ ๋ฆฌ์ŠคํŠธ(List)์™€ ๋น„์Šทํ•˜์ง€๋งŒ ์•„์ดํ…œ ๋ณ€๊ฒฝ ๋ถˆ๊ฐ€ํ•˜๋ฉฐ, ์ถ”๊ฐ€ํ•˜๋ ค๊ณ  ํ•ด ๋ณด๋ฉด ์—๋Ÿฌ(Error)๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.
  • ํŠœํ”Œ(Tuple)์€ ์†Œ๊ด„ํ˜ธ ()๋ฅผ ์ด์šฉํ•ด ์„ ์–ธํ•˜๊ณ , ๋ฐ์ดํ„ฐ ๊ตฌ๋ถ„์€ ์ฝค๋งˆ,๋ฅผ ์ด์šฉํ•œ๋‹ค. ์„ ์–ธ์‹œ ๊ด„ํ˜ธ ์ƒ๋žต์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
  • ํŠœํ”Œ(Tuple)์€ ์ธ๋ฑ์Šค๋ฅผ ์ด์šฉํ•˜์—ฌ ์กฐํšŒ ๊ฐ€๋Šฅํ•˜๋ฉฐ, ํŠœํ”Œ์€ ๋ฆฌ์ŠคํŠธ์™€ ๋‹ฌ๋ฆฌ ์•„์ดํ…œ ์ถ”๊ฐ€, ๋ณ€๊ฒฝ, ์‚ญ์ œ๊ฐ€ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค.

๐Ÿ“—Contents


ํŠœํ”Œ(Tuple)

๋ฆฌ์ŠคํŠธ(List)์™€ ๊ฐ€์žฅ ํฐ ์ฐจ์ด์ 

โ€ป ๋ฆฌ์ŠคํŠธ(List)์™€ ๋น„์Šทํ•˜์ง€๋งŒ ์•„์ดํ…œ ๋ณ€๊ฒฝ ๋ถˆ๊ฐ€
์ถ”๊ฐ€ํ•˜๋ ค๊ณ  ํ•ด ๋ณด๋ฉด, ์—๋Ÿฌ(Error)๊ฐ€ ๋ฐœ์ƒ

  • ์†Œ๊ด„ํ˜ธ()๋ฅผ ์ด์šฉํ•ด ์„ ์–ธํ•˜๊ณ , ๋ฐ์ดํ„ฐ ๊ตฌ๋ถ„์€ ์ฝค๋งˆ,๋ฅผ ์ด์šฉ
  • ์ˆซ์ž, ๋ฌธ์ž(์—ด), ๋…ผ๋ฆฌํ˜• ๋“ฑ ๋ชจ๋“  ๊ธฐ๋ณธ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ™์ด ์ €์žฅํ•  ์ˆ˜ ์žˆ์Œ
students = ('๋ฅ˜ํ˜„๊ธฐ', '๊น€์ง€ํ˜„', '์„ฑ๋ฉธ์น˜', '์ด๋ฐ•์‚ฌ')
numbers = (10, 20, 30, 40, 50, 60)
strs = (3.14, '์‹ญ', 20, 'one', '3.141592')
  • ๋˜ ๋‹ค๋ฅธ ์ปจํ…Œ์ด๋„ˆ ์ž๋ฃŒํ˜• ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•  ์ˆ˜๋„ ์žˆ์Œ
datas = (10, 20, 30, (40, 50, 60))
  • ํŠœํ”Œ ์•„์ดํ…œ ์กฐํšŒ : ์ธ๋ฑ์Šค๋ฅผ ์ด์šฉํ•˜์—ฌ ์•„์ดํ…œ ์กฐํšŒ ๊ฐ€๋Šฅ
students = ('๋ฅ˜ํ˜„๊ธฐ', '๊น€์ง€ํ˜„', '์„ฑ๋ฉธ์น˜', '์ด๋ฐ•์‚ฌ', '์™ธ๊ตญ์ธ')

for i in range(len(students)):
    if i % 2 == 0:
        print('์ง์ˆ˜ student No. {} : {}' .format(i, students[i]))
    else:

        print('ํ™€์ˆ˜ student No. {} : {}'.format(i, students[i]))
  • in/not in Keyword : ์•„์ดํ…œ ์กด์žฌ ์œ /๋ฌด ํŒ๋‹จ
    • ๋ฌธ์ž์—ด์—์„œ๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
studentsTuple = ('๋ฅ˜ํ˜„๊ธฐ', '๊น€์ง€ํ˜„', '์„ฑ๋ฉธ์น˜', '์ด๋ฐ•์‚ฌ', '์™ธ๊ตญ์ธ')
searchName = input('ํ•™์ƒ ์ด๋ฆ„ ์ž…๋ ฅ: ')

if searchName not in studentsTuple:
    print('{} ํ•™์ƒ์€ ์šฐ๋ฆฌ ๋ฐ˜ ํ•™์ƒ์ด ์•„๋‹™๋‹ˆ๋‹ค.' .format(searchName))
else:
    print('{} ํ•™์ƒ์€ ์šฐ๋ฆฌ ๋ฐ˜ ํ•™์ƒ์ž…๋‹ˆ๋‹ค.'.format(searchName))
  • ํŠœํ”Œ ๊ธธ์ด : ํŠœํ”Œ์— ์ €์žฅ๋œ ์•„์ดํ…œ ๊ฐœ์ˆ˜
students = ('๋ฅ˜ํ˜„๊ธฐ', '๊น€์ง€ํ˜„', '์„ฑ๋ฉธ์น˜', '์ด๋ฐ•์‚ฌ', '์™ธ๊ตญ์ธ')

sLength = len(students)
print('sLength : {}' .format(sLength))
  • ํŠœํ”Œ ๊ฒฐํ•ฉ : ๋ง์…ˆ ์—ฐ์‚ฐ์ž โ€˜+โ€™ ๋ฅผ ์ด์šฉํ•ด ๋‘ ๊ฐœ ์ด์ƒ์˜ ํŠœํ”Œ์„ ๊ฒฐํ•ฉํ•  ์ˆ˜ ์žˆ๋‹ค.
    • ๋ฆฌ์ŠคํŠธ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” extend()ํ•จ์ˆ˜๋ฅผ ํŠœํ”Œ์—์„œ๋Š” ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.
students1 = ('์ด๋ฐ•์‚ฌ', '์™ธ๊ตญ์ธ')
students2 = ('๋ฅ˜ํ˜„๊ธฐ', '๊น€์ง€ํ˜„', '์„ฑ๋ฉธ์น˜', '์™ธ๊ตญ์ธ')

students3 = students1 + students2
print(f'{students3}')
  • ํŠœํ”Œ ์Šฌ๋ผ์ด์‹ฑ : ๋ฆฌ์ŠคํŠธ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ [n:m][n:m]์„ ์ด์šฉํ•ด ์›ํ•˜๋Š” ์•„์ดํ…œ์„ ๋ฝ‘์•„๋‚ผ ์ˆ˜ ์žˆ๋‹ค.
    • ํŠœํ”Œ์€ ์Šฌ๋ผ์ด์‹ฑ์„ ์ด์šฉํ•ด์„œ ์•„์ดํ…œ์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋‹ค.
    • slice() ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด์„œ ์•„์ดํ…œ์„ ์Šฌ๋ผ์ด์‹ฑํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๋ฆฌ์ŠคํŠธ์™€ ํŠœํ”Œ
    • ํŠœํ”Œ์€ ๋ฆฌ์ŠคํŠธ์™€ ๋‹ฌ๋ฆฌ ์•„์ดํ…œ ์ถ”๊ฐ€, ๋ณ€๊ฒฝ, ์‚ญ์ œ๊ฐ€ ๋ถˆ๊ฐ€๋Šฅ
    • ํŠœํ”Œ์€ ์„ ์–ธ ์‹œ ๊ด„ํ˜ธ ์ƒ๋žต์ด ๊ฐ€๋Šฅ
students = '์ด๋ฐ•์‚ฌ', '์™ธ๊ตญ์ธ', '๋ฅ˜ํ˜„๊ธฐ', '๊น€์ง€ํ˜„', '์„ฑ๋ฉธ์น˜', '์™ธ๊ตญ์ธ'
print(f'students : {students}')
print(f'type : {type(students)}')
  • ๋ฆฌ์ŠคํŠธ์™€ ํŠœํ”Œ์€ ์ž๋ฃŒํ˜• ๋ณ€ํ™˜์ด ๊ฐ€๋Šฅ
students = '์ด๋ฐ•์‚ฌ', '์™ธ๊ตญ์ธ', '๋ฅ˜ํ˜„๊ธฐ', '๊น€์ง€ํ˜„', '์„ฑ๋ฉธ์น˜', '์™ธ๊ตญ์ธ'

listStudents = list(students)
print(f'type : {type(listStudents)}')

tupleStudents = tuple(listStudents)
print(f'type : {type(tupleStudents)}')
  • ํŠœํ”Œ ์•„์ดํ…œ ์ •๋ ฌ : ํŠœํ”Œ์€ ์ˆ˜์ •์ด ๋ถˆ๊ฐ€ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ๋ฆฌ์ŠคํŠธ ๋ณ€ํ™˜ ํ›„ ์ •๋ ฌ
    • sort() ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜๋ฉด ์•„์ดํ…œ์„ ์ •๋ ฌํ•  ์ˆ˜ ์žˆ๋‹ค.
students = ('์ด๋ฐ•์‚ฌ', '์™ธ๊ตญ์ธ', '๋ฅ˜ํ˜„๊ธฐ', '๊น€์ง€ํ˜„', '์„ฑ๋ฉธ์น˜', '์™ธ๊ตญ์ธ')
print('students type : {}' .format(type(students)))
print('students : {}' .format(students))
print()

students = list(students) # ํŠœํ”Œ > ๋ฆฌ์ŠคํŠธ
students.sort()
print('students type : {}' .format(type(students)))
print('students : {}' .format(students))
print()

students = tuple(students) # ๋ฆฌ์ŠคํŠธ > ํŠœํ”Œ
print('students type : {}' .format(type(students)))
print('students : {}' .format(students))

sorted() : ํŠœํ”Œ์—์„œ๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ. ๋ฐ˜ํ™˜๋˜๋Š” ๋ฐ์ดํ„ฐ๋Š” โ€˜๋ฆฌ์ŠคํŠธโ€™ ํ˜•์‹์ด๋ฉฐ ์›๋ณธ์€ ์œ ์ง€๋จ

  • for๋ฌธ์„ ์ด์šฉํ•˜์—ฌ ํŠœํ”Œ์˜ ์•„์ดํ…œ์„ ์ž๋™์œผ๋กœ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋‹ค.
cars = '๊ทธ๋žœ์ €', '์†Œ๋‚˜ํƒ€', '์•„์ด์˜ค๋‹‰', '์˜๋ Œํ† ', '์ฝ”๋‚˜'

for i in range(len(cars)):
    print(cars[i])
  • ํŠœํ”Œ ๋‚ด๋ถ€์— ๋˜ ๋‹ค๋ฅธ ํŠœํ”Œ์˜ ์•„์ดํ…œ์„ ์กฐํšŒํ•  ์ˆ˜๋„ ์žˆ๋‹ค.
studentsCnt = ((1, 19), (2, 20), (3, 22), (4, 18), (5, 21))

for i in range(len(studentsCnt)):
    print('{}ํ•™๊ธ‰ ํ•™์ƒ ์ˆ˜ : {} ' .format(studentsCnt[i][0], studentsCnt[i][1]))

for classNo, cnt in studentsCnt:
    print('{}ํ•™๊ธ‰ ํ•™์ƒ ์ˆ˜ : {} ' .format(classno, cnt))
  • while๋ฌธ์„ ์ด์šฉํ•˜์—ฌ ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ์•„์ดํ…œ ์กฐํšŒ ๊ฐ€๋Šฅ
# ๋ฐฉ๋ฒ• 1
cars = ('์ฝ”๋‚˜', 'ํˆฌ์‹ผ', '๊ทธ๋žœ์ €', '์บ์Šคํผ', 'G70', 'G80', 'G90', '์•„์ด์˜ค๋‹‰')

n = 0
while n < len(cars):
    print(cars[n])
    n += 1
# ๋ฐฉ๋ฒ• 2
cars = ('์ฝ”๋‚˜', 'ํˆฌ์‹ผ', '๊ทธ๋žœ์ €', '์บ์Šคํผ', 'G70', 'G80', 'G90', '์•„์ด์˜ค๋‹‰')

n = 0
flag = True
while flag:
    print(cars[n])
    n += 1

    if n == len(cars):
        flag = False
# ๋ฐฉ๋ฒ• 3
cars = ('์ฝ”๋‚˜', 'ํˆฌ์‹ผ', '๊ทธ๋žœ์ €', '์บ์Šคํผ', 'G70', 'G80', 'G90', '์•„์ด์˜ค๋‹‰')

n = 0
flag = True
while True:
    print(cars[n])
    n += 1

    if n == len(cars):
        break
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€