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

pop() 함수 pop() 함수를 이용하면 마지막 인덱스에 해당하는 아이템을 삭제할 수 있다. pop(n) 함수를 n인덱스에 해당하는 아이템을 삭제할 수 있다. 다음은 어떤 체조 선수의 점수표이다. 점수표에서 최고 및 최저 점수를 삭제해보자 playerScore = [9.5, 8.9, 9.2, 9.8, 8.8, 9.0] maxScore = max(playerScore) minScore = min(playerScore) print("playerScore: {}".format(playerScore)) def minS(playerScore): return list(filter(lambda x: x > min(playerScore), playerScore)) minScoreIdx = len(minS(player..

insert() 함수 insert() 함수를 이용하면 특정 위치(인덱스)에 아이템을 추가할 수 있다. students = ['홍길동', '박찬호', '이용규', '박승철', '김지은'] students.insert(3, '강호동') print('students : {}'.format(students)) print('students의 길이 : {}'.format(len(students))) print('students의 마지막 인덱스 : {}'.format(len(students) -1)) students : ['홍길동', '박찬호', '이용규', '강호동', '박승철', '김지은'] students의 길이 : 6 students의 마지막 인덱스 : 5 words = ['Ruby', 'studying', ..

append() 함수 append() 함수를 이용하면 마지막 인덱스에 아이템을 추가할 수 있다. students = ['홍길동', '박찬호', '이용규', '박승철', '김지은'] students.append('강호동') print('students : {}'.format(students)) print('students의 길이 : {}'.format(len(students))) print('students의 마지막 인덱스 : {}'.format(len(students) -1)) students : ['홍길동', '박찬호', '이용규', '박승철', '김지은', '강호동'] students의 길이 : 6 students의 마지막 인덱스 : 5 scores = [['국어', 88], ['영어', 91]] s..
enumerate()함수 enumerate() 함수를 이용하면 아이템을 열거할 수 있다. sports = ['농구', '수구', '축구', '마라톤', '테니스'] for i in range(len(sports)): print('{} : {}'.format(i, sports[i])) sports = ['농구', '수구', '축구', '마라톤', '테니스'] for idx, value in enumerate(sports): print('{} : {}'.format(idx, value)) 0 : 농구 1 : 수구 2 : 축구 3 : 마라톤 4 : 테니스 enumerate() 함수는 문자열에도 적용할 수 있다. str = '안녕하세요 김지은입니다.' for idx, value in enumerate(str):..

리스트와 while문 리스트와 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 그랜저 소나타 말리부..

리스트와 for문 리스트와 for문 for문을 이용하면 리스트의 아이템을 자동으로 참조할 수 있다. students = ['홍길동', '박찬호', '이용규', '박승철', '김지은'] for i in range(len(students)): print(students[i]) students = ['홍길동', '박찬호', '이용규', '박승철', '김지은'] for student in students: print(student) 홍길동 박찬호 이용규 박승철 김지은 for문을 이용하면, 리스트 내부에 또 다른 리스트의 아이템을 조회할 수도 있다. studentCnts = [[1, 19], [2, 20], [3, 22], [4, 18], [5, 21]] for classNo, cnt in studentCnts:..

리스트 길이 리스트 길이 리스트의 아이템 개수를 의미한다. 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 = ['홍길동', '박찬호', '이용규',..

인덱스 인덱스 인덱스란, 아이템에 자동으로 부여되는 번호표, 0부터 시작된다. 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]: 이용규 students[3]: 박승철 students[..