在变量列表上尝试所有操作组合

时间:2016-09-16 07:28:36

标签: python combinations

我有一个值列表,如:

values = [1, 2, 3, 4]

我希望尝试此列表中的所有组合,如:

1 + 2
1 + 3
1 + 4
1 * 2
1 * 3
1 * 4
1 + 2 * 3
1 + 2 * 4
1 + 3 * 4

以最简洁的方式获得所有这些可能的操作组合的最简单方法是什么?

我会想象有两个列表,[1,2,3,4]和[+,*, - ,/]然后取所有长度的所有组合,然后用所有组合填充空白

选择[1,2,3],然后选择操作的所有排列并将它们组合在一起。这看起来很混乱,我希望有更清晰的方法来编写代码吗?

4 个答案:

答案 0 :(得分:6)

这是一个递归解决方案,可以从数字和数字构建表达式。运算符,然后使用eval来计算它:

vals = [1, 2, 3]
operators = ['+', '*', '-', '/']

def expressions(values):
    # Base case, only one value left
    if len(values) == 1:
        yield values

    # Iterate over the indexes
    for i in range(len(values)):
        # Pop value from given index and store the remaining values
        # to be used with next recursion
        forward = values[:]
        val = forward.pop(i)

        # Yield all value, operator, subexpression combinations
        for op in operators:
            for rest in expressions(forward):
                yield [val, op] + rest

for expr in expressions(vals):
    expr = ' '.join(str(x) for x in expr)
    print('{} = {}'.format(expr, eval(expr)))

输出(部分):

1 + 2 + 3 = 6
1 + 2 * 3 = 7
1 + 2 - 3 = 0
1 + 2 / 3 = 1.6666666666666665
1 + 3 + 2 = 6
1 + 3 * 2 = 7
1 + 3 - 2 = 2
1 + 3 / 2 = 2.5
1 * 2 + 3 = 5
1 * 2 * 3 = 6
1 * 2 - 3 = -1
1 * 2 / 3 = 0.6666666666666666
1 * 3 + 2 = 5
1 * 3 * 2 = 6
1 * 3 - 2 = 1
1 * 3 / 2 = 1.5
1 - 2 + 3 = 2
1 - 2 * 3 = -5
1 - 2 - 3 = -4

答案 1 :(得分:2)

这个怎么样(因为操作数和运算符的顺序很重要,我们必须使用排列)?

from itertools import chain, permutations

def powerset(iterable):
  xs = list(iterable)
  return chain.from_iterable(permutations(xs,n) for n in range(len(xs)+1) )

lst_expr = []
for operands in map(list, powerset(['1','2','3','4'])):
    n = len(operands)
    #print operands
    if n > 1:
        all_operators = map(list, permutations(['+','-','*','/'],n-1))
        #print all_operators, operands
        for operators in all_operators:
            exp = operands[0]
            i = 1
            for operator in operators:
                exp += operator + operands[i]
                i += 1
            lst_expr += [exp]

print ', '.join(lst_expr)

使用以下输出:

1+2, 1-2, 1*2, 1/2, 1+3, 1-3, 1*3, 1/3, 1+4, 1-4, 1*4, 1/4, 2+1, 2-1, 2*1, 2/1, 2+3, 2-3, 2*3, 2/3, 2+4, 2-4, 2*4, 2/4, 3+1, 3-1, 3*1, 3/1, 3+2, 3-2, 3*2, 3/2, 3+4, 3-4, 3*4, 3/4, 4+1, 4-1, 4*1, 4/1, 4+2, 4-2, 4*2, 4/2, 4+3, 4-3, 4*3, 4/3, 1+2-3, 1+2*3, 1+2/3, 1-2+3, 1-2*3, 1-2/3, 1*2+3, 1*2-3, 1*2/3, 1/2+3, 1/2-3, 1/2*3, 1+2-4, 1+2*4, 1+2/4, 1-2+4, 1-2*4, 1-2/4, 1*2+4, 1*2-4, 1*2/4, 1/2+4, 1/2-4, 1/2*4, 1+3-2, 1+3*2, 1+3/2, 1-3+2, 1-3*2, 1-3/2, 1*3+2, 1*3-2, 1*3/2, 1/3+2, 1/3-2, 1/3*2, 1+3-4, 1+3*4, 1+3/4, 1-3+4, 1-3*4, 1-3/4, 1*3+4, 1*3-4, 1*3/4, 1/3+4, 1/3-4, 1/3*4, 1+4-2, 1+4*2, 1+4/2, 1-4+2, 1-4*2, 1-4/2, 1*4+2, 1*4-2, 1*4/2, 1/4+2, 1/4-2, 1/4*2, 1+4-3, 1+4*3, 1+4/3, 1-4+3, 1-4*3, 1-4/3, 1*4+3, 1*4-3, 1*4/3, 1/4+3, 1/4-3, 1/4*3, 2+1-3, 2+1*3, 2+1/3, 2-1+3, 2-1*3, 2-1/3, 2*1+3, 2*1-3, 2*1/3, 2/1+3, 2/1-3, 2/1*3, 2+1-4, 2+1*4, 2+1/4, 2-1+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, 4/2*3-1, 4+3-1*2, 4+3-1/2, 4+3*1-2, 4+3*1/2, 4+3/1-2, 4+3/1*2, 4-3+1*2, 4-3+1/2, 4-3*1+2, 4-3*1/2, 4-3/1+2, 4-3/1*2, 4*3+1-2, 4*3+1/2, 4*3-1+2, 4*3-1/2, 4*3/1+2, 4*3/1-2, 4/3+1-2, 4/3+1*2, 4/3-1+2, 4/3-1*2, 4/3*1+2, 4/3*1-2, 4+3-2*1, 4+3-2/1, 4+3*2-1, 4+3*2/1, 4+3/2-1, 4+3/2*1, 4-3+2*1, 4-3+2/1, 4-3*2+1, 4-3*2/1, 4-3/2+1, 4-3/2*1, 4*3+2-1, 4*3+2/1, 4*3-2+1, 4*3-2/1, 4*3/2+1, 4*3/2-1, 4/3+2-1, 4/3+2*1, 4/3-2+1, 4/3-2*1, 4/3*2+1, 4/3*2-1

