수량 카운트 프로그램
2019. 8. 20. 20:26ㆍPrograming/Python
반응형
# 제품 수량 카운트 프로그램
# 리스트 배열
name = ["note", "pen", "note", "note", "pen", "ballpen", "eraser", "eraser"]
d = {} # dictionary를 만든다.
for i in name:
if i in d: # "note"라는 key가 dictionary에 들어 있어?
d[i] = d[i] + 1 # "note"가 있다면 수량 하나를 up해줘!
else: # 만약에 "note"가 없다면
d[i] = 1 # dictionary에 넣고, value는 1로 해줘!
print(d) # 출력
# 결과값:
{'note': 3, 'pen': 2, 'ballpen': 1, 'eraser': 2}
반응형
'Programing > Python' 카테고리의 다른 글
폴더 만들기 (0) | 2020.03.17 |
---|---|
() [] {}의 차이점 (0) | 2019.08.20 |
Dictionary (0) | 2019.08.20 |