Application of map and reduce

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.