答案 2 :(得分:1)

我的解决方案使用一个值列表,因此对给定参数的顺序应用操作,作为正常算术评估顺序的替代。例如,1 + 3 + 5 + 7将为(((1 + 3) +5) + 7)。但是,它需要所有可能的值排列,因此无论如何都列出了所有可能性。 它还允许任何操作作为参数,甚至是lambda表达式。

我使用itertools。

from itertools import combinations_with_replacement
from itertools import permutations
from itertools import product
from itertools import chain

要显示我们调用的所有表达式:

def list_all(operations, values):
    if len(values) == 1:
        return values
    permutes = []
    ops = []
    expressions = []
    # for 4 values we want combinations with 2, 3 and 4 values.
    for subset in range(2, len(values)+1):
        # One could use combinations instead of permutations if all ops
        # where know to be transitive.
        permutes.append(list(permutations(values, subset)))
        ops.append(list(combinations_with_replacement(operations, subset - 1)))
    for o, v in zip(ops, permutes):
        expressions.append(list(itertools.product(o, v)))
    return expressions

为了评估它们,execute获取list_all的输出:

def execute(expressions):
    results = []
    # Flatten all expressions into a single iterator
    for ops, arguments in chain.from_iterable(expressions):
        oplist = list(ops)
        arglist = list(arguments)
        res = oplist.pop(0)(arglist.pop(0), arglist.pop(0))
        while len(arglist) > 0:
            res = oplist.pop(0)(res, arglist.pop(0))
        results.append(res)
    return results

使用示例:

from operator import add
from operator import sub
import pprint
expressions = list_all([add, sub, lambda x,y : sqrt(x*x + y*y)], [1, 2, 3, 4])
results = execute(expressions)

# Display list with operators, arguments and results.
pprint.pprint(zip(chain.from_iterable(expressions), results))

答案 3 :(得分:0)

以下是使用operatoritertools实现此目的的更简单,更简洁的代码。

同时检查为何不使用evalIs using eval in Python a bad practice?

from itertools import product, combinations, chain
from operator import add, sub, mul, div, mod, floordiv

my_list = [1, 2, 3, 4, 0]
my_operations = {'+': add, '-': sub, '/': div, '*': mul, '%': mod, '//': floordiv}

for nums, action in chain(product([i for i in combinations(my_list, 2)], my_operations)):
    try:
        result = my_operations[action](nums[0], nums[1])
    except ZeroDivisionError:
        result = 'infinite'
    finally:
        print '{} {} {} = {}'.format(nums[0], action, nums[1], result)

以下是上述代码的输出:

1 // 2 = 0
1 % 2 = 1
1 + 2 = 3
1 * 2 = 2
1 - 2 = -1
1 / 2 = 0
1 // 3 = 0
1 % 3 = 1
1 + 3 = 4
1 * 3 = 3
1 - 3 = -2
1 / 3 = 0
1 // 4 = 0
1 % 4 = 1
1 + 4 = 5
1 * 4 = 4
1 - 4 = -3
1 / 4 = 0
1 // 0 = infinite
1 % 0 = infinite
1 + 0 = 1
1 * 0 = 0
1 - 0 = 1
1 / 0 = infinite
2 // 3 = 0
2 % 3 = 2
2 + 3 = 5
2 * 3 = 6
2 - 3 = -1
2 / 3 = 0
2 // 4 = 0
2 % 4 = 2
2 + 4 = 6
2 * 4 = 8
2 - 4 = -2
2 / 4 = 0
2 // 0 = infinite
2 % 0 = infinite
2 + 0 = 2
2 * 0 = 0
2 - 0 = 2
2 / 0 = infinite
3 // 4 = 0
3 % 4 = 3
3 + 4 = 7
3 * 4 = 12
3 - 4 = -1
3 / 4 = 0
3 // 0 = infinite
3 % 0 = infinite
3 + 0 = 3
3 * 0 = 0
3 - 0 = 3
3 / 0 = infinite
4 // 0 = infinite
4 % 0 = infinite
4 + 0 = 4
4 * 0 = 0
4 - 0 = 4
4 / 0 = infinite