Python学習小記(2)---[list,iterator,and,or,zip,dict.keys]

9290 ワード

1.List動作
alist[:]はalistに相当する.copy()は、alistのshallo copyを作成できますが、alist[:]操作に対してalistオブジェクトを直接操作します.
>>> alist = [1,2,3]
>>> blist = alist[:]               #assign alist[:] to blist
>>> alist
[1, 2, 3]
>>> blist
[1, 2, 3]
>>> blist[2:] = ['a', 'b', 'c']   #allter blist
>>> alist [1, 2, 3] >>> blist [1, 2, 'a', 'b', 'c'] >>> alist[:] = ['a', 'b', 'c'] #alter alist[:] >>> alist ['a', 'b', 'c']

 
2.サイクルテクニック
#list
>>> knights = {'gallahad': 'the pure', 'robin': 'the brave'}
>>> for k, v in knights.items():
...     print(k, v)
...
gallahad the pure
robin the brave

#zip  
>>> questions = ['name', 'quest', 'favorite color']
>>> answers = ['lancelot', 'the holy grail', 'blue']
>>> for q, a in zip(questions, answers): ... print('What is your {0}? It is {1}.'.format(q, a)) ... What is your name? It is lancelot. What is your quest? It is the holy grail. What is your favorite color? It is blue. #reversed & sorted #Note:             ,    iterator #reversed >>> for i in reversed(range(1, 10, 2)): ... print(i) ... 9 7 5 3 1 #sorted >>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana'] >>> for f in sorted(set(basket)): ... print(f) ... apple banana orangez pear

3.
Enumerate()関数はist,strの反復可能なオブジェクトを作成し,反復オブジェクトは毎回1つの(index,value),形式のメタグループを返すことができる.
>>> astr = 'abc'                        
>>> alist = [1,2,3]                     
>>> enumerate(astr)                     
        
>>> enumerate(alist)                    
        
>>> def print_iterator(iterator):       
...     for ele in iterator:            
...             print(ele) ... >>> print_iterator(astr) a b c >>> print_iterator(enumerate(astr)) (0, 'a') (1, 'b') (2, 'c') >>> print_iterator(enumerate(alist)) (0, 1) (1, 2) (2, 3) >>> 

 
4.zip()例
>>> a = [1,2,3]                                       
>>> b = ['a', 'b', 'c']                               
>>> c = ['one', 'two', 'three']                       
>>> a,b,c                                             
([1, 2, 3], ['a', 'b', 'c'], ['one', 'two', 'three']) 
>>>                          
>>> def print_iterator(iterator): ... for ele in iterator: ... print(ele) ... >>> >>> print_iterator(zip(a)) (1,) (2,) (3,) >>> print_iterator(zip(a,b)) (1, 'a') (2, 'b') (3, 'c') >>> >>> print_iterator(zip(a,b,c)) (1, 'a', 'one') (2, 'b', 'two') (3, 'c', 'three') 

 
5.
注意adict.keys()が返すのはadictのkeysのビューだけです
>>> adict = dict(a=1, b=2)
>>> adict
{'a': 1, 'b': 2}
>>> view = adict.keys()
>>> view
dict_keys(['a', 'b']) >>> adict['c'] = 3 >>> view dict_keys(['a', 'b', 'c'])

 
6.異なる論理演算の戻り値
大体のルールは、式の真偽を判別できる最初のオブジェクトを返すことです.
>>> '' and 'a' and 'b'
''
>>> 'c' and '' and 'b'
''
>>> 'c' and 0 and 'b'
0
>>> '' or 'a' or 'b'
'a'
>>> 'c' or '' or 'b'
'c'
>>> '' or 0 or 'b'
'b'
>>> 1 and 3 and 4
4
>>> 0 or '' or []
[]

 
7.listの反復方式に注意し、(k,v)を取得するにはadict.を呼び出す必要がある.items()は,直接反復でkey,adict.しか得られない.keys()は完全に等価です
>>> adict = {'one':'first', 'two':'second', 'three':'third'}
>>> adict
{'one': 'first', 'two': 'second', 'three': 'third'}
>>> it = iter(adict)
>>> it

>>> next(it)
'one'
>>> keys = adict.keys()
>>> keys
dict_keys(['one', 'two', 'three'])
>>> items = adict.items()
>>> items
dict_items([('one', 'first'), ('two', 'second'), ('three', 'third')])
>>> iter(items)

>>> iter(keys)