728x90
:: 파이썬 과제 - 반복문 (for) 활용 ::
3. 반복문 (for)
for, list, dictionary, if
3. 반복문 (for)
요구조건
- 입출력 예제에 있는 사람들 중, 평균 성적이 70점 이상인 사용자의 이름과 나이를 출력해주세요
입출력 예제)
users = [
{"name": "Ronald", "age": 30, "math_score": 93, "science_score": 65, "english_score": 93, "social_score": 92},
{"name": "Amelia", "age": 24, "math_score": 88, "science_score": 52, "english_score": 78, "social_score": 91},
{"name": "Nathaniel", "age": 28, "math_score": 48, "science_score": 40, "english_score": 49, "social_score": 91},
{"name": "Sally", "age": 29, "math_score": 100, "science_score": 69, "english_score": 67, "social_score": 82},
{"name": "Alexander", "age": 30, "math_score": 69, "science_score": 52, "english_score": 98, "social_score": 44},
{"name": "Madge", "age": 22, "math_score": 52, "science_score": 63, "english_score": 54, "social_score": 47},
{"name": "Trevor", "age": 23, "math_score": 89, "science_score": 88, "english_score": 69, "social_score": 93},
{"name": "Andre", "age": 23, "math_score": 50, "science_score": 56, "english_score": 99, "social_score": 54},
{"name": "Rodney", "age": 16, "math_score": 66, "science_score": 55, "english_score": 58, "social_score": 43},
{"name": "Raymond", "age": 26, "math_score": 49, "science_score": 55, "english_score": 95, "social_score": 82},
{"name": "Scott", "age": 15, "math_score": 85, "science_score": 92, "english_score": 56, "social_score": 85},
{"name": "Jeanette", "age": 28, "math_score": 48, "science_score": 65, "english_score": 77, "social_score": 94},
{"name": "Sallie", "age": 25, "math_score": 42, "science_score": 72, "english_score": 95, "social_score": 44},
{"name": "Richard", "age": 21, "math_score": 71, "science_score": 95, "english_score": 61, "social_score": 59},
{"name": "Callie", "age": 15, "math_score": 98, "science_score": 50, "english_score": 100, "social_score": 74},
]
def get_filter_user(users):
# some code
return filter_users
filter_users = get_filter_user(users)
pprint(filter_users)
""" 아래 처럼 출력하기
[{'age': 30, 'name': 'Ronald'},
{'age': 24, 'name': 'Amelia'},
{'age': 29, 'name': 'Sally'},
{'age': 23, 'name': 'Trevor'},
{'age': 26, 'name': 'Raymond'},
{'age': 15, 'name': 'Scott'},
{'age': 28, 'name': 'Jeanette'},
{'age': 21, 'name': 'Richard'},
{'age': 15, 'name': 'Callie'}]
"""
풀이)
from pprint import pprint
users = [
{"name": "Ronald", "age": 30, "math_score": 93, "science_score": 65, "english_score": 93, "social_score": 92},
{"name": "Amelia", "age": 24, "math_score": 88, "science_score": 52, "english_score": 78, "social_score": 91},
{"name": "Nathaniel", "age": 28, "math_score": 48, "science_score": 40, "english_score": 49, "social_score": 91},
{"name": "Sally", "age": 29, "math_score": 100, "science_score": 69, "english_score": 67, "social_score": 82},
{"name": "Alexander", "age": 30, "math_score": 69, "science_score": 52, "english_score": 98, "social_score": 44},
{"name": "Madge", "age": 22, "math_score": 52, "science_score": 63, "english_score": 54, "social_score": 47},
{"name": "Trevor", "age": 23, "math_score": 89, "science_score": 88, "english_score": 69, "social_score": 93},
{"name": "Andre", "age": 23, "math_score": 50, "science_score": 56, "english_score": 99, "social_score": 54},
{"name": "Rodney", "age": 16, "math_score": 66, "science_score": 55, "english_score": 58, "social_score": 43},
{"name": "Raymond", "age": 26, "math_score": 49, "science_score": 55, "english_score": 95, "social_score": 82},
{"name": "Scott", "age": 15, "math_score": 85, "science_score": 92, "english_score": 56, "social_score": 85},
{"name": "Jeanette", "age": 28, "math_score": 48, "science_score": 65, "english_score": 77, "social_score": 94},
{"name": "Sallie", "age": 25, "math_score": 42, "science_score": 72, "english_score": 95, "social_score": 44},
{"name": "Richard", "age": 21, "math_score": 71, "science_score": 95, "english_score": 61, "social_score": 59},
{"name": "Callie", "age": 15, "math_score": 98, "science_score": 50, "english_score": 100, "social_score": 74},
]
def get_filter_user(users):
# some code
filter_users = [] # 최종 리스트 형태로 담아주기 위해서 리스트형식으로 변수 지정
for i in users :
avr = (i['math_score']+i['science_score']+i['english_score']+i['social_score']) / 4 # 평균점수 구하기
# i['avr'] = avr # fliter 함수 사용시 기존 딕셔너리에 'avr' key값으로 값을 생성하여 사용가능
user = {} # 새로 담아줄 딕셔너리 생성
if avr >= 70 : # 계산한 평균 점수로 70점이상인 사람만 새로운 딕셔너리로 추가
user['age'] = i['age'] # 기존 'age' key 값을 새로운 user 'age' key 값에 생성 추가
user['name'] = i['name'] # 기존 'name' key 값을 새로운 user 'name' key 값에 생성 추가
filter_users.append(user) # 최종 return 리스트 안에 딕셔너리를 담아준다
return filter_users # 반복문이 전부돌면 최종 리스트를 return 해준다
get_filter_user(users)
filter_users = get_filter_user(users)
pprint(filter_users)
b = [{'age': 30, 'name': 'Ronald'},
{'age': 24, 'name': 'Amelia'},
{'age': 29, 'name': 'Sally'},
{'age': 23, 'name': 'Trevor'},
{'age': 26, 'name': 'Raymond'},
{'age': 15, 'name': 'Scott'},
{'age': 28, 'name': 'Jeanette'},
{'age': 21, 'name': 'Richard'},
{'age': 15, 'name': 'Callie'}]
print(filter_users == b)
실행 예시)
[{'age': 30, 'name': 'Ronald'},
{'age': 24, 'name': 'Amelia'},
{'age': 29, 'name': 'Sally'},
{'age': 23, 'name': 'Trevor'},
{'age': 26, 'name': 'Raymond'},
{'age': 15, 'name': 'Scott'},
{'age': 28, 'name': 'Jeanette'},
{'age': 21, 'name': 'Richard'},
{'age': 15, 'name': 'Callie'}]
True
종료 코드 0(으)로 완료된 프로세스
728x90
반응형
'coding > Project' 카테고리의 다른 글
[Django] Clone Coding - 01 - 장고 화면 뛰우기! (제로부터 시작하는 인스타그램) (0) | 2022.09.28 |
---|---|
[Django] Clone Coding - 00 - 프로젝트 생성 및 초기 세팅 (제로부터 시작하는 인스타그램) (0) | 2022.09.28 |
[Python] 활용편 - 10 - 반복문(while) 활용(while, if, try / except) (1) | 2022.09.16 |
[Python] 활용편 - 09 - 조건문 활용(if, elif, else, 등급표) (0) | 2022.09.15 |