250x250
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- JS
- shutil
- zipfile
- choice()
- Database
- MySQL
- __annotations__
- remove()
- __sub__
- locals()
- mro()
- shuffle()
- 파이썬
- fileinput
- inplace()
- items()
- 오버라이딩
- CSS
- discard()
- node.js
- View
- glob
- MySqlDB
- count()
- __getitem__
- __len__
- fnmatch
- HTML
- decode()
- randrange()
Archives
- Today
- Total
흰둥이는 코드를 짤 때 짖어 (왈!왈!왈!왈!왈!왈!왈!왈!왈!왈!왈!)
(파이썬) 딕셔너리 본문
728x90
반응형
1. 딕셔너리(Dictionart)
- 대응관계를 나타내는 자료형으로 key와 value라는 것을 한쌍으로 갖는 형태태
- 하나의 딕셔너리의 key는 중복될 수 없음
- 하나의 딕셔너리의 value는 중복 될 수 있음
1-1 딕셔너리 만들기
변수 = {키1:값1, 키2:값2 ... }
In [2]:
dic1 = {} # 빈 딕셔너리를 생성
print(dic1)
{}
In [5]:
dic2 = {1:'김사과', 2:'반하나', 3:'오렌지', 4:'이메론'}
print(dic2)
{1: '김사과', 2: '반하나', 3: '오렌지', 4: '이메론'}
1-2. key를 통해 value 찾기
In [7]:
dic2 = {1:'김사과', 2:'반하나', 3:'오렌지', 4:'이메론'}
print(dic2[1])
print(dic2[3])
김사과
오렌지
In [8]:
dic3 = {'no':1, 'userid':'apple', 'name':'김사과', 'hp':'010-1111-1111'}
print(dic3['no'])
print(dic3['userid'])
1
apple
1-3. 데이터 추가 및 삭제
In [12]:
dic4 = {1:'apple'}
print(dic4)
dic4[100] = 'orange'
print(dic4[100])
dic4[50] = 'melon'
print(dic4)
del dic4[1]
print(dic4)
print(type(dic4))
{1: 'apple'}
orange
{1: 'apple', 100: 'orange', 50: 'melon'}
{100: 'orange', 50: 'melon'}
<class 'dict'>
1-4. 딕셔너리 함수
In [13]:
dic3 = {'no':1, 'userid':'apple', 'name':'김사과', 'hp':'010-1111-1111'}
In [14]:
# keys(): key 리스트를 반환
print(dic3.keys())
dict_keys(['no', 'userid', 'name', 'hp'])
In [15]:
# values(): value 리스트를 반환
print(dic3.values())
dict_values([1, 'apple', '김사과', '010-1111-1111'])
In [17]:
# items(): key와 value를 한쌍으로 묶는 튜플을 반환
print(dic3.items())
dict_items([('no', 1), ('userid', 'apple'), ('name', '김사과'), ('hp', '010-1111-1111')])
In [18]:
print(dic3['userid'])
# get(): key를 이용해서 value를 반환
print(dic3.get('userid'))
apple
apple
In [19]:
print(dic3['age']) # 키가 존재 하지 않으면 에러
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-19-0816588416da> in <module>
----> 1 print(dic3['age']) # 키가 존재 하지 않으면 에러
KeyError: 'age'
In [21]:
print(dic3.get('age')) # None
print(dic3.get('age', '나이를 알 수 없음'))
None
나이를 알 수 없음
In [23]:
# in: key가 딕셔너리 안에 있는지 확인
print('name' in dic3)
print('age' in dic3)
True
False
1-5. 반복문을 이용한 딕셔너리 활용
In [24]:
dic3 = {'no':1, 'userid':'apple', 'name':'김사과', 'hp':'010-1111-1111'}
In [26]:
for i in dic3:
print(i, end=' ') # 키만 복사
no userid name hp
In [27]:
for i in dic3.keys():
print(i, end=' ') # 키만 복사
no userid name hp
In [28]:
for i in dic3.values():
print(i, end=' ') # 값만 복사
1 apple 김사과 010-1111-1111
In [30]:
for i in dic3:
print(dic3[i], end=' ')
1 apple 김사과 010-1111-1111
In [32]:
for i in dic3:
print(dic3.get(i), end= ' ')
1 apple 김사과 010-1111-1111
In [33]:
# 각 키를 순회하면서 키에 해당하는 값이 apple이 있는지 확인
dic3 = {'no':1, 'userid':'apple', 'name':'김사과', 'hp':'010-1111-1111'}
for i in dic3:
if dic3[i] == 'apple':
print('찾았습니다!')
else:
print('찾지 못했음!')
찾지 못했음!
찾았습니다!
찾지 못했음!
찾지 못했음!
728x90
반응형
'파이썬 기초' 카테고리의 다른 글
(파이썬) 사용자 정의 함수 (0) | 2023.03.08 |
---|---|
(파이썬) 세트 (0) | 2023.03.08 |
(파이썬) 반복문 (0) | 2023.03.07 |
(파이썬) 조건문 및 연산자 (0) | 2023.03.07 |
(파이썬) 튜플 (0) | 2023.03.07 |