python의 자료형 중 딕셔너리에 대해 알아보겠습니다.

 

1. 딕셔너리 선언

딕셔너리를 선언하는 방법에는 a = {키 : 값} 으로 선언합니다.

a = {'name' : 'Bae', 'phone' : '01012345678', 'birth' : '860628'}
b = {0 : 'Hello Jay',}
c = {'arr' : [1,2,3,4]}
d = {
    'Name' : 'Jay',
    'City' : 'Seoul',
    'Age' : 34,
    'Grade' : 'A+',
    'Status' : True
}
e = dict(
    Name = 'Jay',
    City = 'Seoul',
    Age = 34,
    Grade = 'A',
    Status = True
)

 

위에 선언한 딕셔너리를 출력해보겠습니다.

print('a : ', type(a), a)
print('b : ', type(b), b)
print('c : ', type(c), c)
print('d : ', type(d), d)
print('e : ', type(e), e)

>> result
a :  <class 'dict'> {'name': 'Bae', 'phone': '01012345678', 'birth': '860628'}
b :  <class 'dict'> {0: 'Hello Jay'}
c :  <class 'dict'> {'arr': [1, 2, 3, 4]}
d :  <class 'dict'> {'Name': 'Jay', 'City': 'Seoul', 'Age': 34, 'Grade': 'A+', 'Status': True}
e :  <class 'dict'> {'Name': 'Jay', 'City': 'Seoul', 'Age': 34, 'Grade': 'A', 'Status': True}

 

위에서 선언한 각 딕셔너리의 키의 개수를 확인하는 방법을 알아보겠습니다.

print('a : ', len(a))
print('b : ', len(b))
print('c : ', len(c))
print('d : ', len(d))
print('e : ', len(e))

>> result
a :  5
b :  1
c :  1
d :  5
e :  5

 

 

2. 원하는 키 출력

위에 선언한 딕셔너리 중 e를 통해 원하는 키를 출력하겠습니다.

'get'을 이용하여 출력할 경우 출력하고자 하는 키가 존재하지 않으면 none으로 출력됩니다.

e = dict(
    Name = 'Jay',
    City = 'Seoul',
    Age = 34,
    Grade = 'A',
    Status = True
)
print(e.get('Name'))
print(e.get('Address'))
print(e.get(0))
print(e.get('City'))
print(e.get('Age'))
print(e.get('Grade'))

>> result
Jay
None
None
Seoul
34
A

 

 

3. 딕셔너리 추가 · 수정

위에 선언한 딕셔너리 중 e를 통해 추가 및 수정해 보겠습니다.

 

e = dict(
    Name = 'Jay',
    City = 'Seoul',
    Age = 34,
    Grade = 'A',
    Status = True
)

 

딕셔너리 추가

e['phone'] = '01011112222'
print('e : ', e)

>> result
e :  {'Name': 'Jay', 'City': 'Seoul', 'Age': 34, 'Grade': 'A', 'Status': True, 'phone': '01011112222'}

딕셔너리 수정

e['Name'] = 'Bae'
print('e : ', e)
>> e :  {'Name': 'Bae', 'City': 'Seoul', 'Age': 34, 'Grade': 'A', 'Status': True, 'phone': '01011112222'}

e.update(City='Busan')
print('e : ', e)
>> e :  {'Name': 'Bae', 'City': 'Busan', 'Age': 34, 'Grade': 'A', 'Status': True, 'phone': '01011112222'}

temp = {'Grade' : 'B'}
e.update(temp)
print('e : ',e)
>> e :  {'Name': 'Bae', 'City': 'Busan', 'Age': 34, 'Grade': 'B', 'Status': True, 'phone': '01011112222'}

 

 

4. 딕셔너리 함수

   
keys() 딕셔너리의 키를 가져옴
values() 딕셔너리의 값을 가져옴
items() 딕셔너리의 키와 값을 둘다 가져옴
pop() 딕셔너리의 값을 꺼내온 후 딕셔너리에서 제거
x in a x가 딕셔너리 a에 포함되어 있는지 확인

 

print('e : ', e.keys()) # 키만 가져옴
>> e :  dict_keys(['Name', 'City', 'Age', 'Grade', 'Status'])

print('e : ', e.values()) # 값만 가져옴
>> e :  dict_values(['Jay', 'Seoul', 34, 'A', True])

print('e : ', e.items()) # 둘다 가져옴
>> e :  dict_items([('Name', 'Jay'), ('City', 'Seoul'), ('Age', 34), ('Grade', 'A'), ('Status', True)])

print('e : ', e.pop('City')) 
print('e : ', e)
>> e :  Seoul
>> e :  {'Name': 'Jay', 'Age': 34, 'Grade': 'A', 'Status': True}

print('e : ', 'Grade' in e)
e :  True

print('e : ', 'City' in e)
e :  False

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

12. python 제어문 - if문  (0) 2019.12.17
11. python 자료형 - 집합  (0) 2019.12.15
09. python 자료형 - 튜플  (0) 2019.12.14
08. python 자료형 - 리스트  (0) 2019.12.14
07. python 자료형 - 문자열  (0) 2019.12.10

+ Recent posts