반응형
Ditionary
{1:one, 2:two}
mapping: key값을 가지고 찾음!
1. 기본적인 사용 방법: {}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | months = {1:'Jan', 2:'Feb', 3:'Mar', 4:'Apr', 5:'May', 6:'Jun', 7:'Jul', 8:'Aug', 9:'Sep', 10:'Oct', 11:'Nov', 12:'Dec'} print(months[1]) #Jan items = [('name', 'May'), ('birth', 5)] dict_items = dict(items) print(dict_items) # {'name': 'May', 'birth': 5} print(len(dict_items)) # 2 del dict_items['birth'] print(dict_items) # {'name': 'May'} print('name' in dict_items) # True print('birth' in dict_items) # False # list_items = [] # list_items[42] = 'Jiyoung' # IndexError: list assignment index out of range dict_items = {} dict_items['name'] = 'Jiyoung' print(dict_items) # {'name': 'Jiyoung'} | cs |
Dictionary 안에 또 Dictionary를 사용할수 있다!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | people = { 'Alice': { 'phone': '010-1234', 'email': 'Alice@mail.com' }, 'Beth': { 'phone': '010-4321', 'email': 'Beth@mail.com' }} print(people) # {'Alice': # {'phone': '010-1234', 'email': 'Alice@mail.com'}, # 'Beth': # {'phone': '010-4321', 'email': 'Beth@mail.com'}} print(people['Alice']) # {'phone': '010-1234', 'email': 'Alice@mail.com'} print(people['Alice']['phone']) # 010-1234 | cs |
활용!
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 31 32 33 34 35 36 37 | people = { 'alice': { 'phone': '010-1234', 'email': 'Alice@mail.com' }, 'beth': { 'phone': '010-4321', 'email': 'Beth@mail.com' }, 'jiyoung': { 'phone': '010-4200', 'email': 'jiyoung@mail.com' }} name = input('Name: ') print('Are you looking for phone number or email?') request = input('Phone number(p) or email(e)?: ') if request == 'p': key ='phone' if request == 'e': key ='email' if name in people: print("{}'s {} is {}".format(name, key, people[name][key])) else: print("We don't have {}'s information".format(name)) # Name: jiyoung # Are you looking for phone number or email? # Phone number(p) or email(e)?: e # jiyoung's email is jiyoung@mail.com # Phone number(p) or email(e)?: p # jiyoung's phone is 010-4200 # Name: Babo # Are you looking for phone number or email? # Phone number(p) or email(e)?: p # We don't have Babo's information | cs |
2. Dicionary method
1) Clear
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | people = { 'alice': { 'phone': '010-1234', 'email': 'Alice@mail.com' }, 'beth': { 'phone': '010-4321', 'email': 'Beth@mail.com' }, 'jiyoung': { 'phone': '010-4200', 'email': 'jiyoung@mail.com' }} print(people.clear()) # None | cs |
* reference 개념이해 *
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | x = {} y = x x['key'] = 'value' print(y) # {'key': 'value'} # pointer 끊 x = {} # y유지 print(y) # {'key': 'value'} # 다시 reference y = x x['key'] = 'value' x.clear() print(x) # {} print(y) # {} | cs |
2) copy
1 2 3 4 5 6 7 8 | x = {'username':'admin', 'machines': ['foo', 'bar', 'baz']} y = x.copy() print(y) # {'username': 'admin', 'machines': ['foo', 'bar', 'baz']} y['username'] = 'jiyoung' print(x) # {'username': 'admin', 'machines': ['foo', 'bar', 'baz']} print(y) # {'username': 'jiyoung', 'machines': ['foo', 'bar', 'baz']} | cs |
# deepcopy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | from copy import deepcopy d = {} d['names'] = ['Alice', 'Beth'] print(d) # {'names': ['Alice', 'Beth']} c = d.copy() print(c) # {'names': ['Alice', 'Beth']} dc = deepcopy(d) print(dc) # {'names': ['Alice', 'Beth']} d['names'].append('Jiyoung') print(d) # {'names': ['Alice', 'Beth', 'Jiyoung']} # d와 c는 서로 link되어있어서 update도 함께 print(c) # {'names': ['Alice', 'Beth', 'Jiyoung']} #deepcopy는 분리 print(dc) # {'names': ['Alice', 'Beth']} | cs |
3) fromkeys (default = None)
1 2 3 4 5 6 7 8 | a = {}.fromkeys(['name', 'age']) print(a) # {'name': None, 'age': None} b = dict.fromkeys(['name', 'age']) print(b) # {'name': None, 'age': None} c = dict.fromkeys(['name', 'age'], 'Not yet') print(c) # {'name': 'Not yet', 'age': 'Not yet'} | cs |
4) get (default = None)
1 2 3 4 5 6 | d = {} print(d.get('name')) # None print(d.get('name', 'N/A')) # N/A d['name'] = 'Jiyoung' print(d.get('name')) # Jiyoung | cs |
활용하기
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 31 32 33 34 35 36 | people = { 'alice': { 'phone': '010-1234', 'email': 'Alice@mail.com' }, 'beth': { 'phone': '010-4321', 'email': 'Beth@mail.com' }, 'jiyoung': { 'phone': '010-4200', 'email': 'jiyoung@mail.com' }} name = input('Name: ') print('Are you looking for phone number or email?') request = input('Phone number(p) or email(e)?: ') if request == 'p': key ='phone' if request == 'e': key ='email' if name in people: print("{}'s {} is {}".format(name, key, people[name][key])) else: print("{} is not in Book. Do you want save new one?".format(name)) r = input('yes(y) or no(n)') if r == 'y': people[name] = {} new_phone = input("phone: ") people[name]['phone'] = new_phone new_email = input("email: ") people[name]['email'] = new_email if r == 'n': print('okay bye!') print("you saved {}!".format(people[name])) | cs |
5) items(), keys(), values() (tuple로 가져옴)
1 2 3 4 5 6 | d = {'title': 'python web site', 'url': 'www.python.org', 'spam': 0} print(d.items()) # dict_items([('title', 'python web site'), # ('url', 'www.python.org'), # ('spam', 0)]) print(d.keys()) # dict_keys(['title', 'url', 'spam']) print(d.values()) # dict_values(['python web site', 'www.python.org', 0]) | cs |
6) update()
1 2 3 4 | d = {'title': 'python web site', 'url': 'www.python.org', 'spam': 0} x = {'title': 'python'} d.update(x) print(d) # {'title': 'python', 'url': 'www.python.org', 'spam': 0} | cs |
반응형
'Data > python·알고리즘' 카테고리의 다른 글
[python 기초] python의 module에 대하여 (0) | 2018.06.28 |
---|---|
[python 기초] 조건문과 Loop문 + List Comprehension (0) | 2018.06.28 |
[python 기초] python의 String에 대하여 (0) | 2018.06.27 |
[python 기초] python의 tuple에 대하여 (0) | 2018.06.27 |
[python 기초] python의 list에 대하여 (0) | 2018.06.27 |
댓글