函数本身也可赋值给变量
In [1]:
import math
math.sqrt(25)
Out[1]:
In [2]:
math.sqrt
Out[2]:
In [3]:
fun = math.sqrt
fun
Out[3]:
In [4]:
fun(10)
Out[4]:
将函数作为参数
In [5]:
def func_add(x, y, f):
"""
functional addition
"""
return f(x) + f(y)
print func_add(4, 25, math.sqrt)
print func_add(-4, 25, abs)
In [8]:
x_2 = [x**2 for x in range(10)]
print x_2
x_sqrt_lst = map(math.sqrt, x_2)
print x_sqrt_lst
x_2_float_lst = map(float, x_2)
print x_2_float_lst
x_2_str_lst = map(str, x_2)
print x_2_str_lst
In [9]:
str_lst = map(str, range(5)) # ['0', '1', ...]
print str_lst
def make_num(str1, str2):
return int(str1) * 10 + int(str2)
result = reduce(make_num, str_lst)
print result
规范字符串
In [10]:
name_lst = ['poNNY MA', 'rObIN li', 'steve JOBS', 'bILL gates']
standard_name_lst = map(str.title, name_lst)
print standard_name_lst
In [11]:
number_lst = range(-10, 10)
def is_negative(x):
return x < 0
filtered_lst = filter(is_negative, number_lst)
print number_lst
print filtered_lst
In [12]:
x_lst = range(10)
result_lst = map(lambda item : item**2 +item**3, x_lst)
print x_lst
print result_lst
In [13]:
x_lst = range(1, 5)
product = reduce(lambda x, y : x*y, x_lst)
print x_lst
print product
In [14]:
number_lst = range(-10, 10)
filtered_lst = filter(lambda x : x<0, number_lst)
print number_lst
print filtered_lst
In [ ]: