Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
HELP ME
#1
I wrote the code, but each digit with 3 digits could not reflect a different 4 permutation please help.

def comb(L): 
      
    for i in range(3): 
        for j in range(3): 
            for k in range(3): 
                  
              
                if (i!=j and j!=k and i!=k): 
                    print(L[i], L[j], L[k]) 
                      

comb([1, 2, 3, 4])
Larz60+ write Nov-23-2020, 03:58 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Fixed for you this time. Please use bbcode tags on future posts.
Reply
#2
try:
def comb(l):
    size = len(l)
    for i in range(size):
        for j in range(size):
            for k in range(size):
                if i != j and j != k and i != k:
                    print(l[i], l[j], l[k])


comb([1, 2, 3, 4])
Reply
#3
I would expect a function named "comb" to generate combination. Superjoe's code, and the code it looks like you are trying to write, make some kind of weird permutation where the values for each entry must be unique. Is that what you were trying to do?

If you need combinations or permutations for an assignment I suggest using the functions in the itertools standard library. Unless, of course, your assignment is to write a combinations function.
Reply


Forum Jump:

User Panel Messages

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