高阶函数(参数中,包含函数的,自定义函数)
def add(x,y,f):
return f(x)+f(y)
add(-5,9,abs)
import math
def 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 math
def is_sqr(x):
r = int(math.sqrt(x))
return r*r ==x
print 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 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)