问题描述
我们有以下python列表:[1,2,3,10]
我要完成以下操作:创建一个接受列表并从算术运算列表中找出数字的函数:['+', '-', '/','*']
哪些组合给我们6作为答案.我们不想重复,所以我们不想在解决方案中使用2*3
和3*2
.我们确实要列出未使用的数字,所以是(这里是1和10).对于2/1*3=6.0
,2*3/1=6.0
,3/1*2=6.0
,3*2/1=6.0
来说,都被认为是等效的,因为我们使用相同的数字而不管操作如何,并且没有使用10.我希望该功能具有足够的通用性,以便可以使用它从那里开始1到9的数字.感谢您的帮助.我尝试使用itertools和permutation获取所有可能组合的列表,但这似乎不必要,并产生了问题2/1*3=6.0
,2*3/1=6.0
,3/1*2=6.0
,3*2/1=6.0
包含在列表中,很难过滤掉
We've got the following python list: [1,2,3,10]
I would like to accomplish the following: Create a function that takes in the list and figures out from the list of arithmetic operations: ['+', '-', '/','*']
which combinations give us 6 as the answer. We don't want repetition so we don't want 2*3
and 3*2
in our solution. We do want to list the numbers we haven't used so that's (1 and 10 here). Same for 2/1*3=6.0
, 2*3/1=6.0
, 3/1*2=6.0
, 3*2/1=6.0
are all considered equivalent since we use the same numbers regardless of operations and have not used 10. I want the function to be general enough so that I can use it from there for numbers from 1 to 9.Your help is appreciated. I tried using itertools and permutation to get a list of all possible combinations but this seems unnecessary and produces the problem that 2/1*3=6.0
, 2*3/1=6.0
, 3/1*2=6.0
, 3*2/1=6.0
are included in the list and this is difficult to filter out.
使用itertools的示例:
Example where I got using itertools:
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','10'])):
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]
lst_stages=[]
for equation in lst_expr:
if eval(equation) == 6:
lst_stages.append(equation)
eq = str(equation) + '=' + str(eval(equation))
print(eq)
推荐答案
这里是可能的解决方案.我们需要保留已用数字的排序元组,以避免2 * 3和3 * 2之类的重复项.
Here is possible solution. We need to keep a sorted tuple of used numbers to avoid duplicates like 2*3 and 3*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','10'])):
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]
numbers = (operands[0],)
i = 1
for operator in operators:
exp += operator + operands[i]
numbers += (operands[i],)
i += 1
lst_expr += [{'exp': exp, 'numbers': tuple(sorted(numbers))}]
lst_stages=[]
numbers_sets = set()
for item in lst_expr:
equation = item['exp']
numbers = item['numbers']
if numbers not in numbers_sets and eval(equation) == 6:
lst_stages.append(equation)
eq = str(equation) + '=' + str(eval(equation))
print(eq, numbers)
numbers_sets.add(numbers)
输出:
2*3=6 ('2', '3')
1+10/2=6.0 ('1', '10', '2')
2/1*3=6.0 ('1', '2', '3')
这篇关于在数字列表上应用算术运算而无需在python中重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!