Functions, adorners in Python

Source: Internet
Author: User
The map () function receives two parameters, one is a function, the other is a sequence, and map functions the passed-in function to each element of the sequence and returns the result as a new list.
>>> s = [' Aasda ', ' Dendy '] >>> def formatstr (ss): Return Ss[0].upper () + ss[1:len (ss)].lower () >&gt ;> v = formatstr (' Aaab ') >>> V ' aaab ' >>> map (formatstr, s) <map object at 0x000000000320a668> >>> for V in map (Formatstr, s): print (v) AASDA Dendy

Reduce functions a function in a sequence [X1, x2, x3 ...] , this function must receive two parameters, reduce to continue the result and the next element of the sequence cumulative calculation, the effect is:

Reduce (f, [X1, x2, X3, x4]) = f (f (f (x1, x2), x3), x4)
>>> L = [2, 3, 4] >>> def prod (a, b): Return a * b >>> >>> functools Import * The;>> reduce (prod, L) >>> filter function, which is the same as map, is designed to filter eligible elements in the list:
>>> s = [1, 2, -3,-5] >>> def notLt0 (x): return x >= 0 >>> filter (notLt0, s) <filter o Bject at 0x000000000320a940> >>> with V in filter (notLt0, s): Print (v) 1 2 >>> as the example above, define a notLt0 method that method to determine whether an element is greater than or equal to 0, returns True, or false. Filter calls the NotLt0 method with each element of list s as a parameter, and then assembles all elements that return true to be returned as a list. anonymous function: You do not need to define a function's name in the following format:
fn = lambda function argument list: The function body where FN is the returned anonymous function, for example:
' anonymous function ' ' F = the closure of the lambda x:x * x print (f (2)) function, like a JavaScript closure, where an external function returns a reference to an intrinsic function that may hold a reference to an external function variable, for example:
' Closure ' def COUNT (): FS = [] for I in range (1, 4): Def f (): print (i) return I *

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.