Python:在字典中将不等式传递给评估 [英] Python: Pass inequality as string in dict for evaluation

查看:129
本文介绍了Python:在字典中将不等式传递给评估的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在函数中将不等式传递给函数进行评估。如果作为字符串传递,是否有评估不平等的方法?或者我必须传递一个不等式的表示并使用if / else语句来生成符号?解决方案

你的问题有点儿含糊不清,但它听起来像你想评估一个包含表达式的字符串(如 x> 5 )。而不是这样做,这是不必要的复杂,并可能是一个安全隐患,只需定义一个函数,无论是传统的方式或使用lambda。

  def gt5(x):
return x> 5

  gt5 = lambda x:x> 5 

这些都是等价的;现在你可以传递 gt5 ,不管你喜欢什么时候,只要把它叫做

  y = 6 
如果gt5(y):
...

由于杰拉特的

a>答案表明,操作员模块可能对这样的事情也很有用。 / p>




现在我知道您正在处理用户字符串,我肯定会建议创建一个将字符串映射到函数的字典。 (也许这就是你的标题中的含义?)将用户域字符串传递给 getattr 在很多方面看起来很糟糕。如果你想添加一个不符合 operator 属性的字符串,会发生什么?如果用户传递一个对应于私有属性的字符串会发生什么?最好创建一个将字符串映射到函数的自定义字典。字典允许你指定你想接受的字符串。

  func_dict = {
'lt':operator.lt,
'gt':operator.gt ,
'nand':lambda x,y:not(x and y)
}

通过使用 getattr + 运算符,您仍然可以通过字符串列表构建字典你想接受。例如:

  func_dict = dict((s,getattr(operator,s))for ['lt',' gt'])

然后更新 func_dict like所以:

  custom_dict = {
'nand':lambda x,y:not(x and y)


func_dict.update(custom_dict)

从那里你可以轻松调用字典中的函数,如下所示:

 >>> func_dict ['lt'](5,7)
True


I need to pass inequalities to a function for evaluation within the function. Is there a way to evaluation the inequality if passed as a string? Or must I pass a representation of the inequality and use if/else statements to generate the sign?

解决方案

Your question is a little vague, but it sounds like you want to evaluate a string containing an expression (such as x > 5). Rather than doing that, which is unnecessarily complex, and potentially a security hazard, just define a function, either in the conventional way or using lambda.

def gt5(x):
    return x > 5

or

gt5 = lambda x: x > 5

These are both equivalent; now you can pass around gt5 however you like, and when the time comes, you simply call it

y = 6
if gt5(y):
    ...

As Gerrat's answer suggests, the operator module may also be useful for things like this.


Now that I know you are processing user strings, I would definitely suggest creating a dictionary that maps strings to functions. (Perhaps that's what you meant in your title?) Passing userland strings into getattr seems bad in a number of ways. What happens if you want to add a string that doesn't correspond to an attribute of operator? What happens if the user passes in a string corresponding to a private attribute? Better to create a custom dictionary mapping strings to functions. A dict allows you to specify just those strings you want to accept.

func_dict = {
    'lt'   : operator.lt,
    'gt'   : operator.gt,
    'nand' : lambda x, y: not (x and y)
}

You could still save yourself work by using getattr + operator to build the dict from a list of strings that you want to accept. Something like:

func_dict = dict((s, getattr(operator, s)) for s in ['lt', 'gt'])

Then update func_dict like so:

custom_dict = {
    'nand' : lambda x, y: not (x and y)
}

func_dict.update(custom_dict)

From there you can easily call functions in the dict like so:

>>> func_dict['lt'](5, 7)
True

这篇关于Python:在字典中将不等式传递给评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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