我正在尝试使用itertools迭代数学运算符。
通常使用array
[1, 2, 3]
combinations
,使用1
1,2
1,3
2,3
1,2,3
我可以得到结果:
array
等
我想在[1, 2, 3]
的{{1}}上使用此方式:
1+2+3
1+2-3
1+2/3
1+2*3
1-2+3
1-2-3
1-2/3
1-2*3
...
出现并给出了等式的结果。
我该怎么做呢?
答案 0 :(得分:1)
以下是我将如何处理它:
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
答案 1 :(得分:1)
不是优雅的(在膝盖上制作)但是有效,只是为了指出我的逻辑。 我们的想法是按正确的顺序逐一减少列表。 E.g:
数据:1 * 2 + 3 * 4
代码:
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)等情况。
答案 2 :(得分: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)))
答案 3 :(得分:0)
使用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
等带括号的表达式。