(1) Lambda
A lambda is a python that creates an anonymous function in the form of the following syntax:
Lambda [arg1[, arg2, ... argn]]: expressionUse examples:
#使用法一
fun = lambda x,y:x+y
print Fun (1,2)
#输出结果为:
3
#使用法二
print (lambda
(1, 100))Print reduce (ADD1, range (1, 100), 20)# 4950 (Note: 1+2+...+99)# 4970 (Note: 1+2+...+99+20)The function is called on an iteration of the item order in sequence, and if there is starting_value, it can also be called as an initial value, for example to sum the list:4 Lambda:g = lambda s:s + ". FSH"Print g ("haha")Print (Lambda x:x * 2) (3)# # HAHA.FSH# 6This is an interesting syntax that
expressions, which is more convenientAs you can see from the above, lambda expressions can be conveniently used instead of simple functions.Two or three-dollar operation1, see what is ternary operation2, Python's ternary operation format is as follows:result= value 1 if x1>>>deff (x, y):2 returnX-yifX>yElseABS (xy)3 #if x is greater than Y, it returns an X-y value, otherwise it returns the absolute values of X- y4 5>>> F (3,4)#36>>> F (4,3)7>>>de
right, to reduce the sequence to a single subsequence example, (λx,y:x+y,[1,2,3,4,5]) (1+2)) (+3) +4). If it is initially present, it is placed before the calculated sequence item and is left as the default value when the sequence is empty.1 def Addnum (num1,num2): 2 return num1+num234 list2={1,2,3,4,5,6}5Print Reduce (Addnum, List2)Code interpretation: Calculate the value of 1+2 first, then pass the computed value as the first parameter into Addnum, the second parameter is 3, and so on. Th
the Python dictionary is useful in some cases, such as when initializing a dictionary.How to use:x = {}X.setdefault (1,0)OUT[15]: 0X[2] = 10XOUT[17]: {1:0, 2:10}X.setdefault (2,1)OUT[18]: 10Alex YuSource: http://www.cnblogs.com/biaoyu/This article is copyright to the author and the blog Park, Welcome to reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to the original link, otherwise r
Lambda expression, this bird thing is to define a function in one line of statements.
Http://woodpecker.org.cn/diveintopython/power_of_introspection/lambda_functions.html
>>> def f (x):
... Return x*2 ...
>>> F (3)
6
>>> g = lambda x:x*2 >>> g (3)
6
>>> (lambda x:x*2) ( 3) # 6
The List is the most frequently used data type in
One, lambda expression 1 >>> def add (x, y): #定义一个加法函数 2 return x+y #返回两个参数的相加的值 3 4 >>> z=f (3, 4) 5 >>> print (z) 6 7 #调用加法函数返回7 7 >>> Lambda x,y:x+ Y 8 As you can see from the above, lambda expressions can be conveniently used instead of simple functions.Two or three-dollar operation1, see what is ternary operation2, Python's ternary operation format is as fol
Map function:
Prototype: Map (function, sequence), which maps a list to another list, How to use: def f (x): Return x**2 L = Range (1,10) Map (f,l) OUT[3]: [1, 4, 9, 16, 25, 36, 49, 64, 81]
Reduce function
Prototype: reduce (function, sequence, startvalue), which is to summarize a list as an output,How to use: def f2 (x, y): Return X+y Reduce (f1,l) OUT[7]: 45 Reduce (f2,l,10) OUT[8]: 55
Filter function
Prototype: filter (function, sequence), which filters o
One or three-tuple operator# when the condition is met 1 o'clock, the res= value is 1; otherwise res= value 2 if Else value 2To illustrate:res=10# Simple If Else statement if abs (res) >0: = 10**2else: Res =0# implemented with ternary operators above if elseifelse 0Second, lambda expression# to define a simple function def func (x, y): return x+y#lambda expression implements Func () function
def groupBy (FN): def Go (LST): = {} for in lst: ifelse m.update ({fn (v): [v]}) #如果存在dict, append to the corresponding key, or none if it does not exist, then update a new key to return m return = GroupBy (lambdais 1) grpby ([1, 2, 3]) The Python implements the GroupBy function. Grpby = GroupBy (lambda x:x%2 is 1), the result of Grpby ([1, 2, 3]) is {True: [1, 3], False: [2]}
One of the most important aspects of Python is functional programming (functional programming), where you can transfer functions where parameters and values are transferred.Lambda x:x%3 = = 0and the following equivalence:def by_three (x): return x%3 = = 0We don't need to really give the function a name, it can also work and get the result. This is why the function that lambda creates is an anonymous func
be processed,initval is the initial value 2. Code1 array=[1,2,3,4,5,6,7]2Lambda x,y:x+y3 ans2= reduce (add , array,0)4print ans3. Function prototypes1 defreduce (function,array,initval):2TMP =list (array)3 ifInitval isNone:4Ans = list.pop (0)#take the first element of the list and delete5 Else:6Ans =Initval7 foreachinchtmp:8Ans =function (Ans,each)9 returnans;Ten Oneans2= Reduce (add,array,0)5. Apply1. Basic formAPPLY(FUnCtion,(parameter of f u nCtion ) )is used to re
/tuple/string and an initial value, and the return value is numeric.#Coding=utf-8" "Created on 2016-12-14@author:jennifer Project: Usage of filter, map, reduce, lambda in Python" "#1.LAMBDA usage, before the colon is the argument, after the colon is the expression, the return value, the simplest functionPrint[(LambdaX:X*X) (x) forXinchRange (11)]#results: [0, 1,
example, you can use to sum a list:defAdd (x, y):returnX +yPrintReduce (Add, range (1, 11)) Note:1+2+3+4+5+6+7+8+9+10reduce (Add, range (1, 11), 20) Note:1+2+3+4+5+6+7+8+9+10+20 iv.Lambda This is Python support an interesting syntax that allows you to quickly define a single line of the smallest function, similar to the C language of the macro, these functions, called Lambda, is borrowed from Lisp, can be
Dictionary is a tuple of the list, variable name is a bit ambiguous, here is to express a tuple composed of the list ha.The reason for using dictionary is that the dictionary is converted to list by simulation.Sorts the list and sorts them according to the elements specified by the lambda function.The example is sorted according to the No. 0 element of each tuple, which is also the key of the dictionary;Python
Return has two functions:1. Used to return the run result of a function, or to call another function. such as the Max () function>>> def fun (A, B): 2. The sign of the end of the function. As soon as the return is run, the function is forced to end. The program after return will not be executed.>>> def fun (A, B): Return (' Don't run the following statement! ') #函数不在往下运行. If A>b:print (a) >>> fun (8,1) #运行到 return, do not run behind the IF statement ' do not run behind the statement
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.