文章目录
前言
一、Lambda函数
提示:本章介绍lambda函数,感觉用处与列表表达式重合
1.语法
lambda函数(匿名函数)的语法只包含一个语句:
lambda [arg1 [,arg2,.....argn]]:expression
2.常见用法
(1)将lambda函数赋值给一个变量,通过变量直接调用lambda函数
jisuan = lambda x, y: x+y
for i in range(5):
print(jisuan(i, i+1))
(2)与map函数连用
result = list(map(lambda x: x ** 2, [1, 2, 3, 4, 5]))
print(result)
(3)与filter函数连用
newlist = filter(lambda x: x % 3 == 0, [1, 2, 3])
print(list(newlist))
3、例题
(1)与map函数连用
spells = ["protego", "accio", "expecto patronum", "legilimens"]
shout_spells_list = list(map(lambda x: x+'!!!', spells))
print(shout_spells_list)
二、函数
The following notation will be used throughout
- Y Y Y: Output
- L L L: Total labor units
- K K K: Physical capital
We write a production function F F F, that transforms labor ( L L L) and capital ( K K K) into output ( Y Y Y) as
Y = F ( K , L ) = z K α L 1 − α Y = F(K, L)= z K^{\alpha} L^{1-\alpha} Y=F(K,L)=zKαL1−α
1.函数定义
def cobb_douglas(K, L):
# Create alpha and z
z = 1
alpha = 0.33
return z * K**alpha * L**(1 - alpha)
If, for any
K
,
L
K, L
K,L, we multiply
K
,
L
K, L
K,L by a value
γ
\gamma
γ
then
- If
Y
2
Y
1
<
γ
\frac{Y_2}{Y_1} < \gamma
Y1Y2<γ then we say the production function has
decreasing returns to scale. - If
Y
2
Y
1
=
γ
\frac{Y_2}{Y_1} = \gamma
Y1Y2=γ then we say the production function has
constant returns to scale. - If
Y
2
Y
1
>
γ
\frac{Y_2}{Y_1} > \gamma
Y1Y2>γ then we say the production function has
increasing returns to scale.
def returns_to_scale(K, L, gamma):
y1 = cobb_douglas(K, L)
y2 = cobb_douglas(gamma*K, gamma*L)
y_ratio = y2 / y1
return y_ratio / gamma
2.函数注释
有了函数注释后,cobb_douglas?即可显示
def cobb_douglas(K, L):
"""
Computes the production F(K, L) for a Cobb-Douglas production function
Takes the form F(K, L) = z K^{\alpha} L^{1 - \alpha}
We restrict z = 1 and alpha = 0.33
"""
return 1.0 * K**(0.33) * L**(1.0 - 0.33)