[3주차] 자료구조 5~9

이철민·2023년 2월 15일
0

[리스트 곱셈]

  • 리스트를 곱셈 연산하면 아이템이 반복된다.
students = ['홍길동', '박찬호', '이용규']
print('students: {}'.format(students))

studentsMul = students * 2
print('studentsMul: {}'.format(studentsMul))

[리스트 아이템 위치 찾기]

  • index(item) 함수로 item의 인덱스를 알아낼 수 있다.
    • 같은 item이 2개 이상일 경우 가장 앞쪽에 있는 1개의 인덱스만 찾는다.
students = ['홍길동','강호동', '박찬호', '이용규', '박승철', '강호동', '김지은']
searchIdx = students.index('강호동')
print('searchIdx: {}'.format(searchIdx))

searchIdx = students.index('강호동', 2, 6)    # 인덱스 2부터 5까지의 범위 중에 강호동의 위치를 찾는 것.
print('searchIdx: {}'.format(searchIdx))

------------------------------------------------------

# 실습: 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(searchIdx))

[특정 아이템]

  • 특정 아이템 개수 알아내기
  • count()함수를 이용하면 특정 아이템의 개수를 알아낼 수 있다.
students = ['홍길동','강호동', '박찬호', '이용규', '박승철', '강호동', '김지은']
print('students: {}'.format(students))

searchCnt = students.count('강호동')
print('searchCnt: {}'.format(searchCnt))
# 실습: 하루 동안 헌혈을 진행한 후 혈액형 별 개수를 파악하는 프로그램을 만들어보자.

import random

types = ['A', 'B', 'O', 'AB']
todayData = []
typeCnt = []

for i in range(100):
    type = types[random.randrange(len(types))]    # randrange() 함수는 여기서 0, 1, 2, 3 중에 하나를 추출
    todayData.append(type)

print('todayData: {}'.format(todayData))
print('todayData length: {}'.format(len(todayData)))

for type in types:
    print('{}형: {}개'.format(type, todayData.count(type)))
  • 특정 아이템 삭제
  • del 키워드를 이용하면 특정 아이템을 삭제할 수 있다.
students = ['홍길동', '강호동', '박찬호', '이용규', '박승철', '강호동', '김지은']

del students[1]    # 슬라이싱 기능도 사용 가능
print('students: {}'.format(students))

[튜플]

  • 리스트와 비슷하지만 아이템 변경이 불가능하다. (수정, 삭제 등)
  • ( )을 이용해서 선언하고, 데이터 구분은 ,를 이용한다.
  • 숫자, 문자, 논리형 등 모든 기본 데이터를 같이 저장할 수 있다.
  • 튜플에 또 다른 컨텐이너 자료형 데이터를 저장할 수도 있다.
students = ('홍길동', '박찬호', '이용규', '박승철', '김지은')
print('students: {}'.format(students))
print('students type: {}'.format(type(students)))

numbers = (10, 20, 30, 40, 50)
print('numbers: {}'.format(numbers))

[튜플 아이템 조회]

  • 튜플도 리스트와 마찬가지로 아이템에 자동으로 부여되는 번호표가 있다.
  • 튜플 아이템은 인덱스를 이용해서 조회 가능하다.
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]))
# 실습: 5명의 학생 이름을 튜플에 저장하고 인덱스가 홀수인 학생과 짝수(0포함)인 학생을 구분해서 인덱스와 학생 이름을 출력하자.

students = ('김성예', '신경도', '박기준', '최승철', '황동석')

print('----------- 인덱스 짝수 -----------')
print('students[0]: {}'.format(students[0]))
print('students[2]: {}'.format(students[2]))
print('students[4]: {}'.format(students[4]))

print('----------- 인덱스 홀수 -----------')
print('students[1]: {}'.format(students[1]))
print('students[3]: {}'.format(students[3]))

for i in range(len(students)):
    if i % 2 == 0:
        print('인덱스 짝수: students[{}]: {}'.format(i, students[i]))
    else:
        print('인덱스 홀수: students[{}]: {}'.format(i, students[i]))

