Python搜索函数
我想写一个搜索函数,它接受一个值x和一个排序序列,并通过从第一个元素开始遍历序列中的元素来返回值应该到达的位置。x在列表中应该到达的位置应该是第一个位置,以便它小于或等于列表中的下一个元素Python搜索函数,python,Python,我想写一个搜索函数,它接受一个值x和一个排序序列,并通过从第一个元素开始遍历序列中的元素来返回值应该到达的位置。x在列表中应该到达的位置应该是第一个位置,以便它小于或等于列表中的下一个元素 Example:>>> search(-5, (1, 5, 10))——0 >>> search(3, (1, 5, 10))——1 既然有人回答了一个类似的问题,我就画一个你可能想做什么的大致框架 声明一个列表并用你的东西填充它; mylist=[1
Example:>>> search(-5, (1, 5, 10))——0
>>> search(3, (1, 5, 10))——1
既然有人回答了一个类似的问题,我就画一个你可能想做什么的大致框架 声明一个列表并用你的东西填充它; mylist=[1,2,3,5,5,6,7] 然后做一个函数,迭代列表
def my_func( x, mylist):
for i in mylist:
if((mylist[i] == x)|| (mylist[i] > x)):
return i
给定列表中的3(1,2,3,4,5),函数应该返回索引2。
给定列表中的3(1,2,4,5,6),它仍然应该返回2
您可能想检查我的python代码,因为我没有检查这段代码是否有错误,我假设您了解一些python,如果您有框架,您应该破解它。哦,python关心我做的tabbibg。既然有人回答了一个类似的问题,我就画一个你可能想做什么的粗略框架 声明一个列表并用你的东西填充它; mylist=[1,2,3,5,5,6,7] 然后做一个函数,迭代列表
def my_func( x, mylist):
for i in mylist:
if((mylist[i] == x)|| (mylist[i] > x)):
return i
给定列表中的3(1,2,3,4,5),函数应该返回索引2。
给定列表中的3(1,2,4,5,6),它仍然应该返回2
您可能想检查我的python代码,因为我没有检查这段代码是否有错误,我假设您了解一些python,如果您有框架,您应该破解它。哦,python关心我做的tabbibg。既然有人回答了一个类似的问题,我就画一个你可能想做什么的粗略框架 声明一个列表并用你的东西填充它; mylist=[1,2,3,5,5,6,7] 然后做一个函数,迭代列表
def my_func( x, mylist):
for i in mylist:
if((mylist[i] == x)|| (mylist[i] > x)):
return i
给定列表中的3(1,2,3,4,5),函数应该返回索引2。
给定列表中的3(1,2,4,5,6),它仍然应该返回2
您可能想检查我的python代码,因为我没有检查这段代码是否有错误,我假设您了解一些python,如果您有框架,您应该破解它。哦,python关心我做的tabbibg。既然有人回答了一个类似的问题,我就画一个你可能想做什么的粗略框架 声明一个列表并用你的东西填充它; mylist=[1,2,3,5,5,6,7] 然后做一个函数,迭代列表
def my_func( x, mylist):
for i in mylist:
if((mylist[i] == x)|| (mylist[i] > x)):
return i
给定列表中的3(1,2,3,4,5),函数应该返回索引2。
给定列表中的3(1,2,4,5,6),它仍然应该返回2
您可能想检查我的python代码,因为我没有检查这段代码是否有错误,我假设您了解一些python,如果您有框架,您应该破解它。哦,python关心我做的tabbibg。如果列表中有很大的空白,那么构建每个项目的列表将有点浪费资源,相反,您可以迭代每个列表项目,直到输入大于值 就你的代码而言-
def search(input,inputList):
for i in range( len( inputList ) ):
if inputList[i]>input:
return i
return len( inputList )
print search(-5, (1, 5, 10))
#Result: 0
print search(3, (1, 5, 10))
#Result: 1
将其插入到列表中,这将起作用,我根据索引在2中分割列表并在中间添加值。
def insert(input,inputList):
index = search(input,inputList) #Get where the value should be inserted
newInput = [input]+list(inputList[index:]) #Add the end of the list to the input
if index:
newInput = list(inputList[:index])+newInput #Add the start of the list if the index isn't 0
return newInput
print insert(-5, (1, 5, 10))
#Result: (-5, 1, 5, 10)
print insert(3, (1, 5, 10))
#Result: (1, 3, 5, 10)
如果列表中存在较大的间隙,则构建每个项目的列表将有点浪费资源,相反,您可以迭代每个列表项目,直到输入大于值 就你的代码而言-
def search(input,inputList):
for i in range( len( inputList ) ):
if inputList[i]>input:
return i
return len( inputList )
print search(-5, (1, 5, 10))
#Result: 0
print search(3, (1, 5, 10))
#Result: 1
将其插入到列表中,这将起作用,我根据索引在2中分割列表并在中间添加值。
def insert(input,inputList):
index = search(input,inputList) #Get where the value should be inserted
newInput = [input]+list(inputList[index:]) #Add the end of the list to the input
if index:
newInput = list(inputList[:index])+newInput #Add the start of the list if the index isn't 0
return newInput
print insert(-5, (1, 5, 10))
#Result: (-5, 1, 5, 10)
print insert(3, (1, 5, 10))
#Result: (1, 3, 5, 10)
如果列表中存在较大的间隙,则构建每个项目的列表将有点浪费资源,相反,您可以迭代每个列表项目,直到输入大于值 就你的代码而言-
def search(input,inputList):
for i in range( len( inputList ) ):
if inputList[i]>input:
return i
return len( inputList )
print search(-5, (1, 5, 10))
#Result: 0
print search(3, (1, 5, 10))
#Result: 1
将其插入到列表中,这将起作用,我根据索引在2中分割列表并在中间添加值。
def insert(input,inputList):
index = search(input,inputList) #Get where the value should be inserted
newInput = [input]+list(inputList[index:]) #Add the end of the list to the input
if index:
newInput = list(inputList[:index])+newInput #Add the start of the list if the index isn't 0
return newInput
print insert(-5, (1, 5, 10))
#Result: (-5, 1, 5, 10)
print insert(3, (1, 5, 10))
#Result: (1, 3, 5, 10)
如果列表中存在较大的间隙,则构建每个项目的列表将有点浪费资源,相反,您可以迭代每个列表项目,直到输入大于值 就你的代码而言-
def search(input,inputList):
for i in range( len( inputList ) ):
if inputList[i]>input:
return i
return len( inputList )
print search(-5, (1, 5, 10))
#Result: 0
print search(3, (1, 5, 10))
#Result: 1
将其插入到列表中,这将起作用,我根据索引在2中分割列表并在中间添加值。
def insert(input,inputList):
index = search(input,inputList) #Get where the value should be inserted
newInput = [input]+list(inputList[index:]) #Add the end of the list to the input
if index:
newInput = list(inputList[:index])+newInput #Add the start of the list if the index isn't 0
return newInput
print insert(-5, (1, 5, 10))
#Result: (-5, 1, 5, 10)
print insert(3, (1, 5, 10))
#Result: (1, 3, 5, 10)
您的问题是什么?刚才回答了一个类似的问题,即python中已经存在函数。另外,为什么要迭代所有元素?您的列表已排序!您的问题是什么?刚才回答了一个类似的问题,即python中已经存在函数。另外,为什么要迭代所有元素?您的列表已排序!您的问题是什么?刚才回答了一个类似的问题,即python中已经存在函数。另外,为什么要迭代所有元素?您的列表已排序!您的问题是什么?刚才回答了一个类似的问题,即python中已经存在函数。另外,为什么要迭代所有元素?您的列表已排序!你可以把if条件简化为
xlist[i]>=x
你可以把if条件简化为xlist[i]>=x
你可以把if条件简化为xlist[i]>=x
你可以把if条件简化为xlist[i]>=x
如果我想把输入插入列表的确切位置,我该怎么办?以“搜索(3,(1,5,10))”为例:如何将3放在位置1?当输入值大于列表中的最高值时会发生什么?添加了一些关于如何插入值的内容,感谢您指出这一点哈哈,实际上,我并没有尝试使用更大的数字,但现在已经修复了:)请注意,以这种方式迭代排序列表是没有效率的。如果列表已经排序,您应该使用二进制搜索来完成任务。如何编写二进制搜索函数而不是原始函数?如果我想将输入插入列表的确切位置,我应该怎么做?以“搜索(3,(1,5,10))”为例:如何将3置于位置1?什么