子表达式 [英] subexpressions

查看:75
本文介绍了子表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!


请帮助,有没有办法在lambda中使用子表达式?

例如,如果我想计算罪( x ^ 2)+ cos(x ^ 2)我必须编码:

lambda x:sin(x * x)+ cos(x * x)

如何制作x * x要评估一次吗?

Hello all!

Please help, is there way to use sub-expressions in lambda?
For example, if I want to calculate sin(x^2)+cos(x^2) I must code:
lambda x: sin(x*x)+cos(x*x)
How to make x*x to be evaluated once?

推荐答案

Sergey Dorofeev写道:
Sergey Dorofeev wrote:

Please求助,有没有办法在lambda中使用子表达式?

例如,如果我想计算sin(x ^ 2)+ cos(x ^ 2)我必须编码:

lambda x:sin(x * x)+ cos(x * x)

如何使x * x被评估一次?
Please help, is there way to use sub-expressions in lambda?
For example, if I want to calculate sin(x^2)+cos(x^2) I must code:
lambda x: sin(x*x)+cos(x*x)
How to make x*x to be evaluated once?


>>(lambda x:[sin(x2)+ cos(x2)for x2 in [x * x]] [0])(。5)== sin(.5 * .5)+
>>(lambda x: [sin(x2) + cos(x2) for x2 in [x*x]][0])(.5) == sin(.5*.5) +



cos(.5 * .5)

真实


真正的答案当然是:使用功能。


Peter

cos(.5*.5)
True

The real answer is of course: Use a function.

Peter




" Peter Otten" < __ ******* @ web.dewrote in message

news:f3 ************* @ news.t-online.com .. 。

"Peter Otten" <__*******@web.dewrote in message
news:f3*************@news.t-online.com...

Sergey Dorofeev写道:
Sergey Dorofeev wrote:

>请帮助,有没有办法使用子表达式在lambda?
例如,如果我想计算sin(x ^ 2)+ cos(x ^ 2)我必须编码:
lambda x:sin(x * x)+ cos(x * x)
如何使x * x评估一次?
>Please help, is there way to use sub-expressions in lambda?
For example, if I want to calculate sin(x^2)+cos(x^2) I must code:
lambda x: sin(x*x)+cos(x*x)
How to make x*x to be evaluated once?


>>>(lambda x:[sin(x2)+ cos(x2)for x2在[x * x]] [0])(。5)== sin(.5 * .5)+
>>>(lambda x: [sin(x2) + cos(x2) for x2 in [x*x]][0])(.5) == sin(.5*.5) +



cos(.5 * .5)

正确


真正的答案当然是:使用一个函数。

cos(.5*.5)
True

The real answer is of course: Use a function.



但是类似


lambda x:sin(y)+ cos(y)其中y = x * x




这可能是个PEP吗?如果没有直接的方法可以做到这一点。

But what about something like

lambda x: sin(y)+cos(y) where y=x*x

?
May be this could be a PEP? If there is no straight way to do this.


Sergey Dorofeev写道:
Sergey Dorofeev wrote:

" Peter Otten" < __ ******* @ web.dewrote in message

news:f3 ************* @ news.t-online.com .. 。
"Peter Otten" <__*******@web.dewrote in message
news:f3*************@news.t-online.com...

> Sergey Dorofeev写道:
>Sergey Dorofeev wrote:

>>请帮助,有没有办法在lambda中使用子表达式?
例如,如果我想计算sin(x ^ 2)+ cos(x ^ 2)我必须编码:
lambda x:sin(x * x)+ cos(x * x)
如何使x * x被评估一次?
>>Please help, is there way to use sub-expressions in lambda?
For example, if I want to calculate sin(x^2)+cos(x^2) I must code:
lambda x: sin(x*x)+cos(x*x)
How to make x*x to be evaluated once?


>>>>(lambda x:[sin(x2)+ cos(x2)for x2 in [x * x] ] [0])(。5)== sin(.5 * .5)+
>>>>(lambda x: [sin(x2) + cos(x2) for x2 in [x*x]][0])(.5) == sin(.5*.5) +


cos(.5 * .5)
True
<当然,真正的答案是:使用一个函数。

cos(.5*.5)
True

The real answer is of course: Use a function.



但是类似


lambda x:sin(y)+ cos(y)其中y = x * x




这可能是个PEP吗?如果没有直接的方法来做到这一点。


But what about something like

lambda x: sin(y)+cos(y) where y=x*x

?
May be this could be a PEP? If there is no straight way to do this.



def f(x):

y = x * x

return sin(y)+ cos( y)


什么不是直截了当的?


彼得

def f(x):
y = x*x
return sin(y) + cos(y)

What is not straightforward about that?

Peter


这篇关于子表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