Python function VII (built-in function)

Source: Internet
Author: User

One. Built-in functions

1. Important built-in functions

  Are all with key: Max min Map filter returns an iterator

Sorted returns the list function name after the

1.1 Max

DIC = {'K1': 20,'K2': 30,'K3': 100}deffunc (x):returnDIC[X]L1= Max (Dic,key=func,)#(the output is that DIC defaults to the output of DIC's key), the function returns a value, that is, the key to select the largest valuePrint(L1)#K3DiC= {3:20,2:30,1:100}deffunc (x):returnXL1= Max (Dic,key=func,)#(the output is that DIC defaults to the output of DIC's key), the function returns the key value, that is, the largest key is selectedPrint(L1)#3

1.2 Zip allows you to place multiple, iterative objects with a minimum number of iterations to return an iterator ( zipper method)

L1 = [1,2,3,4,5]l2= ['Wusir','Alex','Taibai']l3= ['*',"**"]Print(Zip (L1,L2))#<zip object at 0x00000161edc19f08> forIinchZip (l1,l2,l3):Print(i) Results: (1,'Wusir','*')(2,'Alex','**')

1.3 Map

Print([I*i forIinchRange (5)])#[0, 1, 4, 9, +]Print(Map (abs,[1,2,3,-4,-5,-7]))#<map object at 0x0000015b0dcc75c0> iterator forIinchMap (abs,[1,2,3,-4,-5,-7]):#ABS is built-in function, absolute value    Print(i)defFunc (x):returnx*x forIinchMap (Func,range (5)):#Func is a function of its own writing.    Print(i)defFunc (x):returnx*x forIinchMap (func,[0,1,2,3,4]):    Print(i)

1.4 Filter filters out eligible element outputs

Print  for inch if i% 2 = = 0]    #[2, 4]def func1 (x):return x% 2 = 0for in filter (func1,[1,2,3,4,5]):    print(i)#  Result:  #  2#  4

1.5 sorted returns a list, forms a new list, and returns

List.sort () Change the original list

L1 = [1,4,3,5,7,8]Print(Sorted (L1,))#[1, 3, 4, 5, 7, 8]L= ['FSDAFSA','Fddsaf','QQQ','FGASJDLG;DSJFG']deffunc1 (x):returnlen (x)Print(Sorted (L,key=len))#[' QQQ ', ' fddsaf ', ' FSDAFSA ', ' FGASJDLG;DSJFG '] forIinchSorted (l,key=func1):Print(i)

2. Introduction to the rest of the built-in functions

2.1 # All

Print (Any ([' ', 1,0]))     # true        any at least one element is true and returns true  similar to orprint(All ([', 1,0]))     #  False       All elements are true and return true     similar to and

2.2 Ord input character, return Unicode corresponding encoding location

CHR input encoding position, return Unicode corresponding character

#Print (Ord (' A '))#Print (ord (' B '))#Print (Ord (' Medium '))#Results:# the#98#20013Print(Chr (97))#aPrint(Chr (20013))#inPrint(ASCII ('a'))#' A 'Print(ASCII ('Country'))#\U56FD

2.3%r repr True Colours

' alex%r '% ('sb')print(name)                 #Alex ' sb ' Print (Repr ('[up]'))       # ' [up] '

2.4 Slice () slice

L1 = [11,22,33,44]print(l1[1::2= Slice (1,4,2)      # name a rule, after the slice  Print(L1[SL])

2.5 reversed () forms a new iterator.

L1 = [11,33,22,44,'Alex']l1.reverse ()print(L1)      #[' Alex ', A, a, a, a, a.   change is the original list, can only print the original list L2 = reversed (L1)    # This is an iterator iteratorfor   in L2:    print(i)

Two. anonymous functions

Typically used in conjunction with built-in functions

Format: Lambda x:x

L1 = ['234',' A','Fsdafa','Fdsagag']defFunc1 (x):returnX% 2 = =0 forIinchFilter (func1,[1,2,3,4,5]):    Print(i)#2 4 forIinchFilterLambdaX:len (x) >= 3,['234',' A','Fsdafa','Fdsagag']):    Print(i)#234 Fsdafa Fdsagag

Python function VII (built-in function)

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.