Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Permutations
#1
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.


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.
Reply
#2
It's not clear where you are getting your permutations function from. If you are getting it from itertools, use itertools.combinations instead. Otherwise show use the code for your permutations function.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Yes I was using itertools. Thank you for your help.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Creating permutations of N size with no same elements next to one another (recursion) melmoman 1 1,503 Dec-18-2021, 09:39 PM
Last Post: Larz60+
  What happens to a <itertools.permutations object at 0x7fe3cc66af68> after it is read? Pedroski55 3 2,353 Nov-29-2020, 08:35 AM
Last Post: DeaD_EyE
  More Efficent Way of Generating Permutations/ w Rep ClassicalSoul 2 2,649 Aug-22-2019, 05:22 AM
Last Post: perfringo
  permutations calculation with a while instruction kwak86 2 2,238 Sep-10-2018, 02:09 PM
Last Post: kwak86
  How to find the number of permutations of a list of lists? JoeB 5 5,299 Nov-18-2017, 06:31 PM
Last Post: heiner55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020