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


Messages In This Thread
Permutations - by mikke3141 - Dec-23-2019, 05:48 PM
RE: Permutations - by ichabod801 - Dec-23-2019, 05:55 PM
RE: Permutations - by mikke3141 - Dec-23-2019, 06:09 PM

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,585 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,430 Nov-29-2020, 08:35 AM
Last Post: DeaD_EyE
  More Efficent Way of Generating Permutations/ w Rep ClassicalSoul 2 2,720 Aug-22-2019, 05:22 AM
Last Post: perfringo
  permutations calculation with a while instruction kwak86 2 2,286 Sep-10-2018, 02:09 PM
Last Post: kwak86
  How to find the number of permutations of a list of lists? JoeB 5 5,387 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