[ in과 not in 키워드]

  • 아이템의 존재 유/무를 알 수 있다.
  • in, not in 키워드는 문자열에서도 사용 가능하다.
studentsTuple = ('홍길동', '박찬호', '이용규', '박승철', '김지은')
print('studentsTuple: {}'.format(studentsTuple))

searchName = input('학생 이름 입력: ')

# in
if searchName in studentsTuple:
    print('{} 학생은 우리반 학생입니다.'.format(searchName))
else:
    print('{} 학생은 우리반 학생이 아닙니다.'.format(searchName))

# not in
if searchName not in studentsTuple:
    print('{} 학생은 우리반 학생이 아닙니다.'.format(searchName))
else:
    print('{} 학생은 우리반 학생입니다.'.format(searchName))

[튜플 길이]

  • 리스트와 마찬가지로, 튜플에 저장된 아이템 개수를 튜플 길이라고 한다.
studentsTuple = ('홍길동', '박찬호', '이용규', '박승철', '김지은', '강호동')

sLength = len(studentsTuple)
print('sLength: {}'.format(sLength))
  • len()과 반복문을 이용하면 튜플의 아이템 조회가 가능하다.
# for문
for i in range(len(studentsTuple)):
    print('i: {}'.format(i))
    print('students[{}]: {}'.format(i, studentsTuple[i]))

# while문
n = 0
slength = len(studentsTuple)
while n < slength:
    print('n: {}'.format(n))
    print('students[{}]: {}'.format(n, studentsTuple[n]))
    n += 1

# 기타
for s in studentsTuple:
    print('student: {}'.format(s))

[튜플 결합]

  • 두 개의 튜플을 결합할 수 있다.
    • 그러나 extend()는 불가, + 만 가능
studentsTuple1 = ('홍길동', '박찬호', '이용규')
studentsTuple2 = ('박승철', '김지은', '강호동')

studentsTuple3 = studentsTuple1 + studentsTuple2

print('studentsTuple3: {}'.format(studentsTuple3))
# 실습: 튜플을 이용해서 나와 친구가 좋아하는 번호를 합치되 번호가 중복되지 않도록 하는 프로그램

myNumbers = (1, 3, 5, 6, 7)
friendNumbers = (2, 3, 5, 8, 10)

print('myNumbers: {}'.format(myNumbers))
print('friendNumbers: {}'.format(friendNumbers))

for number in friendNumbers:
    if number not in myNumbers:
        myNumbers = myNumbers + (number, )      # 정수인 number을 튜플로 바꾸는 법 -> (number, )

print('myNumbers: {}'.format(myNumbers))

[튜플 슬라이싱]

  • 리스트와 마찬가지로 [n:m]을 이용하면 리스트에서 원하는 아이템만 뽑아낼 수 있다.
students = ('홍길동', '박찬호', '이용규', '강호동', '박승철', '김지은')
print('students: {}'.format(students))
print('students[2:4]: {}'.format(students[2:4]))
print('students[:4]: {}'.format(students[:4]))
print('students[2:]: {}'.format(students[2:]))
print('students[2:-2]: {}'.format(students[2:-2]))

numbers = (2, 50, 0.12, 1, 9, 7, 17)
print('numbers: {}'.format(numbers))
print('numbers[2:4]: {}'.format(numbers[2:4]))
print('numbers[:4]: {}'.format(numbers[:4]))
print('numbers[2:]: {}'.format(numbers[2:]))
print('numbers[-5:-2]: {}'.format(numbers[-5:-2]))
  • 슬라이싱할 때 단계를 설정할 수 있다.
print('numbers[-5:-2:2]: {}'.format(numbers[2:-2:2]))
  • 튜플은 리스트와는 달리 슬라이싱을 이용해서 아이템을 변경할 수 없다.
    • 원래 선언되어 있는 데이터가 리스트인데 이를 튜플 아이템으로 변경은 가능하다. 데이터 타입은 계속 리스트.
