Python 如何找到起点后最小值的指标?
我有一个Python 如何找到起点后最小值的指标?,python,Python,我有一个列表=[5,2,3,10,1,10,5,6,5,8,10] 我想找到某个点后最小值的索引 例如,如果我想在索引1之后找到最小值的索引,那么索引1是2,这意味着最小值是1,这是索引4 我想将其编码为def find_min(lst,index),其中lst是我的列表,index是起点 还需要解释。使用min找出min,然后使用索引获取索引 values = [5,2,3,10,1,10,5,6,5,8,10] afterThisPoint = 1 m = values[afterThisP
列表=[5,2,3,10,1,10,5,6,5,8,10]
我想找到某个点后最小值的索引
例如,如果我想在索引1之后找到最小值的索引,那么索引1是2,这意味着最小值是1,这是索引4
我想将其编码为def find_min(lst,index)
,其中lst
是我的列表,index
是起点
还需要解释。使用min找出min,然后使用索引获取索引
values = [5,2,3,10,1,10,5,6,5,8,10]
afterThisPoint = 1
m = values[afterThisPoint+1:].index(min(values[afterThisPoint+1:]))
print(m+afterThisPoint+1)
使用min找出min,然后使用索引获取索引
values = [5,2,3,10,1,10,5,6,5,8,10]
afterThisPoint = 1
m = values[afterThisPoint+1:].index(min(values[afterThisPoint+1:]))
print(m+afterThisPoint+1)
您要求的格式的函数
def find_min(lst, index):
list_to_check = lst[index:] # creating a list list_to_check with only elements starting from given index to last element
min_value = min(list_to_check) # found the minimum value in the new list list_to_check
return (list_to_check.index(min_value)+index) # list_to_check.index(min_value) gives the index of the minimum value in new list list_to_check. Since index from old list is needed, we add it with index
您要求的格式的函数
def find_min(lst, index):
list_to_check = lst[index:] # creating a list list_to_check with only elements starting from given index to last element
min_value = min(list_to_check) # found the minimum value in the new list list_to_check
return (list_to_check.index(min_value)+index) # list_to_check.index(min_value) gives the index of the minimum value in new list list_to_check. Since index from old list is needed, we add it with index
您可以对索引范围使用min函数,并间接指向最小值的列表。这将在一次通过数据时产生结果(与计算最小值并在列表中搜索相反):
您可以对索引范围使用min函数,并间接指向最小值的列表。这将在一次通过数据时产生结果(与计算最小值并在列表中搜索相反):
这是硬编码吗?比如专门为一个人写的?这是硬编码吗?特别是针对一个?我可以问一下为什么我需要在结尾处+索引吗?用注释@PythonUser428更新我的答案我可以问一下为什么我需要在结尾处+索引吗?用注释@PythonUser428更新我的答案吗