Dec-23-2019, 05:48 PM
Hi,
I'm new to Python and trying to learn the basics. How can I only show unique lists as a result so that if the list values are just in a different order to an already found result, it would not show it as one of the possible alternatives. So for example if the code has already found (1,2,3,4) as a results if would not show (4,3,2,1) as another result.
I'm new to Python and trying to learn the basics. How can I only show unique lists as a result so that if the list values are just in a different order to an already found result, it would not show it as one of the possible alternatives. So for example if the code has already found (1,2,3,4) as a results if would not show (4,3,2,1) as another result.
numlist = [1, 2, 3, 4, 5]
target_sum = 10
for length in range(2, len(numlist) + 1):
perms = list(permutations(numlist, length))
print("checking {} permutations of length {}".format(len(perms), length))
for p in perms:
s = sum(p)
if s == int(target_sum):
print("sum({}) = {}".format(list(p), sum(p)))I appreciate your help.
