Python/Python__works

dict comprehension

말하는감자 2019. 5. 13. 14:13
#dict comprehension

dict_comp_test = [
    (1, 'seoul'),
    (2, 'busan'),
    (3, 'incheon'),
]

dict_test_1 = {city: c for c, city in dict_comp_test}
print(dict_test_1) # {'seoul': 1, 'busan': 2, 'incheon': 3}

dict_test_2 = {code: city.upper() for city, code in dict_test_1.items() if code > 1}
print(dict_test_2) # {2: 'BUSAN', 3: 'INCHEON'}

'Python > Python__works' 카테고리의 다른 글

python list comprehension  (0) 2019.05.23
성적 grade 주기  (0) 2019.05.13
지능형 리스트, list comprehension  (0) 2019.05.13
여백삭제, 유니코드출력  (0) 2019.05.13
리스트 초기화  (0) 2019.05.09