본문 바로가기
프로그램

[파이썬] 문제 : 대학교 리스트의 딕셔너리 요소 처리

by 오디세이99 2023. 11. 2.
728x90
반응형

k_universities = [
 ["Seoul National University", "Seoul, South Korea", "02-880-5114", ["College of Natural Sciences", 
"College of Engineering", "College of Social Sciences"]],
 ["KAIST", "Daejeon, South Korea", "042-350-2114", ["College of Engineering", "College of Business", "College of Computing"]],
 ["Yonsei University", "Seoul, South Korea", "02-2123-5114", ["College of Liberal Arts", "College of Science", "College of Business and Economics"]],
 ["Korea University", "Seoul, South Korea", "02-3290-1354", ["College of Life Sciences & Biotechnology", "College of Informatics", "College of Law"]],
 ["POSTECH", "Pohang, South Korea", "054-279-0114", ["Division of IT Convergence Engineering", 
"Division of Integrative Biosciences and Biotechnology", "Division of Electrical and Electronic Engineering"]],
 ["Hanyang University", "Seoul, South Korea", "02-2220-2114", ["College of Medicine", "College of Business and Economics", "College of Engineering"]],
 ["Sungkyunkwan University", "Seoul, South Korea", "02-760-0114", ["College of Social Sciences",
"College of Software", "College of Science and Technology"]],
 ["Ewha Womans University", "Seoul, South Korea", "02-3277-2114", ["College of Liberal Arts", 
"College of Natural Sciences", "College of Medicine"]],
 ["Sogang University", "Seoul, South Korea", "02-705-0114", ["College of Business Administration", 
"College of Engineering", "College of Liberal Arts"]],
 ["Kyung Hee University", "Seoul, South Korea", "02-961-0114", ["College of Korean Medicine", 
"College of Fine Arts", "College of Dentistry"]],
 ["Chosun University", "Gwangju, South Korea", "062-230-0114", ["College of Natural Sciences", 
"College of Engineering", "College of Medicine"]],
["Sookmyung Women's University", "Seoul, South Korea", "02-710-9114", ["College of Liberal Arts", "College of Science", "College of Business Administration"]],
["Kyungpook National University", "Daegu, South Korea", "053-950-5114", ["College of Natural Sciences", "College of Engineering", "College of Medicine"]],
["Jeju National University", "Jeju, South Korea", "064-555-0114", ["College of Natural Sciences", 
"College of Engineering", "College of Business Administration"]]]

 

# 1. 리스트 k_universities로부터, 대학명만 출력하세요. (for-loop 사용)
for i in range(len(k_universities)):   # k_universities 요소수만큼 반복
    print(k_universities[i][0])         # k_universities의 i번째 요소의 0번째 요소값(대학명)

# 2. 리스트 k_universities로부터, 각 대학의 단과대학명만 출력하세요. (for-loop과 if-else 를 사용)
for i in range(len(k_universities)):            # k_universities 요소수만큼 반복
    for j in range(len(k_universities[i][3])):  # k_universities의 i번째 요소가 리스트임. 이 리스트의 요소만큼 반복
        print(k_universities[i][3][j],end='')   # 요소 하나씩 출력, end=''로 줄바꿈을 하지 않고 연속 출력
        if j < len(k_universities[i][3])-1:     # 마지막 요소가 아니면
            print(', ',end='')                  # ,를 출력
    print()                                     # 각 대학의 단과대학들을 출력하면 줄바꿈

# 3. 리스트 k_universities로부터, list 내의 항목들이 아래와 같은 형태의 dictionary 형태가 되
#    도록 변환한 후, k_universities를 출력하세요. (for-loop을 사용)
'''
[ {
 "name": "Chosun University",
 "address": "Gwangju, South Korea",
 "phone": "062-230-0114",
 "colleges": ["College of Natural Sciences", 
"College of Engineering", "College of Medicine"]
 } ]
'''
tmp = []                                    # 최종 결과물 리스트 변수 지정
for i in range(len(k_universities)):       # k_universities 요소수만큼 반복
    dic = {}                                # 딕셔너리 변수 선언
    dic['name'] = k_universities[i][0]      # 딕셔너리에 name 값 추가
    dic['address'] = k_universities[i][1]
    dic['phone'] = k_universities[i][2]
    dic['colleges'] = k_universities[i][3]
    tmp.append(dic)             # 만들어진 딕셔너리를 리스트에 추가

k_universities = tmp
print(k_universities[0])                   # 결과 확인

# 4. dictionary 항목을 갖는 리스트 k_universities로부터, 서울 외 지역에 위치하는 대학에 대
#    한 정보만 출력하세요. (dictionary 성질 활용. for-loop과 if-else 사용
for i in range(len(k_universities2)):                  #  k_universities2 요소수만큼 반복
    if 'Seoul' not in k_universities2[i]['address']:   # 딕셔너리의 address 항목에 Seoul이 없으면
        print(k_universities2[i])

728x90
반응형

댓글