Search code examples
pythonlistpermutationvariantinvariants

Permutation of lists, combining and not combining list position


I found a good script for the permutation of lists, combining and not combining list position on a tutorial and modified.

Here is the script:

`your text`
list1 = [1]
list2 = [6, 7, 8]
list3 = [9, 10, 11]
list4 = [12]

print("The original lists are : " + str(list1) +
      " " + str(list2) +
      " " + str(list3) +
      " " + str(list4))

res = [[i, j, k, l] for i in list1
                for j in list2
                for k in list3
                for l in list4]

print("All possible permutations are:")
for perm in res:
    print(*perm)

Resulting in this:

1 6 9 12
1 6 10 12
1 6 11 12
1 7 9 12
1 7 10 12
1 7 11 12
1 8 9 12
1 8 10 12
1 8 11 12

My problem is I would also like to have the result permutated result of some list, like list 3 and list 4, adding this result to the previous one:

1 9 6 12
1 10 6 12
1 11 6 12
1 9 7 12

Etc...

Is this possible? I cant find a tutorial/solution to add this to the script.

I'm quite new on python programming. Thanks in advance.


Solution

  • I'm not sure what result you want to achieve. But as I understand your question, you want to create the list res from the lists list1, list2, list3 & list4, which should contain the following values:

    1  6  9 12
    1  6 10 12
    1  6 11 12
    1  7  9 12
    1  7 10 12
    1  7 11 12
    1  8  9 12
    1  8 10 12
    1  8 11 12
    1  9  6 12
    1 10  6 12
    1 11  6 12
    1  9  7 12
    1 10  7 12
    1 11  7 12
    1  9  8 12
    1 10  8 12
    1 11  8 12
    

    If that's your goal, you can easily achieve it by extending your code as follows:

    list1 = [1]
    list2 = [6, 7, 8]
    list3 = [9, 10, 11]
    list4 = [12]
    
    print("The original lists are : " + str(list1) +
          " " + str(list2) +
          " " + str(list3) +
          " " + str(list4))
    
    res = [[i, j, k, l] for i in list1
                    for j in list2
                    for k in list3
                    for l in list4]
    
    res_2 = [[i, k, j, l] for i in list1
                    for j in list2
                    for k in list3
                    for l in list4]
    
    for perm in res_2:
        res.append(perm)
    
    print("All possible permutations are:")
    for perm in res:
        print(*perm)
    

    Update: To delete all lines that contain duplicates, you can extend the code as follows:

    list1 = [1]
    list2 = [6, 7, 8, 9]
    list3 = [9, 10, 11]
    list4 = [12]
    
    print("\nThe original lists are : " + str(list1) +
          " " + str(list2) +
          " " + str(list3) +
          " " + str(list4))
    
    res = [[i, j, k, l] for i in list1
                        for j in list2
                        for k in list3
                        for l in list4]
    
    res_2 = [[i, k, j, l]   for i in list1
                            for j in list2
                            for k in list3
                            for l in list4]
    
    for perm in res_2:
        res.append(perm)
    
    print("\nAll possible permutations with duplicates in lines are:")
    for perm in res:
        print(*perm)
    
    delete_index = []
    for i, line in enumerate(res):
        for j, element in enumerate(line):
            for k in range(j, len(res[0])):
                if j != k:
                    if element == line[k]:
                        delete_index.append(i)
    
    for i in sorted(delete_index, reverse=True):
        del res[i]        
    
    print("All possible permutations are:")
    for perm in res:
        print(*perm)
    

    By executing the code you will get the following output:

    The original lists are : [1] [6, 7, 8, 9] [9, 10, 11] [12]
    
    All possible permutations with duplicates in lines are:
    1 6 9 12
    1 6 10 12
    1 6 11 12
    1 7 9 12
    1 7 10 12
    1 7 11 12
    1 8 9 12
    1 8 10 12
    1 8 11 12
    1 9 9 12
    1 9 10 12
    1 9 11 12
    1 9 6 12
    1 10 6 12
    1 11 6 12
    1 9 7 12
    1 10 7 12
    1 11 7 12
    1 9 8 12
    1 10 8 12
    1 11 8 12
    1 9 9 12
    1 10 9 12
    1 11 9 12
    
    All possible permutations are:
    1 6 9 12
    1 6 10 12
    1 6 11 12
    1 7 9 12
    1 7 10 12
    1 7 11 12
    1 8 9 12
    1 8 10 12
    1 8 11 12
    1 9 10 12
    1 9 11 12
    1 9 6 12
    1 10 6 12
    1 11 6 12
    1 9 7 12
    1 10 7 12
    1 11 7 12
    1 9 8 12
    1 10 8 12
    1 11 8 12
    1 10 9 12
    1 11 9 12
    

    As you can see, the two lines that contained the elements 1 9 9 12 were deleted from the list.