1 #-*-coding:utf-8-*- 2 #首字母大写,其他小写 3 def toCapitalize(s): 4 return s.capitalize() 5 6 S = [‘adam‘,‘LISA‘,‘barT‘] 7 print map(toCapitalize,S) 8 9 #对一个List求积 10 def prod(li): 11 # def multi(a,b): 12 # return a*b 13 # return reduce(multi,li) 14 return reduce(lambda x,y:x*y,li) 15 Li = [3,4,5] 16 print prod(Li) 17 18 #str2int的实现 19 def str2int(s): 20 def char2num(chr): 21 return {‘0‘: 0, ‘1‘: 1, ‘2‘: 2, ‘3‘: 3, ‘4‘: 4, ‘5‘: 5, ‘6‘: 6, ‘7‘: 7, ‘8‘: 8, ‘9‘: 9}[chr] 22 return reduce(lambda x,y : x*10+y ,map(char2num,s)) 23 St= ‘17362‘ 24 print str2int(St)
运行结果:
[‘Adam‘, ‘Lisa‘, ‘Bart‘] 60 17362
原文:http://www.cnblogs.com/chenxy93/p/4975567.html