언어/파이썬

[부트캠프]Dictionary 딕셔너리 -9일차

앨리스.W 2023. 5. 17. 23:31

✍기본 문법

1.기본 딕셔너리 구조

키-값 

dict = {"Bug": "bugvalue.", "Function": "function value."}

 

2.딕션너리 추가 , 생성,  초기화, 새로운 값추가

#딕션너리 추가
dict["abc"] = "abcccccc"

#딕션너리 생성
dict_two ={}

#딕션너리 초기화
dict ={}

#딕션너리 새로운 값 추가
dict["Bug"] = "computer buy"

3. for 문을 통해서 값들을 받아 올때

#기초
#반복문
for key in dict:
  print(key) # 키값
  print(dict[key]) #키에 해당하는 밸류값
  
  
  
  #심화
  #기존의 딕셔너리를 활용해서 새 딕셔너리를 만들때 for문을 이용
  for student in student_scores:
    score = student_scores[student]
    if score > 90:
      student_scores[student] = "outstanding" #값추가
    elif score > 80:
      student_scores[student] = "Exceeds"
    elif score > 70:
      student_scores[student] = "Acceptable"      
    else:
      student_scores[student] = "Fail"

 

4.딕셔너리와 리스트의 활용 ⭐

#딕셔너리 안에 리스트
travel_log = {
  "프랑스": ["파리", "릴레", "디전"],
  "독일": ["베를린", "함부그", "스터가트"],
}

#딕셔너리 중첩
travel_log = {
  "프랑스": {"방문했던 도시": ["파리", "릴레", "디전"], 
             "방문횟수": 12},
  "독일": {"방문했던 도시": ["베를린", "함부그", "스터가트"], 
         "방문횟수": 5},
}

#리스트 안에 딕셔너리로 변경

travel_log = [
{
  "국가": "프랑스", 
  "방문했던 도시": ["파리", "릴레", "디전"], 
  "방문횟수": 12
},
{
  "국가": "독일",
  "방문했던 도시": ["베를린", "함부그", "스터가트"], 
  "방문횟수": 5,
},
]

#기존에 리스트안에 딕셔너리들이 있고 그 리스트안에 새로운 딕셔너리를 추가할려면
def add_new_country(country, visitcount , cities):
    # new_dict = {
    # "country": country,
    # "visits": visitcount,
    # "cities": cities}
    # travel_log.append(new_dict)

    new_dict = {}
    new_dict["city"] = country
    new_dict["visits"] = visitcount
    new_dict["cities"] = cities
    travel_log.append(new_dict)

 

5.미니 프로젝트 - 입찰프로그램⭐

bids = {}
bid_flg = False

def bidfund(record):
    bignum = 0
    winner =""
    for bidder in record:
     amount = record[bidder]
     if amount > bignum:
        bignum = amount
        winner = bidder
    print(f"입찰자는 ${winner} 입니다)
    
    
while not bid_flg:
   name = int(input("이름이 무엇인가요?")) #변환 중요..
   price = int(input("입찰가격을 입력하세요"))
   bids[name] = price
   contiue = input("계속 하시겠습니까? yes or no")
   if contiue =="no"
       bid_flg = True
       bidfund(bids)
    elif contiue = "yes"
    clear()

 

✍메모

여기서부터 난이도가 올라가고 있다는걸 느끼고 있다.

딕셔너리는 아는데도 자주안써서 매번 배울떄마다 새롭다.

자주 봐야겠다.

반응형