[자료구조] Tuple의 sort() 함수

신은지·2024년 8월 3일

Data Structure

목록 보기
22/31
post-thumbnail

Tuple 정렬

Tuple은 수정이 불가하기 때문에 List로 변환 후 정렬해야 한다.


sort() 함수

  • sort() 함수를 이용하면 아이템을 정렬할 수 있다.


  • sort() 함수를 이용하면 Tuple도 정렬할 수 있다.

💡Python으로 sort() 함수를 이용해 정렬하기

# Tuple을 이용한 점수표에서 최저, 최고 점수를 삭제한 후 총점과 평균을 출력
'''
점수 1: 9.5,
점수 2: 8.9,
점수 3: 9.2,
점수 4: 9.8,
점수 5: 8.8,
점수 6: 9.0
'''
scoreTuple = (9.5, 8.9, 9.2, 9.8, 8.8, 9.0)
print(type(scoreTuple))
print(scoreTuple)
print('--- Tuple에서 List로 형 변환 ---')
scoreList = list(scoreTuple)
print(type(scoreList))
print(scoreList)
print()

scoreList.sort()
print('최저 점수: {}'.format(scoreList.pop(0)))
print('최고 점수: {}'.format(scoreList.pop(len(scoreList) - 1)))
print('최저/최고 점수 제거 후: {}'.format(scoreList))
print()

print('총점: {}, 평균: {:.2f}'.format(sum(scoreList), (sum(scoreList)/len(scoreList))))





* 이 글은 제로베이스 데이터 스쿨의 강의 자료 일부를 발췌하여 작성되었습니다.

profile
I believe there is no best, only better

0개의 댓글