Python built-in function two

Source: Internet
Author: User

built-in function two:1.Lambda(anonymous function) A sentence function designed to address some of the needs of a simple answer.        No DEF is required to declare. deffunc (n):returnNNPrint(func (10))# -F=Lambdan:n*NPrint(f (10))# -Note: syntax: Name of function=Lambdaparameters: Return value1the parameters of a function can have more than one.            Multiple parameters are separated by commas. 2anonymous functions can write only one line, no matter how complex, and return data directly after the logic ends.3The return value, like a normal function, can be any data type. Anonymous function is not to say that there must be no name, the preceding variable is a function name, said he is anonymous originally we look through the __name__ when the name, unity is called Lambda. There is nothing special in the call.    Like a normal function call. 2. Sorted (sort function) syntax: Soret (Iterable,key=none,reverse=False)            Iterable: An Iterative object key: A collation (sort function) in which each element of an iterative object is passed to the parameter of the function, sorted according to the result of the function operation. Recerse: Whether it is in reverse.            True: Reverse, False: positive order. <1>LST= [1,5,3,4,6] Lst2=Sorted (LST)Print(LST)#The original list does not change            Print(LST2)#The new list returned is a sortedresults: [1, 5, 3, 4, 6]                [1, 3, 4, 5, 6]            <2>DiC= {1:'A', 3:'C', 2:'B'}            Print(Sorted (DIC))#if it's a dictionary. Returns the key after sortingresults: [1, 2, 3]            <3>#Sort by string lengthLST = ["haha","I love you .","China","Dear Mother"]            #Calculating string Strings length            deffunc (s):returnLen (s)Print(Sorted (LST, key=func)) Results: ['haha','China','I love you .','Dear Mother']            <4>#Sort by string lengthLST = ["haha","I love you .","China","Dear Mother"]            #Calculating string Lengths            Print(Sorted (LST, key=LambdaS:len (s))) Results: ['haha','China','I love you .','Dear Mother']            <5>LST= [{"ID": 1,"name":'a'," Age": 18},                   {"ID": 2,"name":'b'," Age": 16},                   {"ID": 3,"name":'C'," Age": 17}]            #sort student information by age            Print(Sorted (LST, key=Lambdae:e[' Age']) Result: [{'ID': 2,'name':'b',' Age': 16}, {'ID': 3,'name':'C',' Age': 17}, {'ID': 1,'name':'a',' Age': 18}]    3. Filter (Filter function) syntax: filter (function,iterable) function: Used to filter the functions, in the filter will automatically pass the elements in the iteratable to Functio            n then determine whether to preserve this data based on function return TRUE or FALSE. Iterable: An example of an iterative object: Lis= [                    {'ID': 1,' Age': 30},                    {'ID': 2,' Age': 40},                    {'ID': 3,' Age': 20},                    {'ID': 4,' Age': 40}] ll= Filter (Lambdadic:dic[' Age'] >= 40,lis)#returns an iterator                Print(List (LL))#[{' id ': 2, ' age ': +}, {' id ': 4, ' age ': +}]4. Map (Map function) syntax: Map (function,iterable) can be mapped according to each element in an iterative object. To perform the function example separately:<1>#calculates the square of each element in the list, returning the new column list            deffunc (E):returne*E MP= Map (func, [1, 2, 3, 4, 5])            Print(MP)Print(List (MP)) results:<map Object at 0x1037503c8>                [1, 4, 9, 16, 25]            <2>#rewrite it into Lambda .            Print(List (Map (LambdaX:X * x, [1, 2, 3, 4, 5])) Results: [1, 4, 9, 16, 25]            <3>#calculates the data in the same position as two lists andLst1 = [1, 2, 3, 4, 5] Lst2= [2, 4, 6, 8, 10]            Print(List (Map (LambdaX, y:x+y, Lst1, lst2))) Results: [3, 6, 9, 12, 15]

Python built-in function two

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.