python tutorial 学习笔记(二) Sequence type

本文介绍了Python中的内置函数如range(), len(), filter(), map()等,并详细解释了列表、元组、集合和字典的操作方法及技巧。此外还探讨了序列比较规则。

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值