apply() filter()
2019獨角獸企業重金招聘Python工程師標準>>>
apply() ? 相當于給一個函數傳遞參數,以列表或元祖的形式的形式,必須和函數的參數數量對應
def sum(x =1,y=2):
? ? return x + y
print apply(sum,(1,3))
4
def sum(x,y):
? ? return x + y
print apply(sum,[1,3])
4
filter(func or None,squence) ?> list or tuple or string
def func(x):
? ? if x >0:
? ? ? ? return x
print filter(func,range(-9,10))
[1, 2, 3, 4, 5, 6, 7, 8, 9]
reduce(func,sequence[,initial]) >value ?
def sum(x,y):
? ? return x+y
print reduce(sum,range(0,10))
45
map(func,sequence[,sequence,...]) >list ?對sequence里的每個元素執行func的操作,返回列表
print map(None,(1,2))
[1, 2]
cmp(x,y) ?比較函數
delattr(obj,name) 等價于 del obj.name
class people(object):
? ? def __init__(self,name,age):
? ? ? ? self.name = name
? ? ? ? self.age = age
p = people('yz',27)
print p.name
print p.age
delattr(p,'name') ?
print p.name ? 這句話時會報錯,AttributeError: 'people' object has no attribute 'name' 因為name屬性被刪除
print p.age
zip(seq1[seq2,...])
a ?= [1,2,3,4,5]
print zip(a)
print zip(a,a)
print zip(a,a,a)
[(1,), (2,), (3,), (4,), (5,)]
[(1, 1), (2, 2), (3, 3), (4, 4), (5, 5)]
[(1, 1, 1), (2, 2, 2), (3, 3, 3), (4, 4, 4), (5, 5, 5)]
轉載于:https://my.oschina.net/012345678/blog/270452
總結
以上是生活随笔為你收集整理的apply() filter()的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux01-Linux编辑内核定制属
- 下一篇: 为 Visual Studio 安装数据