U E D R , A S I H C RSS

2학기파이선스터디/ 튜플, 사전



1. 1.


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) .

~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) .
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

2.3.


globals() ( ) () .

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:22:13
Processing time 0.0176 sec