목록전체 글 (305)
RUBY
튜플(Tuple) 리스트(List)와 비슷하지만 아이템 변경이 불가하다. ‘()’를 이용해서 선언하고, 데이터 구분은 ‘,’를 이용한다. students = ('홍길동', '박찬호', '이용규', '강호동', '박승철', '김지은') numbers = (10, 20, 30, 40, 50, 60) strs = (3.14, '십', 20, 'one', '3.141592') 숫자, 문자(열), 논리형 등 모든 기본 데이터를 같이 저장할 수 있다. datas = (10, 20, 30, (40, 50, 60)) 튜플에 또 다른 컨테이너 자료형 데이터를 저장할 수도 있다.
리스트 곱셈연산 리스트를 곱셈 연산하면 아이템이 반복된다. index() 함수 index(item) 함수를 item의 인덱스를 알아낼 수 있다. 1부터 10까지의 정수가 중복되지 않고 섞여 있을 때 행운의 숫자 7의 위치를 찾자! import random sampleList = random.sample(range(1, 11), 10) selectIdx = int(input('숫자 7의 위치 입력: ')) searchIdx = sampleList.index(7) if searchIdx == selectIdx: print('정답!') else: print('틀렸어요ㅠㅠ') print('sampleList: {}'.format(sampleList)) print('searchIdx: {}'.format(sear..
리스트 슬라이싱[n:m] [n:m]을 이용하면 리스트에서 원하는 아이템만 뽑아낼 수 있다. [n:m]을 이용하면 문자열도 슬라이싱이 가능하다. str = 'abcdefghijklmnopqrstuvwxyz' print('str length: {}'.format(len(str))) print('str: {}, str length: {}'.format(str, len(str))) print('str: {}, str length: {}'.format(str[2:4], len(str))) print('str: {}, str length: {}'.format(str[:4], len(str))) print('str: {}, str length: {}'.format(str[2:], len(str))) print('st..
reverse() 함수 reverse() 함수를 이용하면 아이템을 순서를 뒤집을 수 있다. 다음은 전쟁에서 사용되는 암호이다. 암호를 해독하는 프로그램을 만들어보자. * 먼저 암호의 규칙을 알아보자 → 2 * 7 = 14 → 1 * 5 = 5 → 6 * 2 = 12 → 3 * 1 = 3 즉, 14는 2 * 7 5는 1 * 5 12는 6* 2 3은 3* 1 뒤부터 적어나가면 13326125157214의 순으로 적을 수 있다. secret = '27156231' secretList = [] solvedList = '' for cha in secret: secretList.append(int(cha)) secretList.reverse() val = secretList[0] * secretList[1] sec..
sort() 함수 sort() 함수를 이용하면 아이템을 정렬할 수 있다. 아래 점수표에서 최저 및 최고 점수를 삭제한 후 총점과 평균을 출력해 보자 playerScore = [9.5, 8.9, 9.2, 9.8, 8.8, 9.0] print('playerScore: {}'.format(playerScore)) maxScore = max(playerScore) minScore = min(playerScore) def minS(playerScore): return list(filter(lambda x: x > min(playerScore), playerScore)) minScoreIdx = len(minS(playerScore)) -1 print("playerScore: {}".format(minS(playe..
extend() 함수 extend() 함수를 이용하면 리스트에 또 다른 리스트를 연결(확장)할 수 있다. 덧셈 연산자를 이용해서 리스트를 연결할 수도 있다. 나와 친구가 좋아는 번호를 합치되 번호가 중복되지 않게 하는 프로그램을 만들자 myFavoriteNumbers = [1, 3, 5, 6, 7] friendFavoriteNumbers = [2, 3, 5, 8, 10] print('myFavoriteNumbers: {}'.format(myFavoriteNumbers)) print('friendFavoriteNumbers: {}'.format(friendFavoriteNumbers)) addList = myFavoriteNumbers + friendFavoriteNumbers print('addList ..
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', ..