students = ['홍길동', '박찬호', '이용규', '강호동', '박승철', '김지은']
print('students: {}'.format(students))

students[1:4] = ('park chanho', 'lee yonggyu', 'gang hodong')
print('students: {}'.format(students))
  • slice()함수를 이용해서 아이템을 슬라이싱 할 수 있다.
    -> students[2:4] = students[slice(2,4)]

[리스트와 튜플의 차이점]

튜플은 리스트와 달리 아이템 추가, 변경, 삭제가 불가능하다.

  • 튜플은 선언 시 괄호 생략이 가능하다.
students = '홍길동', '박찬호', '이용규', '이대호'
print(students)
print(type(students))
  • 리스트와 튜플은 자료형 변환이 가능하다.
students = ['홍길동', '박찬호', '이용규', '박용택']
print('students: {}'.format(students))
print('students type: {}'.format(type(students)))

students = tuple(students)
print('students: {}'.format(students))
print('students type: {}'.format(type(students)))

students = list(students)
print('students: {}'.format(students))
print('students type: {}'.format(type(students)))
# 실습: 튜플을 이용한 점수표에서 최저, 최고 점수를 삭제한 후 총점과 평균을 출력해보자.

playerScore = (9.5, 8.9, 9.2, 9.8, 8.8, 9.0)
print('playerScore: {}'.format(playerScore))
print('playerScore type: {}'.format(type(playerScore)))

playerScore = list(playerScore)
print('playerScore: {}'.format(playerScore))
print('playerScore type: {}'.format(type(playerScore)))

playerScore.sort()
print('playerScore: {}'.format(playerScore))

playerScore.pop(0)
playerScore.pop(len(playerScore)-1)
print('playerScore: {}'.format(playerScore))

playerScore = tuple(playerScore)
print('playerScore: {}'.format(playerScore))

sum = 0
avg = 0

for score in playerScore:
    sum += score

avg = sum / len(playerScore)

print('총점: %.2f' % sum)
print('평균: %.2f' % avg)

[튜플 정렬]

  • 튜플은 수정이 불가하기 때문에 리스트로 변환 후 정렬
    -> 다시 튜플로 변환
students = ('홍길동', '박찬호', '이용규', '강호동', '박찬호', '김지은')
print('students type: {}'.format(type(students)))
print('students: {}'.format(students))

students = list(students)
print('students: {}'.format(students))

students.sort(reverse=True)
print('students: {}'.format(students))
  • sorted() 함수를 이용하면 튜플도 정렬할 수 있다. -> 정렬 된 리스트로 반한된다.
students = ('홍길동', '박찬호', '이용규', '강호동', '박찬호', '김지은')
sortedStudents = sorted(students)
print('sortedStudents type: {}'.format(type(sortedStudents)))
print('sortedStudents: {}'.format(sortedStudents))

[튜플과 for문]

  • for문을 이용하면 튜플의 아이템을 자동으로 조회할 수 있다.
cars = '그랜저', '쏘나타', '말리부', '카니발', '쏘렌토'

for i in range(len(cars)):
    print(cars[i])

for car in cars:
    print(car)
  • for문을 이용한 내부 튜플 조회
    • for문을 이용하면 튜플의 아이템을 자동으로 참조할 수 있다.
studentCnts = (1, 19), (2, 20), (3, 22), (4, 18), (5, 21)
for classNo, cnt in studentCnts:
    print('{}학급 학생수: {}'.format(classNo, cnt))
  • for문과 if문을 이용해서 과락 과목 출력하기
minScore = 60

korScore = int(input('국어 점수: '))
engScore = int(input('영어 점수: '))
mathScore = int(input('수학 점수: '))
sciScore = int(input('과학 점수: '))
hisScore = int(input('국사 점수: '))

scores = (('국어', korScore), ('영어', engScore), ('수학', mathScore), ('과학', sciScore), ('국사', hisScore))

