map reduce 用法 str处理lower() capitalize()

--

1 s='123456'
2 l={'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9}[s[0]]
3 print(l)

取出dic里面key的元素

 

1 def normalize(name):
2     tempn=name.lower().capitalize()
3     return tempn
4 L1 = ['adam', 'LISA', 'barT']
5 L2 = list(map(normalize, L1))
6 print(L2)

只大写第一个字母,其余小写  其中用到 字符串 lower()  capitalize()  函数

 

1 ##sum
2 num1=[20,30,50]
3 print(sum(num1[1:]))
4 ##multiply
5 def prod(L):
6     return  reduce(lambda x,y:x*y ,L)
7 L3=[1,2,3,4,6]
8 print(prod(L3[:2]))
9 print(1*2)

 

List 求和和求积函数    其中用到  reduce lambda表达式

1 def str2float(s):
2     tempstr=s.split('.')
3     ustr=tempstr[0]+tempstr[1]
4     def str2num(r):
5         return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[r]
6     ws=len(tempstr[1])
7     return reduce(lambda x, y: x * 10 + y, map(str2num, ustr))/(10**ws)

 

--str转换float函数。 思路去掉小数点,转换成整数,然后除以小数点的位数。

posted @ 2017-09-11 14:06  Anlovescode  阅读(471)  评论(0编辑  收藏  举报