循环列表中元素并逐一比较,比较待查值是否存在于列表中,存在返回下标,不存在返回-1。
# -*- coding: utf-8 -*- ''' 顺序查找: 和整个序列中元素逐个比较,若存在,则返回下标;不存在返回-1. ''' def sqential_search(ilist,value): for id,ele in enumerate(ilist): if ele == value: return id return -1 if __name__ == '__main__': pre_list = [1, 6, 3, 3, 7] key = 6 res = sqential_search(pre_list,key) print(res)
原文链接:https://blog.csdn.net/wulele2/article/details/111183361