The advanced function of Python Learning (II.)

Source: Internet
Author: User

The built-in function Zip function:  zip () is a python built-in function that takes a series of iterated objects as parameters, combining the corresponding elements in the object in order into a tuple, each of which contains the elements of the corresponding ordinal position in the original sequence. The list is then returned by 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. In the case of all parameters of the same length, zip () is similar to map (), without parameters the ZIP () returns an empty list example: l1 = [1,2,3,4]l2 = [' A ', ' B ', ' C ', ' d '] For i in zip (L1,L2):     print (i) returns the result: [(1, ' a '), (2, ' B '), (3, ' C '), (4, ' d ')] The filter function: The  filter function is another useful high-order function built into  Python , and the filter () function receives a function f and a list, the function  f  The function is to judge each element, return  true or  false,filter () automatically filters out the non-conforming elements according to the result of the judgment, and returns a new list that is composed of eligible elements. Example: Def add (n): return  n / 2 == 1for i in filter (add, [1,2,3,4]):     print (i)          #打印2     map function:  map () Yes  Python  built-in high-order function, which receives a function  f  and a  list, and by putting the function f on each of the  list  elements in turn, to get a new   list  and returns. Example: L = [1,2,3,4]def add (x):     return x+1fOr i in map (add,l):     print (i) Reduce function: the  reduce () function receives a parameter similar to  map () , a function  f, a list, but the behavior and  map () are different, reduce () the incoming function  f  must receive two parameters, reduce () repeatedly calls the function f on each element of the list, and returns an example of the final result value: From functools import reducel = [1,2,3,4,5]print (Reduce (lambda x,y: x+y, &NBSP;L,&NBSP;10)       #参数10是可选参数, as the initial value     two, anonymous function lambda     1.lambda is just an expression, and the function body is much simpler than def. The body of a     2.lambda is an expression, not a block of code. Only a finite amount of logic can be encapsulated in a lambda expression. The &NBSP;&NBSP;&NBSP;&NBSP;3.LAMBDA expression is a function of sketching. Allows the definition of a function to be embedded within the code.          Example:     f = lambda x,y:  x+y         #x, y is a parameter, X+y is an expression, and then assigns a value to F    print (f )          #返回f的内存地址     print (f ())       #返回表达式的值         l = [' Alex ',  ' Wupeiqi ',  ' Yuanhao ']    res = map (lambda  x: x +  ' _SB ',  l)     print (res)     print (list (res))         nums = [2,4,6,9]    res1 =  map (lambda x: x**2, nums)     print (list (res1))               Recursive call      call the function itself directly or indirectly during a function call. This is the recursive invocation of the function      Example:     def age (n):         if n == 1:             return 18        return age (n-1)  + 2     print (age (5))         l = [1, 2,  10,33,53,71,73,75,77,85,101,201,202,999,11111]        def search (FIND_NUM,SEQ):         if len (seq)  == 0:             print (' not exists ')              return        mid_index=len (seq)//2         mid_num=seq[mid_index]         print (Seq,mid_num)         if find_num > mid_ num:             #in  the right             seq=seq[mid_index+1:]             search (FIND_NUM,SEQ)          elif find_num < mid_num:             #in  the left             seq=seq[:mid_index]             search (FIND_NUM,SEQ)          else:            print (' Find it ')     search (77,l)      Four, package, module     import spam         import  Import module do three things     1. Create a new namespace      2. With the new namespace as the global namespace, execute the file code     3. Get a module name spam, point to spam.py generated namespaces      import spam as x     #将spam用x代替, suitable for use when module name is too long          from ... import ...     advantages:  Convenient, no prefix added      Cons:   easy to work with the current file name Conflict          Package:  will have linked modules together, effectively avoid the problem of module name conflict, make the organization structure clearer.      a standard application architecture:    app/         __init__.py         #__init__. py Do some initialization work can be empty files, can also write some initial configuration          a/             __init__.py            a.py         b/             __init__.py            b.py      from app.a import a     from app.b.b import  test    #  "." The left must be the package name      a.test ()           Test ()            can add a package to the location of the SYS.PATH environment variable, or add the location of the package to the environment variable 

This article is from the "Linux Technology" blog, so be sure to keep this source http://xiaojishu.blog.51cto.com/4278020/1930999

The advanced function of Python Learning (II.)

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.