apply() filter()

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



你可能感兴趣的:(apply() filter())