1.1. 1. ¶
- 갖 갖 (, , 결, , 길 )
- () .
- 과 경 과, 갖고 과 ( ) 갖 .
- () .
- 과 경 과, 갖고 과 ( ) 갖 .
~cpp t = () # 공 t = (1,2,3) t = 1,2,3 # r = (1,) r = 1, # r=1 기 t[0]=100 # . * 개 . >>> x,y,z=1,2,3 * 값 게 . >>> x = 1 >>> y = 2 >>> x, y = y, x >>> x, y (2,1)
1.2. 과 ¶
- 개 고 .
~cpp t = 1,2,'hello'- , 꺼 고 .
~cpp x,y,z = t
* .
~cpp >>> T = (1,2,3,4,5) >>> L = list(T) >>> L[0] = 100 >>> L [100, 2, 3, 4, 5] >>> T = tuple(L) >>> T (100, 2, 3, 4, 5)
1.3. 경 ¶
1. 값 경
~cpp >>> def calc(a,b): return a+b, a*b >>> x, y = calc(5, 4)
2.
~cpp >>> print 'id : %s, name : %s' % ('gslee','GangSeong') id : gslee, name : GangSeong
3. apply
~cpp >>> apply(calc, (4,5)) (9,20)
4. 그 고 값 기 .
~cpp >>> d = {'one':1, 'two':2} >>> d.items() [('one',1), ('two',2)]
2. 2. ¶
- 객 , 갖 .
- 과 , (mapping).
- (key) 값(value) 근.
- 과 , (mapping).
- (key) 값(value) 근.
~cpp >>> dic = {} # dic . >>> dic['dictionary'] = '1. A reference book containing an alphabetical list of words, ...' >>> dic['python'] = 'Any of various nonvenomous snakes of the family Pythonidae, ...' >>> dic['dictionary'] # dic, dictionary ? '1. A reference book containing an alphabetical list of words, ...'
~cpp >>> member = {'basketball' :5, 'soccer':11, 'baseball':9} >>> member['baseball'] # 9 >>> member['volleyball'] = 7 # >>> member {'soccer' : 11, 'volleyball' : 7 'baseball' : 9 , 'basketball' : 5} >>> len(member) 4 >>> del member['basketball'] #
* 값 .
구 기 . < (hash) >
* 값 객 , 경 (immutable) . , , , , .
값 .~cpp >>> def add(a,b): return a+b >>> def sub(a,b): return a-b >>> action = {0:add, 1:sub} >>> action[0](4,5) 9 >>> action[1](4,5) -1
2.1. 객 ¶
1. D.keys() :
2. D.values() : 값
3. D.items() : (key, value)
4. D.has_key(key) : . D key 고 . (1), 거(0) .
6. D.copy() :
8. D.setdefalut(key , x) : get 과 값 값 (Dkey = x)
9. D.update(b) : for k in b.keys(): Dk=bk , b D .
10. D.popitem() : (, 값) 고 거.
2. D.values() : 값
3. D.items() : (key, value)
4. D.has_key(key) : . D key 고 . (1), 거(0) .
key in D .
5. D.clear() : D 6. D.copy() :
~cpp >>> a = d # . ( 객 공) >>> a = d.copy() # . ( 객 .) >>> phone = {'jack': 232412, 'jim':1111, 'Joseph' : 234632} >>> p = phone >>> phone['jack'] = 1234 >>> phone {'jack': 1234, 'jim':1111, 'Joseph' : 234632} >>> p {'jack': 1234, 'jim':1111, 'Joseph' : 234632} >>> ph = phone.copy() >>> phone['babo'] = 5324 >>> phone {'jack': 1234, 'jim':1111, 'Joseph' : 234632, 'babo' : 5324} >>> ph {'jack': 1234, 'jim':1111, 'Joseph' : 234632}7. D.get(key , x) : 값 Dkey 값 , x
8. D.setdefalut(key , x) : get 과 값 값 (Dkey = x)
9. D.update(b) : for k in b.keys(): Dk=bk , b D .
10. D.popitem() : (, 값) 고 거.
2.2. for 기 ¶
~cpp >>> D = {'a':1, 'b':2 ,'c':3} >>> for key in D.keys(): print key, D[key] b 2 c 3 a 1
* 기
~cpp >>> items = D.items() >>> items.sort() >>> items [('a', 1), ('b', 2) , ('c', 3)] >>> for k,v in items: print k, v a 1 b 2 c 3
* 2.2
~cpp >>> D = {'a':1, 'b':2, 'c':3} >>> for key in D: print key, D[key] a 1 c 3 b 2