본문 바로가기

닥치고 공부/공돌이

스크립트 언어1 - Python - 201

Microsoft Windows [Version 6.1.7601] Copyright (c) 2009 Microsoft Corporation. All rights reserved. C:\Users\U402-14>python Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win 32 Type "help", "copyright", "credits" or "license" for more information. >>> dict <type 'dict'>

>>> dir(dict) ['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__', '__doc__' , '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '_ _new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__' , '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get ', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues', 'keys', 'pop', 'po pitem', 'setdefault', 'update', 'values', 'viewitems', 'viewkeys', 'viewvalues'] >>> dict_b = [] // dict_b를 List Type으로 지정해줌
>>> type(dict_b) <type 'list'>

>>> dict_b = {} // dict_b를 Dictionary Type으로 지정해줌 >>> type(dict_b) <type 'dict'>

>>> dict_c = {'a':123,'b':456,'c':789} // dict_c를 Dictionary Type으로 지정해줌

Key a의 value = 123

Key b의 value = 456

Key c의 value = 789


이것을 테이블로 정의 하면 아래와 같다.


 KEY

 Value

 a

 123

 b

 456

 c

 789


즉 KEY는 Name, 주민번호, 전화번호와 같은 개념

>>> dict_c {'a': 123, 'c': 789, 'b': 456}


>>> dict_c['a'] // dict_c의 A KEY의 Value 출력

123 >>> dict_c['d'] // dict_c의 d KEY는 정의되지 않았으므로 오류 발생 Traceback (most recent call last): File "<stdin>", line 1, in <module> KeyError: 'd'

>>> dict_c['d'] = 0 // dict_c의 d KEY를 정의하고 Value 값 '0'을 추가 >>> dict_c {'a': 123, 'c': 789, 'b': 456, 'd': 0}

>>> dict_c['e'] = 'XXX' // dict_c의 e KEY를 정의하고 Value 값 'XXX'을 추가

>>> dict_c // dict_c의 Key 순서는 아래와 같이 Random 배정인듯 함 {'a': 123, 'c': 789, 'b': 456, 'e': 'XXX', 'd': 0} >>> dir(dict_c) ['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__', '__doc__' , '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '_ _new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__' , '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get ', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues', 'keys', 'pop', 'po pitem', 'setdefault', 'update', 'values', 'viewitems', 'viewkeys', 'viewvalues'] >>> dict_c.keys() // dict_c의 Key값만 출력 ['a', 'c', 'b', 'e', 'd']

>>> dict_c.values() // dict_c의 Value값만 출력

[123, 789, 456, 'XXX', 0]

>>> for KEY in dict_c.keys(): // for 배열문(?)의 KEY에 dict_c.keys()를 정의 ... print KEY // KEY를 출력 ... a c b e d


>>> for KEY in dict_c.keys(): // for 배열문(?)의 KEY에 dict_c.keys()를 정의 ... print dict_c[KEY] // dict_c의 KEY의 Value를 출력

... 123 789 456 XXX 0


>>> for KEY in dict_c.keys(): // for 배열문(?)의 KEY에 dict_c.keys()를 정의 ...  print KEY, print dict_c[KEY] // dict_c의 KEY의 key값과 dict_c의 KEY의 Value를 출력

... a 123 c 789 b 456 e XXX d 0


>>> sorted(dict_c.keys()) // dict_c.keys의 KEY를 정렬

['a', 'b', 'c', 'd', 'e']

>>> dict_c.keys() // 본 출력 명령어에서는 여전히 정렬되지 않는다. ['a', 'c', 'b', 'e', 'd']

>>> temp_k = sorted(dict_c.keys()) // temp_k에 정렬된 c.keys 명령어를 넣는다. >>> temp_k ['a', 'b', 'c', 'd', 'e']

>>> dict_c {'a': 123, 'c': 789, 'b': 456, 'e': 'XXX', 'd': 0}

>>> dict_c['a'] = 'YYY' // KEY a의 Value값을 'YYY'로 변경한다. >>> dict_c {'a': 'YYY', 'c': 789, 'b': 456, 'e': 'XXX', 'd': 0}

>>> dict_c['d'] = len // KEY d의 Value값을 len 함수로 변경한다. >>> dict_c {'a': 'YYY', 'c': 789, 'b': 456, 'e': 'XXX', 'd': <built-in function len>}

>>> len(temp_k) // temp_k의 총 길이 5

>>> dict_c['d']('abc') // d를 'abc'에 대입하니 3의 값이 출력된다. d의 변수는 len 함수를 가지고 있기 때문에

3


>>> dict_c['d'](temp_k) // d를 temp_k에 대입하니 5의 값이 출력된다. 위의 len(temp_k) 명령 결과와 같게 됨

5

>>> dir(dict) ['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__', '__doc__' , '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '_ _new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__' , '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get ', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues', 'keys', 'pop', 'po pitem', 'setdefault', 'update', 'values', 'viewitems', 'viewkeys', 'viewvalues'] >>> dict_c {'a': 'YYY', 'c': 789, 'b': 456, 'e': 'XXX', 'd': <built-in function len>}


'닥치고 공부 > 공돌이' 카테고리의 다른 글

[스크랩]라디안에 대한 깨달음  (0) 2016.02.16
스크립트 언어1 - Python - 202  (0) 2015.04.01
스크립트 언어1 - Python - 102  (0) 2015.03.25
스크립트 언어1 - Python - 101  (0) 2015.03.25
VBA 공부 ①  (0) 2014.08.01