딕셔너리형

 

# 파이썬 딕셔너리
# 범용적으로 가장 많이 사용
# 딕셔너리 자료형(순서X, 키 중복X, 수정O, 삭제O)

 

 


딕셔너리(Dictionary) 사용법

딕셔너리 선언
딕셔너리 특징
딕셔너리 수정
딕셔너리 함수
딕셔너리 중요성

 

 

 


# 선언

a = {'name': 'Kim', 'phone': '01012345678', 'birth': '870124'} #키(key):값(value)
b = {0: 'Hello python!'} #키는 숫자 문자 모두 가능하다.
c = {'arr': [1, 2, 3, 4]} #값은 어떠한 자료형태도 들어올 수 있다.
d = {
 'Name' : 'Niceman',
 'City'   : 'Seoul',
 'Age': '33',
 'Grade': 'A',
 'Status'  : True
}
e =  dict([                #dic안에 list안에 tuple로 선언하는 방법도 있다. 가독성이 떨어짐.
 ( 'Name', 'Niceman'),
 ('City', 'Seoul'),
 ('Age', '33'),
 ('Grade', 'A'),
 ('Status', True)
])

f =  dict(
 Name='Niceman',
 City='Seoul',
 Age='33',
 Grade='A',
 Status=True
)

print('a - ', type(a), a) #a -  <class 'dict'> {'name': 'Kim', 'phone': '01012345678', 'birth': '870124'}

print('b - ', type(b), b) #b -  <class 'dict'> {0: 'Hello python!'}

print('c - ', type(c), c) #c -  <class 'dict'> {'arr': [1, 2, 3, 4]}

print('d - ', type(d), d) #d -  <class 'dict'> {'Name': 'Niceman', 'City': 'Seoul', 'Age': '33', 'Grade': 'A', 'Status': True}

print('e - ', type(c), e) #e -  <class 'dict'> {'Name': 'Niceman', 'City': 'Seoul', 'Age': '33', 'Grade': 'A', 'Status': True}

print('f - ', type(c), f) #f -  <class 'dict'> {'Name': 'Niceman', 'City': 'Seoul', 'Age': '33', 'Grade': 'A', 'Status': True}

 

 



# 출력

print('a - ', a['name'])  #a -  Kim
print('a - ', a.get('name'))  #a -  Kim

#print('a - ', a['name1']) 존재X ->에러 발생
#print('a - ', a.get('name1')) 존재X -> None 처리
#get을 쓰면 키값이 존재하지 않아도 none이라고 처리해서 더 안정적으로 개발이 가능하다.

print('b - ', b[0]) #b -  Hello python!
print('b - ', b.get(0)) #b -  Hello python!
print('c - ', c['arr']) #c -  [1, 2, 3, 4]
print('c - ', c['arr'][3]) #c -  4
print('c - ', c.get('arr')) #c -  [1, 2, 3, 4]
print('d - ', d.get('Age')) #d -  33
print('e - ', e.get('Grade')) #e -  A
print('f - ', f.get('City')) #f -  Seoul

 

 

 

 


# 딕셔너리 추가

a['address'] = 'seoul'
#a['name'] = 'seoul'을 하면 기존의 name값을 seoul로 수정한다.
print('a - ', a) #a -  {'name': 'Kim', 'phone': '01012345678', 'birth': '870124', 'address': 'seoul'}


a['rank'] = [1, 2, 3]
print('a - ', a) #a -  {'name': 'Kim', 'phone': '01012345678', 'birth': '870124', 'address': 'seoul', 'rank': [1, 2, 3]}

 


# 딕셔너리 길이(키의 갯수)

print(len(a)) #5
print(len(b)) #1
print(len(d)) #5
print(len(e)) #5

 



# 딕셔너리에서 지원하는 함수들 dict_keys, dict_values, dict_items : 반복문(iterate) 사용 가능

print('a - ', a.keys()) #a -  dict_keys(['name', 'phone', 'birth', 'address', 'rank'])
print('b - ', b.keys()) #b -  dict_keys([0])
print('c - ', c.keys()) #c -  dict_keys(['arr'])
print('d - ', d.keys()) #d -  dict_keys(['Name', 'City', 'Age', 'Grade', 'Status'])

