【发布时间】:2020-12-13 21:27:16
【问题描述】:
def main():
testList = [3, 1, 8, 1, 5, 2, 21, 13]
print("searching for 5 in", testList,"...")
searchResult1 = linearSearch(testList, 5)
print(searchResult1)
def linearSearch(aList, target):
for item in aList:
if item == target:
return True
return False
main()
如果值在列表中,我如何返回该值的位置,而不是返回 true?
【问题讨论】:
-
当你遇到重复的目标值时会发生什么,你想要什么?
aList.index(target)如果您想要首次出现索引,则可以使用 -
这能回答你的问题吗? Finding the index of an item in a list
-
等等,你为什么要编辑代码?这个问题已经没有任何意义了。
标签: python python-3.x linear-search