본문 바로가기
✨Python

[Python/파이썬]파이썬 빅데이터분석 및 인공지능 퀴즈 6.10답 (객체지향프로그램 for문)

by 김말자 2022. 5. 18.
728x90
728x90
BIG

quiz 6.10 다음은 각각의 사람들 성씨와 빈도수를 출력하고 어느 성씨가 가장 높은 빈도수를 차지했는지를 출력하는 다음의 프로그램을 완성하라.

명령문

people = ['홍','홍','김','이','홍','김']
def max_count(people):
    counts = {} #초기화, 집합, set
    for i in people : #포문
        if i in counts:
            counts[i] = counts[i]+1
        else:
            counts[i] = 1
    return counts   #빠져나감
counts = max_count(people) #호출

first = []
max_num = max(counts.values()) #가장높은값
for name , count in counts.items():
    print(name,':',count,'번')
    if count == max_num :
        first.append(name)
print('1등:',first)

 

결과

 

728x90
반응형
BIG

댓글