python tutorial 學習筆記(二) Sequence type

built-in functions:

range()

len(object)

keyword in, return boolean

filter(function,sequence)

map(function,*sequence)

reduce(function,sequence)

enumerate(object) –> [(0,item0),(1,item1),…]

zip(seq1,seq2,…) –> [ (seq1[0],seq2[0]), (seq1[1],seq2[1] ), ….. ]

reversed(sequence), the original sequence object is not changed

sorted(sequence), the original sequence object is not changed

Note: sequence can be type str,unicode,list,tuple,buffer,xrange

list: [item1,item2,item3,…]

list.append(x) list.extend(l)

list.pop([i]),list.remove(x), del list[int:int]

li_a + li_b

List comporehension: [ expression for elem in sequence if condition]

 

turple:(item1,item2…)

immutable

tuple with one item: (“hello”, ) or “hello”,

packing and unpacking:

t =12345,54321,”hello"!”  #returns a turple

x,y,z = t                           #t can be any sequence type, number of left-side variables must equal len(t)

set, an unordered collection of unique elements

set() function, return set type

a-b, a|b, a&b, a^b

dictionary: {key1:val1, key2:val2, …}

.keys()

.sort()

built-in function dict(   [(key1,value1), (key2,value2), ….]  )  constructs a dictionary object

dict( key1=val1, key2=val2, …. )   , its keyword arguments form

Looping Techniques:

for k,v in dictionary.iteritems(): print k,v

for i,v in enumerate( list ): print i,v

for q,a in zip(list1,list2): print q,a

Comparing:

Sequence objects may be compared to other objects with the same sequence type. The comparison uses lexicographical ordering: first the first two items are compared, and if they differ this determines the outcome of the comparison; if they are equal, the next two items are compared, and so on, until either sequence is exhausted

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章