def sequential_search(lis, key): length = len(lis) for i in range(length): if lis[i] == key: return i return False LIST = [1, 5, 8, 123, 22, 54, 7, 99, 300, 222] result = sequential_search(LIST, 123) print(result)
如果查找到123就会打印出123 的位置索引,否则显示false
def sequential_search(lis, key): length = len(lis) for i in range(length): if lis[i] == key: return i return False LIST = [1, 5, 8, 123, 22, 54, 7, 99, 300, 222] result = sequential_search(LIST, 123) print(result)
如果查找到123就会打印出123 的位置索引,否则显示false