def find_last_occurance(range, target):
"""
找到范围中最后一次出现目标值的位置并返回1,如果找不到返回0。
"""
index = -1
for i in range:
if i == target:
index = range.index(i, index + 1)
if index != -1:
return 1
else:
return 0
使用方法:
range = [2, 3, 5, 2, 6, 7, 8, 2]
target = 2
result = find_last_occurance(range, target)
print(result)
输出:
1