Python---Advanced function map, filter, zip, enumerate, etc.

Source: Internet
Author: User

Today, when we look at Natural language processing this book, is attracted by the concept of high-level function here, because I think all functions are just functions, is to achieve a specific function of the implementation, there should be no high-level, low-grade AH! However, after understanding, found that these functions are really a bit advanced, very useful, so here to do a simple summary.

1. Haskell: Previously thought it was a function, in fact it is a collectively. The functions in Haskell can be transmitted as parameters and return values, and such functions are called higher-order functions.

2. Map (function, list): is to call the function function for each element in the list to process, return a new list

1 >>> d=[1,2,3]2def  F (s):3 ...     return s*1004 ... 5 >>> map (f,d)6 [100, 200, 300]

3. Filter (function, list): Invokes function for each element in the list and returns a list of elements that satisfy the condition. It feels like a map, yes, but there are subtle differences, see the code below to know.

1>>> Filter (LambdaX:x>2, D)2 [3] 3>>>filter (f, D)4[1, 2, 3]5>>> Map (LambdaX:x>2, D)6 [False, False, True]7>>>defF1 (s):8... s=s*1209 ...Ten>>>Map (F1, D) One[None, none, none]

4. zip ([iterable, ...])

Zip () is an intrinsic function of Python that takes a series of iterated objects as parameters, packages the corresponding elements in the object into tuple (tuples), and then returns a list of these tuples. If the length of the passed parameter is not equal, the length of the returned list is the same as the object with the shortest length in the parameter. Using the * operator, the list unzip (unzip) can be

1 >>> a = [3]2 >>> b = [4,5,6] >>> c = [4,5,6 , 7,8]4 >>> zipped = zip (A, b)5 [(1, 4), (2, 5), (3, 6)]6 >>> Zip (a,c)7 [(1, 4), (2, 5), (3, 6)]8 >>> Zip (*  zipped)9 [(1, 2, 3), (4, 5, 6)]

5. Enumerate (s): Returns a pairing that contains the item at the index and index.

1 >>> Enumerate (d)2 <enumerate object at 0x02873b98>3 >>> List (Enumerate (d)) 4 [(0, 1), (1, 2), (2, 3)]

Python---Advanced function map, filter, zip, enumerate, etc.

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.