Вы можете создать рекурсивную функцию для генерации всех возможных перестановок с заданными условиями, а затем отфильтровать, чтобы сохранить только те списки, которые суммируются с требуемым значением:
def list_results(a, b):
return [i for i in permutations(b) if sum(i) == a]
def permutations(d, current = []):
if len(current) == d:
yield current
else:
for i in range(10):
yield from permutations(d, current+[i])
print(list_results(5, 3))
Выход:
[[0, 0, 5], [0, 1, 4], [0, 2, 3], [0, 3, 2], [0, 4, 1], [0, 5, 0], [1, 0, 4], [1, 1, 3], [1, 2, 2], [1, 3, 1], [1, 4, 0], [2, 0, 3], [2, 1, 2], [2, 2, 1], [2, 3, 0], [3, 0, 2], [3, 1, 1], [3, 2, 0], [4, 0, 1], [4, 1, 0], [5, 0, 0]]
Редактировать: чуть быстрее повлечет за собой дополнительную проверку в рекурсивной функции:
import time
def timeit(f):
def wrapper(*args, **kwargs):
c = time.time()
results = list(f(*args, **kwargs))
print("Result from function '{}' achieved in {}".format(f.__name__, abs(c-time.time())))
return results
return wrapper
@timeit
def outer_permutations():
def permutations1(d, b, current = []):
if len(current) == d:
yield current
else:
for i in range(10):
if len(current) < 2 or sum(current+[i]) == b:
yield from permutations1(d, b, current+[i])
yield from permutations1(3, 5)
@timeit
def list_results(a, b):
return [i for i in permutations(b) if sum(i) == a]
v = outer_permutations()
v1 = list_results(3, 5)
Выход:
Result from function 'outer_permutations' achieved in 0.0006079673767089844
Result from function 'list_results' achieved in 0.09148788452148438
Обратите внимание, что выходные данные обеих функций:
[[0, 0, 5], [0, 1, 4], [0, 2, 3], [0, 3, 2], [0, 4, 1], [0, 5, 0], [1, 0, 4], [1, 1, 3], [1, 2, 2], [1, 3, 1], [1, 4, 0], [2, 0, 3], [2, 1, 2], [2, 2, 1], [2, 3, 0], [3, 0, 2], [3, 1, 1], [3, 2, 0], [4, 0, 1], [4, 1, 0], [5, 0, 0]]