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

时间:2023-03-09 03:56:12
map  reduce  用法 str处理lower() capitalize()

--

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

取出dic里面key的元素

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

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

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

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

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

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