목록프로그래밍 언어/Python (143)
RUBY

딕셔너리(Dictionary) 키(key)와 값(value)를 이용해서 자료를 관리한다. ‘{ }’를 이용해서 선언하고, ’키:값’의 형태로 아이템을 정의한다. students = {'s1':'홍길동', 's2':'박찬호', 's3':'이용규', 's4':'박승철', 's5':'김지은'} memInfo = {'이름':'홍길동', '메일':'gildong@gmail.com', '학년':3, '취미':['농구','게임']} students1 = {'이름':'홍길동', '메일':'gildong@gmail.com', '학년':3} students2 = {'이름':'박찬호', '메일':'chanho@gmail.com', '학년':2} students3 = {'이름':'이용규', '메일':'yonggyu@gmail..

튜플 while문을 이용한 조회 while문을 이용하면 다양한 방법으로 아이템 조회가 가능하다. cars = ('그랜저', '소나타', '말리부', '카니발', '쏘렌토') n = 0 while n < len(cars): print(cars[n]) n += 1 cars = ('그랜저', '소나타', '말리부', '카니발', '쏘렌토') n = 0 flag = True while flag: print(cars[n]) n += 1 if n == len(cars): flag = False cars = ('그랜저', '소나타', '말리부', '카니발', '쏘렌토') n = 0 while True: print(cars[n]) n += 1 if n == len(cars): break studentCnts = ((1..

튜플 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 = ('홍길동', '..