for subject, score in scores:
    if score < 60:
        print('과락 과목: {}, 점수: {}'.format(subject, score))
# 실습: 아래의 표와 튜플을 이용해 학급 학생 수가 가장 작은 학급과 가장 많은 학급을 출력해보자.

studentCnts = ((1,18), (2,19), (3,23), (4,21), (5,20), (6,22), (7,17))

minClassNo = 0; maxClassNo = 0
minCnt = 0; maxCnt = 0

for classNo, cnt in studentCnts:
    if minCnt == 0 or minCnt > cnt:   # minCnt == 0 이면 들어오는 값 먼저 대입
        minClassNo = classNo
        minCnt = cnt

    if maxCnt < cnt:
        maxClassNo = classNo
        maxCnt = cnt

print('학생 수가 가장 적은 학급(학생수): {}학급({})명'.format(minClassNo, minCnt))
print('학생 수가 가장 많은 학급(학생수): {}학급({})명'.format(maxClassNo, maxCnt))

[튜플과 while문]

  • while문을 이용하면 다양한 방법으로 아이템 조회가 가능하다.
cars = ('그랜저', '쏘나타', '말리부', '카니발', '쏘렌토')

# 1)
n = 0
while n < len(cars):
    print(cars[n])
    n += 1

# 2)
n = 0
flag = True
while flag:
    print(cars[n])
    n += 1

    if n == len(cars):
        flag = False

# 3)
n = 0
while True:
    print(cars[n])
    n += 1

    if n == len(cars):
        break
  • while문과 if문을 이용해서 과락 과목 출력하기
minScore = 60

scores = (('국어', 58), ('영어', 77), ('수학', 89), ('과학', 99), ('국사', 50))

n = 0
while n < len(scores):
    if scores[n][1] < minScore:
        print('과락 과목: {}, 점수: {}'.format(scores[n][0], scores[n][1]))
    n += 1
# 실습: while문 이용해서 학급 학생 수가 가장 작은 학급, 가장 많은 학급 출력

studentCnt = (1, 18), (2, 19), (3, 23), (4, 21), (5, 20), (6, 22), (7, 17)

minClassNo = 0; maxClassNo = 0
minCnt = 0; maxCnt = 0

n = 0
while n < len(studentCnt):

    if minCnt == 0 or minCnt > studentCnt[n][1]:   # minCnt == 0, 처음에 오면 0이니 변경을 해줘야 함.
        minClassNo = studentCnt[n][0]
        minCnt  = studentCnt[n][1]

    if maxCnt < studentCnt[n][1]:
        maxClassNo = studentCnt[n][0]
        maxCnt = studentCnt[n][1]

    n += 1

print('학생 수가 가장 적은 학급(학생수): {}학급({})명'.format(minClassNo, minCnt))
print('학생 수가 가장 많은 학급(학생수): {}학급({})명'.format(maxClassNo, maxCnt))

[딕셔너리]

  • 키(key)와 값(value)를 이용해서 자료를 관리한다.
    -> 키는 절대 중복되지 말아야 한다. 값은 중복되도 가능하다.

[딕셔너리 선언]

  • { }를 이용해서 선언하고, '키:값' 형태로 아이템을 정의한다.
  • key와 value에는 숫자, 문자(열), 논리형 뿐만 아니라 컨테이너 자료형도 올 수 있다.
  • 단, key에 immutable 값은 올 수 있지만, mutable 값은 올 수 없다. (키에는 변경 불가능한 값만 올 수 있다. ex. 리스트 불가, 튜플 가능)
students = {'이름': '홍길동', '메일': 'gildong@gmail.com', '학년': 3, '취미': ['농구', '게임']}
students = {1: student1, 2: student2, 3: student2}

[딕셔너리 조회]

  • 딕셔너리는 키(key)를 이용해서 값(value)를 조회한다.
students = {'s1':'홍길동', 's2':'박찬호', 's3':'이용규', 's4':['박세리','박공주']}
print(students['s1'])
print(students['s2'])
print(students['s3'])
print(students['s4'][1])  # 박공주만 출력
  • 존재하지 않는 키를 이용한 조회 시 에러 발생.
