DICTIONARIES¶
- my_dict = {'key1':'value1', 'key2':'value2', 'key3':'value3'}
- 딕쳐너리는 키 밸류의 쌍으로 되어 있다.
- 키는, 딕셔너리 안에 유일한 값으로 되어 있다. 따라서 키가 같은 값을 가질 수 없다. 그러나 밸류는 같은 값이 여러개 있어도 상관없다.
- 리스트는 인덱스의 오프셋으로 접근하지만, 딕셔너리는 키로 접근한다.
In [1]:
{}
Out[1]:
{}
In [3]:
dict()
Out[3]:
{}
In [7]:
# key : value => 1쌍은 item
my_phone = {"brand" : "Apple" , "model" : "iphone X" , "year" : "2018"}
In [ ]:
#데이터 액세스는, 변수명 오른쪽에 대괄호로 한다.
#단 대괄호 안에 숫자를 쓰는것이 아니라 키를쓴다.
In [10]:
my_phone["brand"]
Out[10]:
'Apple'
In [19]:
my_phone["color"]
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) Cell In [19], line 1 ----> 1 my_phone["color"] KeyError: 'color'
In [ ]:
#함수로 액세스 하는 방법! => get()
In [17]:
my_phone.get('brand')
Out[17]:
'Apple'
In [18]:
my_phone.get("color")
In [20]:
my_phone["model"] = "iphone 12"
In [21]:
my_phone
Out[21]:
{'brand': 'Apple', 'model': 'iphone 12', 'year': '2018'}
In [24]:
my_phone["year"] = "2021"
In [25]:
my_phone
Out[25]:
{'brand': 'Apple', 'model': 'iphone 12', 'year': '2021'}
In [26]:
#딕셔너리에 새로운 데이터 추가
In [27]:
my_phone
Out[27]:
{'brand': 'Apple', 'model': 'iphone 12', 'year': '2021'}
In [31]:
my_phone["color"] = "red"
In [32]:
my_phone
Out[32]:
{'brand': 'Apple', 'model': 'iphone 12', 'year': '2021', 'color': 'red'}
In [33]:
#딕셔너리의 데이터 삭제
In [34]:
del my_phone["model"]
In [35]:
my_phone
Out[35]:
{'brand': 'Apple', 'year': '2021', 'color': 'red'}
In [39]:
#딕셔너리의 key 값만 가져오기
In [38]:
my_phone.keys()
Out[38]:
dict_keys(['brand', 'year', 'color'])
In [ ]:
#밸류값만 가져오기
In [40]:
my_phone.values()
Out[40]:
dict_values(['Apple', '2021', 'red'])
In [ ]:
#쌍으로 가져오기
In [41]:
my_phone.items()
Out[41]:
dict_items([('brand', 'Apple'), ('year', '2021'), ('color', 'red')])
In [ ]:
# my_phone 에 brand 가 있나???
In [42]:
"brand" in my_phone
Out[42]:
True
In [43]:
"Apple" in my_phone
Out[43]:
False
In [44]:
"Apple" in my_phone.values()
Out[44]:
True
In [47]:
my_list = [5,1,2,4]
In [48]:
my_list
Out[48]:
[5, 1, 2, 4]
In [51]:
my_list.clear()
In [52]:
my_list
Out[52]:
[]
In [53]:
len(my_phone)
Out[53]:
3
In [54]:
my_phone.clear()
In [55]:
my_phone
Out[55]:
{}
BOOLEANS¶
- Boolean 은 다음 2가지의 오브젝트로 나타낸다. "False" and "True".
- 숫자 0과 1과 같은 의미이다.
In [ ]:
True
In [ ]:
False
In [56]:
int(True)
Out[56]:
1
In [57]:
int(False)
Out[57]:
0
'DataScience' 카테고리의 다른 글
Python 2.비교연산자와 if문 (0) | 2022.11.17 |
---|---|
Python 1.기본 자료구조 D)튜플, 셋 (0) | 2022.11.16 |
Python 1.기본 자료구조 B)리스트 (0) | 2022.11.16 |
Python 1.기본 자료구조 A)리스트 (0) | 2022.11.15 |
Python 0.기본데이터타입 (0) | 2022.11.14 |