【问题标题】:How to Iterate through arithmetic operators across a static, excecutable formula in Python?如何在 Python 中的静态可执行公式中遍历算术运算符?
【发布时间】:2017-06-05 23:45:01
【问题描述】:

我正在尝试使用 itertools 来遍历数学运算符。 通常使用[1, 2, 3]array,使用combinations 我可以获得结果:

1
1,2
1,3
2,3
1,2,3

等等

我想在[1, 2, 3]array 上使用它,这样:

1+2+3
1+2-3
1+2/3
1+2*3
1-2+3
1-2-3
1-2/3
1-2*3
...

出现并给出等式的结果。

我该怎么做?

【问题讨论】:

  • operator 模块中有执行标准算术运算的函数。
  • 有两种可能的方法:一种是将公式创建为字符串,然后使用eval 对其进行评估;另一个,使用来自operator 模块的函数。
  • 看看int__add____sub____mul____div__方法。
  • 是不是只能从加减法开始?
  • 它并不一定要从任何特定的东西开始,它可以按任何顺序进行。

标签: python itertools


【解决方案1】:

一种推广到任意数量的操作数的解决方案,并保持运算符的正常优先级:

from itertools import product

operands = [1, 2, 3, 4]
operators = [ '+', '*', '-', '//' ] # change '//' to '/' for floating point division
for opers in product(operators, repeat=len(operands)-1):
    formula = [ str(operands[0]) ]
    for op, operand in zip(opers, operands[1:]):
        formula.extend([op, str(operand)])
    formula = ' '.join(formula)
    print('{} = {}'.format(formula, eval(formula)))

【讨论】:

  • 这真是太好了。但是,通过将“//”更改为“/”,它最终得到与“//”相同的答案。所以划分略有偏差。
  • 呃,你用的是 Python 2.x 吗?在这种情况下,是的,/// 之间没有区别,除非你添加这个:from __future__ import division
【解决方案2】:

以下是我的处理方法:

import itertools
import operator

首先列出所有可能的组合:

funcs = [operator.add, operator.sub, operator.mul, operator.div]

combos = list(itertools.product(funcs, repeat=2))

>>[(<function operator.add>, <function operator.add>),
 (<function operator.add>, <function operator.sub>),
 (<function operator.add>, <function operator.mul>),
 (<function operator.add>, <function operator.div>),
 (<function operator.sub>, <function operator.add>),
 (<function operator.sub>, <function operator.sub>),
 (<function operator.sub>, <function operator.mul>),
 (<function operator.sub>, <function operator.div>),
 (<function operator.mul>, <function operator.add>),
 (<function operator.mul>, <function operator.sub>),
 (<function operator.mul>, <function operator.mul>),
 (<function operator.mul>, <function operator.div>),
 (<function operator.div>, <function operator.add>),
 (<function operator.div>, <function operator.sub>),
 (<function operator.div>, <function operator.mul>),
 (<function operator.div>, <function operator.div>)]

然后我们将遍历这个列表来解决所有可能的结果:

for fn in combos:
    print 'This combo {} yielded this result {}'.format(fn, fn[1](fn[0](*seq[:2]), seq[-1]))

This combo (<built-in function add>, <built-in function add>) yielded this result 6
This combo (<built-in function add>, <built-in function sub>) yielded this result 0
This combo (<built-in function add>, <built-in function mul>) yielded this result 9
This combo (<built-in function add>, <built-in function div>) yielded this result 1
This combo (<built-in function sub>, <built-in function add>) yielded this result 2
This combo (<built-in function sub>, <built-in function sub>) yielded this result -4
This combo (<built-in function sub>, <built-in function mul>) yielded this result -3
This combo (<built-in function sub>, <built-in function div>) yielded this result -1
This combo (<built-in function mul>, <built-in function add>) yielded this result 5
This combo (<built-in function mul>, <built-in function sub>) yielded this result -1
This combo (<built-in function mul>, <built-in function mul>) yielded this result 6
This combo (<built-in function mul>, <built-in function div>) yielded this result 0
This combo (<built-in function div>, <built-in function add>) yielded this result 3
This combo (<built-in function div>, <built-in function sub>) yielded this result -3
This combo (<built-in function div>, <built-in function mul>) yielded this result 0
This combo (<built-in function div>, <built-in function div>) yielded this result 0

编辑:这是一种遵循操作规则的方式

ops = ['+','-','*','/']

combos = list(itertools.product(ops, repeat=2))

for tup in list(itertools.product(combos, [seq])):
    print 'These operations {} evaluate to this ---> {}'.format(tup[0],eval(''.join(*zip(seq[0],tup[0][0],seq[1],tup[0][1],seq[-1]))))

