목록전체 글 (305)
RUBY
튜플 for문을 이용한 조회 for문을 이용하면 튜플의 아이템을 자동으로 참조할 수 있다. for문을 이용하면 튜플의 아이템을 자동으로 참조할 수 있다. 아래 표와 튜플을 이용해서 학급별 학생 수와 전체 학생 수 그리고 평균 학생수를 출력해보자 studentCnts = (1, 18), (2, 19), (3, 23), (4, 21), (5, 20), (6, 22), (7, 17) sum = 0 avg = 0 for classNo, cnt in studentCnts: print('{}학급 학생수: {}명'.format(classNo, cnt)) sum += cnt print('전체 학생 수: {}명'.format(sum)) print('평균 학생 수: {}명'.format(sum / len(studentCn..
튜플 정렬 튜플은 수정이 불가하기 때문에 리스트로 변환 후 정렬하자. sort() 함수를 이용하면 아이템을 정렬할 수 있다. sorted() 함수를 이용하면 튜플도 정렬할 수 있다 튜플로 정의된 점수표에서 최저 및 최고 점수를 삭제한 후 총점과 평균을 출력해 보자. playerScore = (9.5, 8.9, 9.2, 9.8, 8.8, 9.0) print('playerScore: {}'.format(playerScore)) playerScore = list(playerScore) playerScore.sort() print('playerScore: {}'.format(playerScore)) playerScore.pop(0) playerScore.pop(len(playerScore) - 1) player..
리스트와 튜플 차이점 튜플은 리스트와 달리 아이템 추가, 변경, 삭제가 불가하다. 튜플은 선언 시 괄호 생략이 가능하다. 리스트와 튜플은 자료형 변환이 가능하다. 튜플을 이용한 점수표에서 최저 및 최고 점수를 삭제한 후 총점과 평균을 출력해 보자 playerScore = (9.5, 8.9, 9.2, 9.8, 8.8, 9.0) print('playerScore: {}'.format(playerScore)) maxScore = max(playerScore) minScore = min(playerScore) print(type(playerScore)) def minS(playerScore): return list(filter(lambda x: x > min(playerScore), playerScore)) m..
튜플 슬라이싱[n:m] 리스트와 마찬가지로 [n:m]을 이용하면 리스트에서 원하는 아이템만 뽑아낼 수 있다. 슬라이싱할 때 단계를 설정할 수 있다. numbers = (2, 50, 0.12, 1, 9, 7, 17, 35, 100, 3.14) print('numbers: {}'.format(numbers[2:-2])) print('numbers: {}'.format(numbers[2:-2:2])) print('numbers: {}'.format(numbers[:-2:2])) print('numbers: {}'.format(numbers[::2])) numbers: (0.12, 1, 9, 7, 17, 35) numbers: (0.12, 9, 17) numbers: (2, 0.12, 9, 17) numbers..
튜플 결합 두 개의 튜플을 결합할 수 있다. studentTuple1 = ('홍길동', '박찬호', '이용규') studentTuple2 = ('박승철', '김지은', '강호동') studentTuple3 = studentTuple1 + studentTuple2 print('studentTuple3: {}'.format(studentTuple3)) studentTuple3: ('홍길동', '박찬호', '이용규', '박승철', '김지은', '강호동') 리스트에서 사용할 수 있는 extend()함수를 튜플에서는 사용할 수 없다. studentTuple1 = ['홍길동', '박찬호', '이용규'] studentTuple2 = ['박승철', '김지은', '강호동'] studentTuple1.extend(stud..
튜플 길이 리스트와 마찬가지로, 튜플에 저장된 아이템 개수를 튜플 길이라고 한다. students = ('홍길동', '박찬호', '이용규', '박승철', '김지은') sLength = len(students) print('length of students : {}'.format(sLength)) length of students : 5 len()과 반복문을 이용하면 튜플의 아이템 조회가 가능하다. students = ('홍길동', '박찬호', '이용규', '박승철', '김지은') for i in range(len(students)): print('i : {}'.format(i)) print('students[{}] : {}'.format(i, students[i])) students = ('홍길동', '..
in, not in 키워드 in, not in 키워드를 이용하면 아이템의 존재 유/무를 알 수 있다. studentsTuple = ('홍길동', '박찬호', '이용규', '박승철', '김지은') searchName = input('학생 이름 입력: ') if searchName in studentsTuple: print('{}학생은 우리반 학생입니다.'.format(searchName)) else: print('{}학생은 우리반 학생이 아닙니다.'.format(searchName)) 학생 이름 입력: 김지은 김지은학생은 우리반 학생입니다. 학생 이름 입력: 김보람 김보람학생은 우리반 학생이 아닙니다. in, not in 키워드는 문자열에서도 사용 가능하다. pythonStr = '안녕하세요 저는 김지은입..
튜플(Tuple) 인덱스 튜플도 리스트와 마찬가지로 아이템에 자동으로 부여되는 번호표가 있다. 튜플 아이템은 인덱스를 이용해서 조회 가능하다. students = ('홍길동', '박찬호', '이용규', '박승철', '김지은') print('students[0]: {}'.format(students[0])) print('students[1]: {}'.format(students[1])) print('students[2]: {}'.format(students[2])) print('students[3]: {}'.format(students[3])) print('students[4]: {}'.format(students[4])) students[0]: 홍길동 students[1]: 박찬호 students[2]:..