gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 09:22:13 26 4
gpt4 key购买 nike

我有一个名为 ModelsProduct 的函数,在其中生成 "+/-""a,b,c,d, e”。在下面的代码中#productmodelsOne

modelsStepOne 选择 +/- 的所有 8 种组合

modelsStepTwo 选择列表的第一个值,即 a

modelsStepThree 选择列表的第一个值,即 ('a','a')

modelsStepFour 选择列表的第一个值,即 ('d','d')

modelsStepFive 选择列表的第一个值,即 ('e','e')

组合 [+,+,+,+,+,+,+,+,a,a,a,d,d,e,e] 并迭代所有可能的组合。

以下是打印时的输出。

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

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

('+', '+', '+', '+', '+', '+', '+', '+', '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)

最佳答案

方法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

关于python - 如何在Python列表中查找某些值的特定索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50723384/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com