This is an example of using map to normalize strings (uppercase letters, the remaining letters lowercase)
#!/usr/bin/env pythondef lower2upper (s): loop = 0 l = ' for n in s: if n.islower () and loop == 0: l = l + n.upper () loop += 1 elif N.isupper () and loop == 0: l = l + n loop += 1 elif n.islower () and loop != 0: loop += 1 l = l + n else: l = l + n.lower () return lresult = map (lower2upper, [' Adam ', ' LISA ', ' BarT ') print result
This is an example of using reduce to multiply the number in a list
#!/usr/bin/env pythondef prod (list): Def multiply (x, y): return x * y return reduce (multiply, list) print prod ([1, 3, 5, 7])
This is an example of a string-to-int conversion using map and reduce
#!/usr/bin/env pythondef Str2Int (s): DEF fn (x, y): return x * + y def char2num (s): return {' 0 ': 0, ' 1 ': 1, ' 2 ': 2, ' 3 ': 3, ' 4 ': 4, ' 5 ': 5 ', ' 6 ': 6, ' 7 ': 7, ' 8 ': 8, ' 9 ': 9}[s] return reduce (FN, map (Char2num, s)) Print str2int (' 1234 5 ')
Where map is used to split the string into corresponding numbers and return it as a list
Reduce is used to accumulate the sum on each bit.
Reference
[1].http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/ 00141861202544241651579c69d4399a9aa135afef28c44000
Application of map and reduce