#키값들을 리스트로 변환.
print('a - ', list(a.keys())) #a -  ['name', 'phone', 'birth', 'address', 'rank']
print('b - ', list(b.keys())) #b -  [0]
print('c - ', list(c.keys())) #c -  ['arr']
print('d - ', list(d.keys())) #d -  ['Name', 'City', 'Age', 'Grade', 'Status']

print('a - ', a.values()) #a -  dict_values(['Kim', '01012345678', '870124', 'seoul', [1, 2, 3]])
print('b - ', b.values()) #b -  dict_values(['Hello python!'])
print('c - ', c.values()) #c -  dict_values([[1, 2, 3, 4]])
print('d - ', d.values()) #d -  dict_values(['Niceman', 'Seoul', '33', 'A', True])

print('a - ', list(a.values())) #a -  ['Kim', '01012345678', '870124', 'seoul', [1, 2, 3]]
print('b - ', list(b.values())) #b -  ['Hello python!']
print('c - ', list(c.values())) #c -  [[1, 2, 3, 4]]
print('d - ', list(d.values())) #d -  ['Niceman', 'Seoul', '33', 'A', True]

#키와 밸류를 동시에 가져온다.
print('a - ', a.items()) #a -  dict_items([('name', 'Kim'), ('phone', '01012345678'), ('birth', '870124'), ('address', 'seoul'), ('rank', [1, 2, 3])])
print('b - ', b.items()) #b -  dict_items([(0, 'Hello python!')])
print('c - ', c.items()) #c -  dict_items([('arr', [1, 2, 3, 4])])
print('d - ', d.items()) #d -  dict_items([('Name', 'Niceman'), ('City', 'Seoul'), ('Age', '33'), ('Grade', 'A'), ('Status', True)])

print('a - ', list(a.items())) #a -  [('name', 'Kim'), ('phone', '01012345678'), ('birth', '870124'), ('address', 'seoul'), ('rank', [1, 2, 3])]
print('b - ', list(b.items())) #b -  [(0, 'Hello python!')]
print('c - ', list(c.items())) #c -  [('arr', [1, 2, 3, 4])]
print('d - ', list(d.items())) #d -  [('Name', 'Niceman'), ('City', 'Seoul'), ('Age', '33'), ('Grade', 'A'), ('Status', True)]

 

 

 

 

print('a - ', a.pop('name')) #a -  Kim
print('a - ', a) #a -  {'phone': '01012345678', 'birth': '870124', 'address': 'seoul', 'rank': [1, 2, 3]}
print('b - ', b.pop(0)) #b -  Hello python!
print('b - ', b) #b -  {}
print('c - ', c.pop('arr')) #c -  [1, 2, 3, 4]
print('c - ', c) #c -  {}
print('d - ', d.pop('City')) #d -  Seoul
print('d - ', d) #d -  {'Name': 'Niceman', 'Age': '33', 'Grade': 'A', 'Status': True}


#popitem을 하면 임의대로 아무거나 꺼내온다.

print('f - ', f.popitem()) #f -  ('Status', True)
print('f - ', f) #f -  {'Name': 'Niceman', 'City': 'Seoul', 'Age': '33', 'Grade': 'A'}
print('f - ', f.popitem()) #f -  ('Grade', 'A')
print('f - ', f.popitem()) #f -  ('Age', '33')
print('f - ', f.popitem()) #f -  ('City', 'Seoul')
print('f - ', f.popitem()) #f -  ('Name', 'Niceman')

 

 

 


#in연산자

print('a - ', 'name' in a) #a -  False   name이라는 키가 a에 있냐?
print('a - ', 'addr' in a) #a -  False

 

 



# 수정

a['test'] = 'test_dict' #추가
print('a- ', a) #a-  {'phone': '01012345678', 'birth': '870124', 'address': 'seoul', 'rank': [1, 2, 3], 'test': 'test_dict'}

a['address'] = 'dj' #기존의 값 수정
print('a- ', a) #a-  {'phone': '01012345678', 'birth': '870124', 'address': 'dj', 'rank': [1, 2, 3], 'test': 'test_dict'}


f.update(Age=36) #문법적으로 확실하게 표현하고 싶으면 업데이트 메소드를 사용해서 수정해도 된다.
print('f - ', f) #f -  {'Age': 36}

temp = {'Age': 27}
f.update(temp)
print('f - ', f) #f -  {'Age': 27}

 

 

 

 

 

 

 

 

 

 

+ Recent posts