students = {'s1':'홍길동', 's2':'박찬호', 's3':'이용규', 's4':['박세리','박공주']}
print(students['s5'])
  • get(key)를 이용해서 값(value)을 조회할 수 있다. -> get()은 에러발생 X
students = {'s1':'홍길동', 's2':'박찬호', 's3':'이용규', 's4':['박세리','박공주']}
print(students.get('s1'))
print(students.get('s6'))  # 에러 발생 X, None 출력

[딕셔너리 추가]

  • 딕셔너리이름[키(key)] = 값(value) 형태로 아이템을 추가한다.
myInfo = {}

myInfo['이름'] = '이철민'
myInfo['나이'] = 27
myInfo['취미'] = ['축구','야구']
print(myInfo)
  • 추가하려는 키가 이미 있다면 기존 값이 변경된다.
myInfo['메일'] = 'sjleepc@naver.com'    # 키 값은 중복될 수 없기에 메일의 값이 변경됨.
print(myInfo)
# 실습1: 학생 정보를 입력받아 딕셔너리에 추가해보자.

students = {}

students['name'] = input('이름 입력: ')
students['grade'] = input('학년 입력: ')
print(f'students: {students}')

---------------------------------------------------

# 실습2: 0부터 10까지의 각각의 정수에 대한 팩토리얼을 딕셔너리에 추가해보자.

factorialDic = {}

for i in range(11):
    if i == 0:
        factorialDic[i] = 1
    else:
        for j in range(1,i+1):
            factorialDic[i] = factorialDic[i-1] * j

print(f'factorialDic: {factorialDic}')

[딕셔너리 수정]

  • 딕셔너리이름[키(key)] = 값(value) 형태로 아이템을 수정한다.
# 실습1: 학생의 시험 점수가 60점 미만이면 'F(재시험)'으로 값을 변경해보자.

scores = {'kor':88, 'eng':55, 'math':85, 'sci':57, 'his':82}
print(f'scores: {scores}')

minScore = 60
fstr = 'F(재시험)'

if scores['kor'] < minScore: scores['kor'] = fstr
if scores['eng'] < minScore: scores['eng'] = fstr
if scores['math'] < minScore: scores['math'] = fstr
if scores['sci'] < minScore: scores['sci'] = fstr
if scores['his'] < minScore: scores['his'] = fstr

print(f'scores: {scores}')

# 실습2: 하루에 몸무게(kg)와 신장(m)이 각각 -0.3kg, + 0.001m씩 변한다고 할 때, 30일 후의 몸무게와 신장의 값을 저장하고 Bmi 값도 출력해보자.

myBodyInfo = {'이름':'gildong', '몸무게':83.0, '신장':1.8}
myBmi = myBodyInfo['몸무게'] / myBodyInfo['신장'] ** 2
print(f'myBodyInfo: {myBodyInfo}')
print(f'myBmi: {round(myBmi, 2)}')

date = 0
while True:

    myBodyInfo['몸무게'] = round((myBodyInfo['몸무게'] - 0.3), 2)
    print('몸무게: {}'.format(myBodyInfo['몸무게']))

    myBodyInfo['신장'] = round((myBodyInfo['신장'] + 0.001), 3)
    print('신장: {}'.format(myBodyInfo['신장']))

    myBmi = myBodyInfo['몸무게'] / myBodyInfo['신장'] ** 2

    date += 1

    if date >= 30:
        break

print(f'myBodyInfo: {myBodyInfo}')
print(f'myBmi: {round(myBmi, 2)}')

[keys( )와 value( )]

  • 전체 key와 value를 조회할 수 있다.
memInfo = {'이름':'홍길동', '메일':'cmleepc@naver.com', '학년':4}

ks = memInfo.keys()
print(f'ks: {ks}')
print(f'ks type: {type(ks)}')

vs = memInfo.values()
print(f'vs: {vs}')
print(f'vs type: {type(vs)}')

