如何在列表Python中找到某些值的特定索引

时间:2018-06-06 14:51:14

标签: python arrays python-3.x list

我有一个名为 ModelsProduct 的函数,其中我生成" + / - " &#34的所有可能组合; A,b,C,d,E" 即可。在下面的代码中 #producemodelsOne

modelsStepOne选择 +/-

的全部8种组合

modelsStepTwo选择列表的第一个值i-e a

modelsStepThree选择列表的第一个值i-e (' a',' a')

modelsStepFour选择列表的第一个值i-e (' d',' d')

modelsStepFive选择列表的第一个值i-e (' e'' e')

它组合了[+,+,+,+,+,+,+,+,a,a,a,d,d,e,e]并对所有可能的组合进行迭代。

以下是打印时的输出。

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

(' +',' +',' +',' +',' +& #39;,' +',' +',' +',' a',' a' ,' a','' d',' e'' e')

(' +',' +',' +',' +',' +& #39;,' +',' +',' +',' a',' a' ;,' a'' d'' e',' e'' e')

问题 如何在所有可能的组合中找到特定的指数值,例如可能组合的指数值是什么[+, - ,+,+,+,+, - , - ,a,a,b,c,c ,e,c]?

以下是制作所有可能组合的代码

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)

1 个答案:

答案 0 :(得分:1)

方法 index()返回obj出现的列表中的索引。此方法返回找到的对象的索引,否则引发一个异常,指示找不到值。

示例:

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

问题中上面讨论的问题可以是

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

输出:

Index of ModelsOne :6144