Python。将 str('+') 转换为数学运算

发布于 2024-09-27 09:24:20 字数 247 浏览 1 评论 0原文

如何将str('+')转换为数学运算? 例如:

a = [0,1,2] # or a = ['0','1','2']
b = ['+','-','*']
c = int(a[0]+b[0]+a[1])

换句话说,如何将 str('-1*2') 转换为 int(),而无需 for i in c: if i == “+”:... 谢谢。

How transform str('+') to mathematical operation?
For example:

a = [0,1,2] # or a = ['0','1','2']
b = ['+','-','*']
c = int(a[0]+b[0]+a[1])

In other words, how transform str('-1*2') to int(), without for i in c: if i == '+': ...
Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

若水般的淡然安静女子 2024-10-04 09:24:20

您还可以使用操作员模块:

import operator as op
#Create a mapping between the string and the operator:
ops = {'+': op.add, '-': op.sub, '*': op.mul}

a = [0,1,2]
b = ['+','-','*']

#use the mapping
c = ops[b[0]](a[0], a[1])

You can also use the operator module:

import operator as op
#Create a mapping between the string and the operator:
ops = {'+': op.add, '-': op.sub, '*': op.mul}

a = [0,1,2]
b = ['+','-','*']

#use the mapping
c = ops[b[0]](a[0], a[1])
萌化 2024-10-04 09:24:20

我认为你正在寻找 eval(),但我建议使用其他东西......
但是,

>>> eval('-1*2')
-2

eval 会“执行”您传递给它的字符串,就像代码一样。所以这对于安全来说是相当危险的,特别是如果参数是用户输入的话......

在这种情况下我建议使用解析库,例如 ply http://www.dabeaz.com/ply/
对于这样的事情来说,使用起来非常简单而且非常有效:)

i thin you're looking for eval(), but i advice to use something else...
however,

>>> eval('-1*2')
-2

eval 'executes' the string you pass to it, like code. so it's quite dangerous for security, especially if the parameters are user input...

in this case i suggest to use parsing library, such as ply http://www.dabeaz.com/ply/
that for such thing is really simple to use and very effective :)

巷雨优美回忆 2024-10-04 09:24:20

如果你的数学表达式适合 Python 语法,但你不喜欢 eval(你应该这样做),你可以查看 python 的 ast 模块(文档)。它将表达式解析为可以迭代的抽象语法树。您可以评估 Python 的有限子集,并在遇到表达式语法之外的任何内容时抛出错误。

If your math expressions will fit Python syntax but you are skeered of eval (you should be) you can look into python's ast module (docs). It will parse the expression into an abstract syntax tree you can iterate over. You can evaluate a limited subset of Python and throw errors if you encounter anything outside your expression grammar.

心不设防 2024-10-04 09:24:20

像其他人所说的那样使用 eval 但首先对其进行过滤。

>>> s = '1 + 12 / 2 - 12*31'
>>> allowed = set(' 1234567890()+-/*\\')
>>> if allowed.issuperset(s):
...     eval(s)
... 
-365

Use eval like everyone else is saying but filter it first.

>>> s = '1 + 12 / 2 - 12*31'
>>> allowed = set(' 1234567890()+-/*\\')
>>> if allowed.issuperset(s):
...     eval(s)
... 
-365
情绪 2024-10-04 09:24:20

使用评估:

> eval(str('-1*2'))
> -2

Use eval:

> eval(str('-1*2'))
> -2
青衫负雪 2024-10-04 09:24:20

Dett,

对整个字符串进行简单的评估...但是请注意,如果用户输入字符串,评估是有风险的,除非您首先进行一些解析,

x = '-1*2'
y = eval(x)

然后 y 将是整数值。

Dett,

A Simple eval on the whole string... However be aware that if the user inputs the string, eval is risky, unless you do some parsing first

x = '-1*2'
y = eval(x)

y will then be the integer value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文