Python2

来源:互联网 发布:做淘宝用什么电脑好 编辑:程序博客网 时间:2024/05/01 12:35

高阶函数(参数中,包含函数的,自定义函数)

def add(x,y,f):    return f(x)+f(y)add(-5,9,abs)import mathdef add(x,y,f):    return f(x)+f(y)print add(25,9,math.sqrt)map()函数def format_name(s):    return s[0].upper()+s[1:].lower()print map(format_name, ['adam', 'LISA', 'barT'])filter   过滤import mathdef is_sqr(x):    r = int(math.sqrt(x))    return r*r ==xprint filter(is_sqr, range(1, 101))scorted   忽略大小写,进行排序def cmp_ignore_case(s1,s2):    u1 = s1.upper()    u2 = s2.upper()    if u1<u2:        return -1    if u1>u2:        return 1    return 0print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
0 0
原创粉丝点击