The first name begins with the lower case; Practice:
1 def Normalize (name): 2 return name[0].upper () + name[1:].lower ()3 L1 = ['Adam' 'LISA'barT']4 L2 = List ( Map (normalize, L1))5print(L2)
Reduce quadrature:
1 from functools import reduce 2 3 def prod (L): Span style= "COLOR: #008080" >4 def FN (x, y): 5 return x * y 6 return reduce (FN, L) 7 print ( Span style= "COLOR: #800000" > " 3 * 5 * 7 * 9 = "
, prod ([3, 5, 7, 9]))
reduceThe result continues and the next element of the sequence is calculated cumulatively
String to floating point number exercises:
1 fromFunctoolsImportReduce2 3 defStr2Int (s):4 defChar2num (c):5 return{'0': 0,'1': 1,'2': 2,'3': 3,'4': 4,'5': 5,'6': 6,'7': 7,'8': 8,'9'8 {}[c]6 returnReduceLambdaX, y:x *10 +y, Map (Char2num, s))7 8 defStr2float (s):9S_list = S.split ('.')TenFloat_i = Str2Int (s_list[0])#123 OneFloat_f = Str2Int (s_list[1])/(10**len (s_list[1]))#456/1000 A returnFloat_i +Float_f - Print('str2float (\ ' 123.456\ ') =', Str2float ('123.456'))
Python Advanced function--map/reduce