items = memInfo.items()
print(f'items: {items}')
print(f'items type: {type(items)}')
  • 리스트로 변환하기
ks = list(ks)
print(f'ks: {ks}')
print(f'ks type: {type(ks)}')
  • for문을 이용한 조회 (가장 중요!)
for key in memInfo.keys():
    print(f'{key}: {memInfo[key]}')
# 실습: 학생의 시험 점수가 60점 미만이면 F(재시험)으로 값을 변경하는 코드를 keys()를 이용해서 작성해보자.

scores = {'kor':88, 'eng':55, 'math':85, 'sci':57, 'his':82}
print(f'scores: {scores}')

minScore = 60
fDic = {}
fstr = 'F(재시험)'

for key in scores:
    if scores[key] < minScore:
        scores[key] = fstr
        fDic[key] = fstr

print(f'scores: {scores}')
print(f'fDic: {fDic}')

[딕셔너리 삭제]

  • del과 key를 이용한 item 삭제
memInfo = {'이름':'홍길동', '메일':'cmleepc@naver.com', '학년':4, '취미':['축구', '야구']}
print(f'memInfo: {memInfo}')

del memInfo['이름']
print(f'memInfo: {memInfo}')
  • pop()와 key를 이용한 item 삭제
    • 키 값이 사라지는 del과 달리, pop은 키 값이 반환된다.
memInfo = {'이름':'홍길동', '메일':'cmleepc@naver.com', '학년':4, '취미':['축구', '야구']}
print(f'memInfo: {memInfo}')

returnValue = memInfo.pop('메일')
print(f'memInfo: {memInfo}')
print(f'returnValue: {returnValue}')
print(f'returnValue type: {type(returnValue)}')
# 실습: 딕셔너리에 저장된 점수 중 최저 및 최고 점수를 삭제하는 프로그램을 만들어보자.

scores = {'s1':8.9, 's2': 8.1, 's3': 8.5, 's4': 9.8, 's5': 8.8}

minScore = 0; minScoreKey = 0
maxScore = 0; minSocreKey = 0

for key in scores.keys():
    if minScore == 0 or scores[key] < minScore:
        minScore = scores[key]
        minScoreKey = key

    if scores[key] > maxScore:
        maxScore = scores[key]
        maxScoreKey = key

print(f'scores: {scores}')

print(f'minScore: {minScore}')
print(f'minScoreKey: {minScoreKey}')
print(f'maxScore: {maxScore}')
print(f'maxScoreKey: {maxScoreKey}')

del scores[minScoreKey]
del scores[maxScoreKey]

print(f'scores: {scores}')

[딕셔너리 유용한 기능들]

  • in, not in : 키(key) 존재 유/무를 판단한다.
memInfo = {'이름':'홍길동', '메일':'cmleepc@naver.com', '학년':4, '취미':['축구', '야구']}
print('이름' in memInfo)
print('name' not in memInfo)
  • len(): 딕셔너리 길이(아이템 개수)를 알 수 있다.
memInfo = {'이름':'홍길동', '메일':'cmleepc@naver.com', '학년':4, '취미':['축구', '야구']}
print('len(memInfo): {}'.format(len(memInfo)))
  • clear(): 모든 아이템을 삭제한다.
memInfo = {'이름':'홍길동', '메일':'cmleepc@naver.com', '학년':4, '취미':['축구', '야구']}
memInfo.clear()
print('memInfo: {}'.format(memInfo))
# 실습: 개인 정보에 '연락처'와 '주민등록번호'가 있다면 삭제하는 코드를 작성하자.

myInfo = {'이름':'이철민', '나이': 27, '연락처':'010-1234-5678',
          '주민등록번호':'970000-1000000', '주소':'서울'}

deleteItems = ['연락처', '주민등록번호']

for item in deleteItems:
    if item in myInfo:
        del myInfo[item]

print(myInfo)
profile
늘 온 마음을 다해 :)

0개의 댓글