Python 表达式等于结果时如何打印

Python 表达式等于结果时如何打印,python,generator,Python,Generator,我被困在这个问题上,是否有任何方法可以打印出与用户输入的结果数相等的表达式,而不是所有可能的排列 编辑:如果你可以看到我有一个名为result的变量,现在有了所有的置换表达式,我想打印与结果相等的表达式如果你将整数转换成字符串,那么你可以将每个置换连接到字符串,你可以使用eval来计算它-即如果eval1+2+3+4==10: 编辑:对于@lbragile版本,返回 要添加到@furas answer中,我认为以下代码语法更好: vals = [1, 2, 3, 4] operators = [

我被困在这个问题上,是否有任何方法可以打印出与用户输入的结果数相等的表达式,而不是所有可能的排列


编辑:如果你可以看到我有一个名为result的变量,现在有了所有的置换表达式,我想打印与结果相等的表达式如果你将整数转换成字符串,那么你可以将每个置换连接到字符串,你可以使用eval来计算它-即如果eval1+2+3+4==10:

编辑:对于@lbragile版本,返回


要添加到@furas answer中,我认为以下代码语法更好:

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

def expressions(values):

    if len(values) == 1:
        return [values]  # has to return list of lists

    results = [] # create list for all lists

    for i in range(len(values)):

        forward = values[:]
        val = forward.pop(i)

        for op in operators:
            for rest in expressions(forward):
                results.append([val, op] + rest)

    return results # return all lists at once


for x in expressions(vals):
    x = ' '.join(map(str, x))
    if eval(x) == result:
        print(x, '=', eval(x))
输出:
你能给我更多的信息吗?这个问题不清楚。请在你的文章中包含任何代码和数据,而不是图片。编辑,粘贴code@furas请问您为什么使用收益率而不是收益率?@lbragile我使用问题和OP使用收益率的代码。我甚至没有检查代码中的内容。@furas我看到了。我认为这是因为pop减少了每次递归调用的列表大小,而yield只是生成输出。我不知道为什么使用return会给出:回溯最近的调用last:File main.py,第20行,在表达式中的x中vals:File main.py,第13行,在表达式中rest的表达式中forward:File main.py,第13行,在表达式中rest的表达式中forward:File main.py,第14行,在表达式中return[val,op]+rest TypeError:只能将列表not int连接到list@lbragile对于return,您必须创建包含所有结果的列表,然后返回此列表。因为它返回列表列表,所以它还需要在返回[values]中使用[]来创建列表列表-列表中有一个列表values@lbragile我还使用return添加到了answer版本中,这将给出与结果变量相同类型的输出RHS 10,在本例中为aka integer。
vals = [1, 2, 3, 4]
operators = ['+', '*', '-', '/']
result = 10

def expressions(values):

    if len(values) == 1:
        yield values

    for i in range(len(values)):

        forward = values[:]
        val = forward.pop(i)


        for op in operators:
            for rest in expressions(forward):
                yield [val, op] + rest


for x in expressions(vals):
    x = ' '.join(map(str, x))
    if eval(x) == result:
        print(x, '=', eval(x))
vals = [1, 2, 3, 4]
operators = ['+', '*', '-', '/']
result = 10

def expressions(values):

    if len(values) == 1:
        return [values]  # has to return list of lists

    results = [] # create list for all lists

    for i in range(len(values)):

        forward = values[:]
        val = forward.pop(i)

        for op in operators:
            for rest in expressions(forward):
                results.append([val, op] + rest)

    return results # return all lists at once


for x in expressions(vals):
    x = ' '.join(map(str, x))
    if eval(x) == result:
        print(x, '=', eval(x))
def expressions(vals):
  global operators

  if len(vals) == 1:
    yield vals

  else:
    for i in range(len(vals)):
      forward = vals[:]
      val = forward.pop(i)

      for op in operators:
        for rest in expressions(forward):
          yield [val, op] + rest

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

for x in expressions(vals):
  x = ' '.join(map(str, x))
  if eval(x) == result:
    print("{} = {}".format(x, int(eval(x))))
1 + 2 + 3 + 4 = 10
1 + 2 + 4 + 3 = 10
1 + 3 + 2 + 4 = 10
1 + 3 + 4 + 2 = 10
1 + 4 + 2 + 3 = 10
1 + 4 + 3 + 2 = 10
1 * 2 * 3 + 4 = 10
1 * 3 * 2 + 4 = 10
1 * 3 * 4 - 2 = 10
1 * 4 + 2 * 3 = 10
1 * 4 + 3 * 2 = 10
1 * 4 * 3 - 2 = 10
2 + 1 + 3 + 4 = 10
2 + 1 + 4 + 3 = 10
2 + 3 + 1 + 4 = 10
2 + 3 + 4 + 1 = 10
2 + 4 + 1 + 3 = 10
2 + 4 + 3 + 1 = 10
2 * 1 * 3 + 4 = 10
2 * 3 + 1 * 4 = 10
2 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