파이썬 (python) dic 기초 예제
# chapter 03_5
# 파이썬 딕셔너리
# 범용적으로 가장 많이 사용
# 딕셔너리 자료형(순서x, 키 중복x, 수정 가능, 삭제 가능)
# 선언
# []리스트 ()튜플
a = {'name' : 'kim', 'phone' : '01012345678', 'birth' : '870514' }
b = {0: 'hello python'}
c = {'arr' : [1,2,3,4]}
d = {
'name' : 'nicemnan',
'city' : 'seoul',
'age' : '33',
'grade' : 'A',
'status' : True
}
e = dict([
('name','niceman'),
('city','seoul'),
('age','33'),
('grade','A'),
('Status', True)
])
# f가 가장 많이 사용되는 형태
f =dict(
name ='niceman',
city ='seoul',
age ='33',
grade = 'A',
Status = True
)
print(type(a))
print(type(b))
print(type(c))
print(type(d))
print(type(e))
print(type(f))
print(a['name'])
print(a.get('name'))
print(a.get('name1')) # Error 시 return을 None으로 반환
print(b[0])
print(b.get(0))
print(f.get('city'))
print(f.get('age'))
# 딕셔너리 추가
a['address'] ='seoul'
print(a)
a['rank'] =[1,2,3]
print(a)
# 딕셔너리 길이
print()
print(len(a))
print(len(b))
print(len(c))
print(len(d))
print(len(e))
print()
print(a.keys())
print(b.keys())
print(c.keys())
print(d.keys())
print()
print(list(a.keys()))
print(list(b.keys()))
print()
print(a.values())
print(b.values())
print(c.values())
print(d.values())
print()
print(list(a.values()))
print(list(b.values()))
print()
print(list(a.values()))
print(list(b.values()))
print()
print(list(a.items()))
print(list(b.items()))
print(list(c.items()))
print()
print(list(a.items()))
print(list(b.items()))
print()
print(a.pop('name'))
print(a)
print(c.pop('arr'))
print(c)
print()
print(f.popitem()) # 랜덤하게 아이템을 꺼내서 쓴다. 쓰고 난 후에는 변수에서는 삭제
print(f)
print(f.popitem())
print(f)
print(f.popitem())
print(f)
print(f.popitem())
print(f)
print(f.popitem())
print(f)
# print(f.popitem())
# print(f)
print()
print('a -', 'birth' in a)
print('d -','city' in d)
a['test'] ='test_dict'
print(a)
a['address'] = 'dj'
결과
[Command: python -u C:\python_basic\chapter03_05.py]
<class 'dict'>
<class 'dict'>
<class 'dict'>
<class 'dict'>
<class 'dict'>
<class 'dict'>
kim
kim
None
hello python
hello python
seoul
33
{'name': 'kim', 'phone': '01012345678', 'birth': '870514', 'address': 'seoul'}
{'name': 'kim', 'phone': '01012345678', 'birth': '870514', 'address': 'seoul', 'rank': [1, 2, 3]}
5
1
1
5
5
dict_keys(['name', 'phone', 'birth', 'address', 'rank'])
dict_keys([0])
dict_keys(['arr'])
dict_keys(['name', 'city', 'age', 'grade', 'status'])
['name', 'phone', 'birth', 'address', 'rank']
[0]
dict_values(['kim', '01012345678', '870514', 'seoul', [1, 2, 3]])
dict_values(['hello python'])
dict_values([[1, 2, 3, 4]])
dict_values(['nicemnan', 'seoul', '33', 'A', True])
['kim', '01012345678', '870514', 'seoul', [1, 2, 3]]
['hello python']
['kim', '01012345678', '870514', 'seoul', [1, 2, 3]]
['hello python']
[('name', 'kim'), ('phone', '01012345678'), ('birth', '870514'), ('address', 'seoul'), ('rank', [1, 2, 3])]
[(0, 'hello python')]
[('arr', [1, 2, 3, 4])]
[('name', 'kim'), ('phone', '01012345678'), ('birth', '870514'), ('address', 'seoul'), ('rank', [1, 2, 3])]
[(0, 'hello python')]
kim
{'phone': '01012345678', 'birth': '870514', 'address': 'seoul', 'rank': [1, 2, 3]}
[1, 2, 3, 4]
{}
('Status', True)
{'name': 'niceman', 'city': 'seoul', 'age': '33', 'grade': 'A'}
('grade', 'A')
{'name': 'niceman', 'city': 'seoul', 'age': '33'}
('age', '33')
{'name': 'niceman', 'city': 'seoul'}
('city', 'seoul')
{'name': 'niceman'}
('name', 'niceman')
{}
a - True
d - True
{'phone': '01012345678', 'birth': '870514', 'address': 'seoul', 'rank': [1, 2, 3], 'test': 'test_dict'}
[Finished in 0.128s]