본문 바로가기
IT관련/Python

[확고의 쓱~ 훑어보는 기록] Python - dict 사전 예제(3) (for문 이용해서 key와 value값 가져오기)

by 확고 2022. 7. 28.
728x90
반응형
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#-*- coding:utf-8
'''
Created on 2022. 7. 28.
 
@author: hwakgo
'''
 
score = {'우변':100,'봄날의햇살최수연':1004}
print(score.keys())#▶dict_keys(['우변', '봄날의햇살최수연'])
print(score.values())#▶dict_values([100, 1004])
 
#dict의 key값만 반복해서 가져오기(dict의 keys함수 활용)
for k in score.keys() :
    print(k, end=' ')#▶우변 봄날의햇살최수연 
print()
 
#dict의 value값만 반복해서 가져오기(dict의 values함수 활용)
for v in score.values() :
    print(v, end=' ')#▶100 1004
print()
 
#dict의 key와 value 반복해서 가져오기 1(dict의 get함수 활용)
for i in score :
    print(i,':',score.get(i))#score.get(i) = score[i] 결과 같음
print()
#▶우변 : 100
#▶봄날의햇살최수연 : 1004
 
#dict의 key와 value 반복해서 가져오기 2(dict의 items함수 활용)
for i in score.items() :
    print(i)
#▶('우변', 100)   
#▶('봄날의햇살최수연', 1004)
   
for i,j in score.items() :
    print(i,':',j)
#▶우변 : 100
#▶봄날의햇살최수연 : 1004
cs

 

▼ (이전 게시물) [확고의 쓱~ 훑어보는 기록] Python - dict 사전 예제(2) (데이터 확인, dict함수(get, update, clear))

 

[확고의 쓱~ 훑어보는 기록] Python - dict 사전 예제(2) (데이터 확인, dict함수(get, update, clear))

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 #-*- coding:utf-8 ''' Created on 2022. 7. 28. @author: hwakgo ''' score = {'..

j-growthdiary.tistory.com

 

▼ (첫 번째 게시물) [확고의 쓱~ 훑어보는 기록] Python - 출력 예제 (print / str, int 함수 / 서식 문자 사용)

 

[확고의 쓱~ 훑어보는 기록] Python - 출력 예제 (print / str, int 함수 / 서식 문자 사용)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 #-*- coding:utf-8 ''' Created on ..

j-growthdiary.tistory.com

728x90
반응형

댓글