python 2 map() reduce()

利用map()函数,把用户输入的不规范的英文名字,变为首字母大写,其他小写的规范名字。输入:['adam', 'LISA', 'barT'],输出:['Adam', 'Lisa', 'Bart']。

1 def cg(name):
2     return name[0].upper()+name[1:].lower()
3 L = ['adam', 'LISA', 'barT']
4 print map(cg,L1)
View Code

编写一个prod()函数,可以接受一个list并利用reduce()求积。

1 def prod(num1,num2):
2     return num1*num2
3 L = [3,7,5,9]
4 print reduce(prod,L)
View Code

 

posted @ 2017-07-04 13:27  tfatdos  阅读(152)  评论(0编辑  收藏  举报