Python 如果存在重复项,如何查找列表元素的最大索引?
我有这样一份清单:Python 如果存在重复项,如何查找列表元素的最大索引?,python,Python,我有这样一份清单: animals = ['Goat', 'Tiger', 'Lion', 'Lion'] 如何找到'Lion'的最大索引 expected result : 4 'Lion'在索引3和4中,我想要最大索引(4)。您的索引结果是错误的,不是4而是3 Animal=['Goat'、'Tiger'、'Lion'、'a'、'Lion',1,5、'Lion',3] 动物=[“山羊”、“老虎”、“狮子”、“狮子”] def测试(列表1,word): rev_list=list1[:-1
animals = ['Goat', 'Tiger', 'Lion', 'Lion']
如何找到'Lion'
的最大索引
expected result : 4
'Lion'
在索引3和4中,我想要最大索引(4)。您的索引结果是错误的,不是4而是3
Animal=['Goat'、'Tiger'、'Lion'、'a'、'Lion',1,5、'Lion',3]
动物=[“山羊”、“老虎”、“狮子”、“狮子”]
def测试(列表1,word):
rev_list=list1[:-1]
idx_rev_list=rev_list.index(word)
列表长度=长度(列表1)
res=列表\u len-idx\u版本\u列表
返回res-1
res=试验(动物,“狮子”)
打印(res)
印刷品(动物[res])
<代码>反转列表?相反,你可以这样做,<代码>动物。索引(“,”1)<代码>,如果“狮子”在列表中间,它就不起作用了。