map,zip,reduce函数

 1 lt=range(5,10)
 2 lw=range(8,13)
 3 def mul(a,b):
 4     return a*b
 5 def mul_list(param1,param2):
 6     return_list=[]
 7     for i in range(len(lt)):
 8         return_list.append(mul(lt[i],lw[i]))
 9     print return_list
10     return return_list
11 m=mul_list(lt,lw)
View Code

map函数

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

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

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

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

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

求5的阶乘

 

posted @ 2016-12-20 13:59  自动化前行者  阅读(106)  评论(0编辑  收藏  举报