1

I have a function called ModelsProduct in which I am generating all possible combinations of "+/-" and "a,b,c,d,e". In the below code #producemodelsOne

modelsStepOne selects the all 8 combinations of +/-

modelsStepTwo selects the first value of list i-e a

modelsStepThree selects its first value of list i-e ('a','a')

modelsStepFour selects its first value of list i-e ('d','d')

modelsStepFive selects its first value of list i-e ('e','e')

which makes a combination of [+,+,+,+,+,+,+,+,a,a,a,d,d,e,e] and iterates so on for all possible combinations.

Following is the Output when you print it.

print (modelsOne[0])
print (modelsOne[1])

('+', '+', '+', '+', '+', '+', '+', '+', 'a', 'a', 'a', 'd', 'd', 'e', 'e')

('+', '+', '+', '+', '+', '+', '+', '+', 'a', 'a', 'a', 'd', 'e', 'e', 'e')

Question How to find a specific index value in all possible combinations e.g What will be the index value of possible combination [+,-,+,+,+,+,-,-,a,a,b,c,c,e,c] ?

Here is the code for making all possible combinations

def ModelsProduct(modelsOne, modelsTwo, modelsThree, modelsFour,modelsFive):

modelsStepOne = list(product("+-",repeat = 8)) ## It gives total 12288 model combinations
modelsStepThree = [('a','a'),('a','b'),('a','c'),('a','d'),('a','e'),('b','b'),('b','c'),('b','d'),('b','e'),('c','c'),('c','d'),('c','e')]
modelsStepFour = [('d','d'),('d','e')]
modelsStepFive = [('e','e')]



#produce modelsOne
modelsStepTwo = [('a',),('b',)]
for one in modelsStepOne:
    for two in modelsStepTwo:
        for three in modelsStepThree:
            for four in modelsStepFour:
                for five in modelsStepFive:
                    modelsOne.append(one+two+three+four+five)


#produce modelsTwo
modelsStepTwo = [('a',),('b',)]
for one in modelsStepOne:
    for two in modelsStepTwo:
        for three in modelsStepThree:
            for four in modelsStepFour:
                for five in modelsStepFive:
                    modelsTwo.append(one+two+three+four+five)


#produce modelsThree
modelsStepTwo = [('a',),('b',)]
for one in modelsStepOne:
    for two in modelsStepTwo:
        for three in modelsStepThree:
            for four in modelsStepFour:
                for five in modelsStepFive:
                    modelsThree.append(one+two+three+four+five)

#ModelsFour
modelsStepTwo = [('a',),('d',)]
for one in modelsStepOne:
    for two in modelsStepTwo:
        for three in modelsStepThree:
            for four in modelsStepFour:
                for five in modelsStepFive:
                    modelsFour.append(one+two+three+four+five)

 #ModelsFive
modelsStepTwo = [('a',),('e',)]
for one in modelsStepOne:
    for two in modelsStepTwo:
        for three in modelsStepThree:
            for four in modelsStepFour:
                for five in modelsStepFive:
                    modelsFive.append(one+two+three+four+five)

 return modelsOne, modelsTwo, modelsThree, modelsFour, modelsFive

modelsOne, modelsTwo,modelsThree, modelsFour,modelsFive = ModelsProduct(modelsOne, modelsTwo, modelsThree, modelsFour, modelsFive)
2
  • 2
    you can get the index of a element in a list by using modelsOne.index() so if you do modelsOne.index(('+', '+', '+', '+', '+', '+', '+', '+', 'a', 'a', 'a', 'd', 'd', 'e', 'e')) you will get 0, is it your question? Commented Jun 6, 2018 at 15:11
  • Yes, I wanted to print the index value of any possible combination. The combination can be [+,-,+,-,+,-,-,-,a,a,b,c,d,d,e,] or [+,+,+,-,-,-,+,-,a,a,a,b,b,b,d] Commented Jun 6, 2018 at 15:25

1 Answer 1

1

The method index() returns the index in list that obj appears. This method returns index of the found object otherwise raise an exception indicating that value does not find.

Example:

alist = [123, 'xyz', 'muhammad', 'abc'];
print ("Index for xyz : ", alist.index( 'xyz' ))
print ("Index for Muhammad : ", alist.index( 'muhammad' ) )

Index for xyz :  1
Index for Muhammad :  2

The problem discussed above in the question can be

 print("Index of ModelsOne :",modelsOne.index(('+', '+', '+', '+', '+', '+', '+', '+', 'a', 'a', 'a', 'd', 'd', 'e', 'e')))

OutPut:

Index of ModelsOne :6144
Sign up to request clarification or add additional context in comments.

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.