Sduty/python

Dictionary

돌멘 2019. 3. 12. 15:50

Dictionary


dictionary 추가

   a = {1:'a'}

   a[2] = 'b'   ==>  {1:'a', 2:'b'}

   a['text'] = 'text_test'   ==>  {1:'a', 2:'b', 'text':'text_test'}


dictionary 삭제

   a={1:'a', 2:'b', 'text':'text_test'}

   del a[1]  ==>  {2:'b', 'text':'text_test'}



Dictionary 함수

keys : key 리스트 만들기

    a = {'a':'text1','b':'text2','c':'text3'}

    a.keys()   ==>   dict_key['a','b','c']  (a의 key만을 모아서 dict_key라는 객체로 return)


values : value 리스트 만들기

    a = {'a':'text1','b':'text2','c':'text3'}

    a.values()   ==>  dict_values(['text1','text2','text3'])   (dict_values라는 객체로 return)


items : key, value 값 얻기

    a = {'a':'text1','b':'text2','c':'text3'}

    a.items()    ==>  dict_items([('a','text1'),('b','text2'),('c','text3'),])    (dict_items 라는 객체로 return)


clear : key, value 값 지우기

    a = {'a':'text1','b':'text2','c':'text3'}

    a.clear()    ==>   {}  (key, value를 전부 삭제)

get : key로 value 얻기

    a = {'a':'text1','b':'text2','c':'text3'}

    a.get('a')   ===>  'text1'


in : 해당 key가 dictionary안에 있는지 조사

    a = {'a':'text1','b':'text2','c':'text3'}

    'a' in a    ==>    True   (결과는 True, False로 return)


'Sduty > python' 카테고리의 다른 글

제어문(if, while, for)  (0) 2019.03.12
자료형 복사  (0) 2019.03.12
집합 자료형  (0) 2019.03.12
List 관련 함수  (0) 2019.03.12
문자열 escape code(출력), 주석처리  (0) 2019.03.12