map,zip,reduce函数

 lt=range(5,10)
lw=range(8,13)
def mul(a,b):
return a*b
def mul_list(param1,param2):
return_list=[]
for i in range(len(lt)):
return_list.append(mul(lt[i],lw[i]))
print return_list
return return_list
m=mul_list(lt,lw)

map函数

定义一个函数,遍历一个或者多个序列,对其用函数进行处理

map(function,sequence[,seq1,seq2,seq3])

 map(...)
map(function, sequence[, sequence, ...]) -> list Return a list of the results of applying the function to the items of
the argument sequence(s). If more than one sequence is given, the
function is called with an argument list consisting of the corresponding
item of each sequence, substituting None for missing values when not all
sequences have the same length. If the function is None, return a list of
the items of the sequence (or a list of tuples if more than one sequence).
 def add(a,b,c):
if type(a)==type(b) and type(b)==type(c) and type(a)==type(c):
return a+b+c
else:
print '+ needed all data type the same'
list1=range(1,10)
list2=range(11,20)
list3=range(21,30)
lists=map(add,list1,list2,list3)
print '------------'
print lists
 zip(...)
zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)] Return a list of tuples, where each tuple contains the i-th element
from each of the argument sequences. The returned list is truncated
in length to the length of the shortest argument sequence.

返回list中的每一项是tuple类型

reduce函数:reduce把一个函数作用在一个序列[x1, x2, x3...]上,这个函数必须接收两个参数,reduce把结果继续和序列的下一个元素做累积计算

求5的阶乘

上一篇:ios 信任charles https 证书


下一篇:CSS背景图像位置属性background-position百分比详解