fromFunctoolsImportReducedefStr2Int (s):defChar2int (c):return{"0": 0,"1": 1,"2": 2,"3": 3,"4": 4, "5": 5,"6": 6,"7": 7,"8": 8,"9": 9}[c]returnReduceLambdaX, Y:x * 10 +y, Map (Char2int, s))Print(Str2Int ("98986553"))
Title, change the string to int
1.
# using the map () function, the user entered an irregular English name, the first letter capitalized, the other lowercase canonical name.
# input: [' Adam ', ' Lisa ', ' Bart '], output: [' Adam ', ' Lisa ', ' Bart ']:
m = map (Lambda s:s[:1].upper () + s[1:].lower (), ['Adam'LISA ' ' BarT ' ])print(list (m))
The sum () function provided by 2.Python can accept a list and sum, write a prod () function that accepts a list and uses the reduce () to calculate the product:
def prod (l): return reduce (lambda x, y:x * y, L)
# Use map and reduce to write a str2float function that converts the string ' 123.456 ' to a floating-point number of 123.456:
def Str2float (s): return reduce (lambda x, y:x + y * (0.1 * * (len (y))), map (int, s.split ('). ')))
Python mapreduce Exercises