Coding/Python

[python]딕셔너리, enumerate

후__아 2024. 4. 24. 15:22

※ dict() == {}

dict([('a', '가'), ('b', '나'), ( , ), ... ])

== dict({'a': '가', 'b': '나', '': '', ...})

== dict(zip(['a', 'b', ... ], ['가', '나', ... ])) != dict([], [])  # 그냥 리스트 두 개만 넣으면 안 됨!!!

a_dic.keys(): 키값만 담은 dict_key 객체 return

 

list(a_dic.keys())
for k in a_dic.keys():

     print(k)

a_dic.get('키') == a_dic['키']

 

a_dic.values()
a_dic.items(): dict_items[(키, 값), (키, 값), ...] 객체 return
# 프로그래머스 lv0 옷가게 할인
def solution(price):
    return price*0.8 if price>=500000 else price*0.9 if price>=300000 else int(price*0.95) if price>=100000 else price

# 다른 사람 풀이 - 딕셔너리, items()
def solution(price):
    discount_rates = {500000: 0.8, 300000: 0.9, 100000: 0.95, 0: 1}
    for discount_price, discount_rate in discount_rates.items():
        if price >= discount_price:
            return int(price * discount_rate)

 

cf) https://dojang.io/mod/page/view.php?id=2213

https://wikidocs.net/16#key-keys

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

[python]2차원 리스트 생성 및 접근, 값 추가  (0) 2024.06.13
[python]isdigit()  (0) 2024.06.13
[python]소수점 자리수 round  (0) 2024.04.24
[python]리스트 합집합/교집합/차집합  (0) 2024.04.24
[python]range  (0) 2024.04.24