These operations ('+', '+') evaluate to this ---> 6
These operations ('+', '-') evaluate to this ---> 0
These operations ('+', '*') evaluate to this ---> 7
These operations ('+', '/') evaluate to this ---> 1
These operations ('-', '+') evaluate to this ---> 2
These operations ('-', '-') evaluate to this ---> -4
These operations ('-', '*') evaluate to this ---> -5
These operations ('-', '/') evaluate to this ---> 1
These operations ('*', '+') evaluate to this ---> 5
These operations ('*', '-') evaluate to this ---> -1
These operations ('*', '*') evaluate to this ---> 6
These operations ('*', '/') evaluate to this ---> 0
These operations ('/', '+') evaluate to this ---> 3
These operations ('/', '-') evaluate to this ---> -3
These operations ('/', '*') evaluate to this ---> 0
These operations ('/', '/') evaluate to this ---> 0

【讨论】:

  • 那么,你的意思是This combo (&lt;built-in function sub&gt;, &lt;built-in function mul&gt;) yielded this result -3 是 1 - 2 * 3 === 1 - 6 == -3? :) 操作的优先级如何?)
  • 不尊重bodmas rule。 1+2*3 不应导致 9。
  • 是的,你说得对,我没有考虑操作顺序,一秒钟我会修改
  • 谢谢你,我认为它比我要找的要重一些,但我感谢你的努力。
  • 添加了遵循操作规则的方法
【解决方案3】:

不是一个优雅的(膝盖制作的)但有效,只是为了指出我的逻辑。 这个想法是按照正确的顺序一个一个地减少列表。例如:

数据:1 * 2 + 3 * 4

  • 在第 1 步之后(第一个 * 评估):2 + 3 * 4
  • 在第 2 步之后(第二个 * 评估):2 + 12
  • 在第 3 步之后(+ 评估):14

代码:

import operator
import itertools

data = [1.0, 2.0, 3.0, 4.0]
operators_1 = [operator.mul, operator.div] # this operators have priority over that below
operators_2 = [operator.add, operator.sub]

def processOps(formula, data, operators):
    res_formula = list(formula)
    result = list(data)
    for op in formula:
        if op not in operators: continue

        i = res_formula.index(op)
        result = result[:i] + [op(result[i], result[i + 1])] + result[i + 2:]
        res_formula.remove(op)

        if len(result) == 1:
            break

    return (res_formula, result)

for f in itertools.product(operators_1 + operators_2, repeat=len(data)-1):
    result = list(data)
    formula = list(f)
    formula, result = processOps(formula, result, operators_1)
    formula, result = processOps(formula, result, operators_2)
    print f, result

UDP 这个更新后的逻辑可以正确处理 (1 * 2) + (3 / 4) 之类的情况。

【讨论】:

  • 我在这里遇到了一些错误。 For: (, , ) [4.166666666666667] 其中应该是1/2*3+4,答案应该是5.5
  • 我看到了你的下一个编辑,这很好用。谢谢你:)
  • @NameyMcNamo,也谢谢你提出这个问题 - 让它工作对我来说真的很有趣)
【解决方案4】:

使用 operator 模块中的相应函数并迭代它们。

import itertools
import operator

ops = [operator.add, operator.sub, operator.mul, operator.div]

for f1, f2 in itertools.product(*ops, repeat=2):
    print f1(array[0], f2(array[1], array[2]))

现在,如果 array 可以有任意长度,它就会变得有点棘手。

for operations in itertools.product(*ops, repeat=len(array)-1):
  result = operations[0](array[0], array[1])
  for op, operand in zip(operations[1:], array[2:]):
      result = op(result, operand)
  print(result)

上述结构避免了必须知道每个操作的适当标识元素。

如果您想遵循优先级(看起来很可能),您需要创建一个表达式并使用eval 对其进行评估(适用标准警告)。

for ops in itertool.product("+", "-", "*", "/", repeat=len(array)-1):
  expr = "%s%s%s" % (array[0], ops[0], array[1])
  for op, operand in zip(ops[1:], array[2:]):
    expr = "%s%s%s" % (expr, op, operand)
  result = eval(expr)

除了1+2*3之外,我把它作为一个练习来扩展它以产生像(1+2)*3这样的括号表达式。

【讨论】:

  • len(array) 替换为len(array)-1 - 运算符的数量比操作数的数量少一。
  • 谢谢; operationsarray 在对 zip 的调用中被不同的起始索引切片,这让我觉得有些不对劲,但我并没有停下来思考什么。
  • 我尝试运行这些,但它说:TypeError: 'int' object is not iterable 我认为它指的是 2。
  • 我总是忘记; repeat 必须指定为关键字参数。
猜你喜欢
  • 2018-06-22
  • 1970-01-01
  • 2013-05-16
  • 1970-01-01
  • 2013-04-16
  • 2012-09-12
  • 2017-04-15
  • 1970-01-01
  • 2014-10-20
相关资源
最近更新 更多