python – 获取条件字符串返回值:True或False

python – 获取条件字符串返回值:True或False,第1张

概述我有条件字符串,如下所示, condition1 = "((2=2) or (3=1)) and (1=1)"condition2 = "((2=3) or (1=1)) and (4=5)" 以上两个条件分别给出了输出True和False. 更多解释: condition1的工作原理, if ((2==2) or (3==1)) and (1==1): Return Trueel 我有条件字符串,如下所示,

condition1 = "((2=2) or (3=1)) and (1=1)"condition2 = "((2=3) or (1=1)) and (4=5)"

以上两个条件分别给出了输出True和False.

更多解释:

condition1的工作原理,

if ((2==2) or (3==1)) and (1==1):      Return Trueelse:      Return False

condition1的输出:True

condition2的工作原理,

if ((2==3) or (1==1)) and (4==5):      Return Trueelse:      Return False

condition2的输出:True

更新:

对不起朋友,

我有条件字符串,如上面的条件1和条件2.

我想在将条件解析为字符串时执行函数,并且该函数执行如if – else并返回布尔值

请帮忙…

谢谢
Chintan

解决方法 警告:使用eval是 potentially dangerous,您永远不应该评估不受信任的输入(例如任何类型的用户输入).

In [1]: condition1 = "((2=2) or (3=1)) and (1=1)"   ...: condition2 = "((2=3) or (1=1)) and (4=5)"   ...: In [2]: eval(condition1.replace('=','=='))Out[2]: TrueIn [3]: eval(condition2.replace('=','=='))Out[3]: False
总结

以上是内存溢出为你收集整理的python – 获取条件字符串返回值:True或False全部内容,希望文章能够帮你解决python – 获取条件字符串返回值:True或False所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1192747.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存