0

I am new at codding.I have a question.How do I add text to combinations and permutations I've created? Also, is there a way to delete repetitive states in a repetitive permutation, that is, to print like one? If so, how is it done?

colour = list(itertools.permutations(["green","green","green","yellow","yellow"],5))
print("first one is " + colour[0] + " and second one is " + colour[1] + " and third one is " + colour[2] + " and fourth one is " + colour[3] + " and fifth one is " + colour[4])

I wrote such code, but it doesn't work. At the beginning of the permutations of the top five elements, I want to add texts such as first to the first row and second to the second row, how can I do this? I hope I was able to explain. I will be glad if you can help.

1
  • Can you show what you would like it to print if it "worked"? Commented Aug 30, 2024 at 17:39

2 Answers 2

1

I am not sure I know what you are asking but to get distinct permutations with multiple same items, you could use from sympy.utilities.iterables import multiset_permutations.

from sympy.utilities.iterables import multiset_permutations

colour = list(multiset_permutations(["green","green","green","yellow","yellow"]))

for perm in colour:
    print(perm)

This prints:

['green', 'green', 'green', 'yellow', 'yellow']
['green', 'green', 'yellow', 'green', 'yellow']
['green', 'green', 'yellow', 'yellow', 'green']
['green', 'yellow', 'green', 'green', 'yellow']
['green', 'yellow', 'green', 'yellow', 'green']
['green', 'yellow', 'yellow', 'green', 'green']
['yellow', 'green', 'green', 'green', 'yellow']
['yellow', 'green', 'green', 'yellow', 'green']
['yellow', 'green', 'yellow', 'green', 'green']
['yellow', 'yellow', 'green', 'green', 'green']
Sign up to request clarification or add additional context in comments.

Comments

0

If you need to create a list of all possible color combinations, you can use the following:

colours = [ "green", "yellow" ]
aux = [ None ] * 5
out = []

  // we verify that the items are not all the same
def isNotEqual( arr ):
    for i in range( 4 ):
        if arr[ i ] != arr[ i + 1 ]:
            return True
    return False
    
def create( aux, position ):
    if position < 5:
        for k in range( 2 ): 
            aux[ position ] = colours[ k ]
            create( aux, position + 1 )
    else:
        if isNotEqual( aux ):
            out.append( aux.copy() )

create( aux, 0 )
print( out )

As you can see, recursion is used to obtain the combinations, in each execution of create the value of position is taken to choose which item of aux to instantiate, the for assigns one of the values of colours, then recursion is performed, passing position + 1 to change the instantiated item.
When position reaches “5”, it enters the else and if isNotEqual returns “true” it saves a copy of aux in out.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.