python高级函数作业

首字母大写

1 # -*- coding:utf-8 -*-
2 def normalize(name):
3     return name.capitalize()
4 
5 L1 = ['adam','LISA','barT']
6 L2 = list(map(normalize,L1))
7 print(L2)

求积

1 #-*- coding:utf-8 -*-
2 from functools import reduce
3 def prod(L):
4     return reduce(lambda x,y:x*y,L)
5 
6 print('3*5*7*9 = ',prod([3,5,7,9]))

字符串转浮点

 1 #-*- coding:utf-8 -*-
 2 from functools import reduce
 3 
 4 def str2float(s):
 5     L = s.split(".",1)
 6     ret = 0.0
 7     if len(L) == 1:
 8         ret = reduce(lambda x,y:float(x)*10+float(y),L[0])
 9     elif len(L)==2:
10         ret = reduce(lambda x,y:float(x)*10+float(y),L[0])
11         ret = ret + reduce(lambda x,y:float(x)*10+float(y),L[1])/10**len(L[1])
12     return ret
13 
14 print('str2float(\'123.456\')=',str2float('123.456'))

 

教程来源:

廖雪峰python3教程

posted @ 2017-08-25 15:58  绝望的老猫  阅读(220)  评论(0编辑  收藏  举报