Python map () function

Source: Internet
Author: User

1. Description

The map function in Python is applied to each iteration of an item and returns a list of results. If other iterative parameters are passed in, the map function iterates through each parameter with the corresponding processing function. The map () function receives two parameters, one is a function, the other is a sequence, and map passes the incoming function to each element of the sequence sequentially, returning the result as a new list.

Map (function, iterable, ...)

2. For example
    1. With a list, L = [1,2,3,4,5,6,7,8] we're going to work with F (x) =x^2 on this list, so we can use the map function.
       
      >>> L = [1,2,3,4,]
      >>> def pow2(x):
      ... return x*x
      ...
      >>> map(pow2,L)
      [1, 4, 9, 16]
    2. If an additional iteration parameter is given, the ' function ' is applied simultaneously to each element in the iteration parameter.
>>> def mknum(a,b,c):...     return a*10000+b*100+c... >>> l1 = [10,20,30]>>> l2 = [40,50,60]>>> l3 = [70,80,90]>>> map(mknum,l1,l2,l3)[104070205080306090]

The results show that the map function deals with the Mknum function of each list by taking the same subscript.

3. Small Tasks

Using the map () function, the nonstandard English name entered by the user becomes the first letter capitalized, and the other lowercase canonical names. Input: [' Adam ', ' Lisa ', ' Bart '], output: [' Adam ', ' Lisa ', ' Bart '].

  #!/usr/bin/env python    def chname(name):     0    forin name:        if n==0:            cname = a.upper()        else:            cname = cname + a.lower()        n = n+1    return cname    print map(chname,[‘bob‘,‘jeAN‘,‘jessica‘])

Python map () function

Related Article

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.