{"id":1075,"date":"2023-03-24T09:47:07","date_gmt":"2023-03-24T01:47:07","guid":{"rendered":""},"modified":"2023-03-24T09:47:07","modified_gmt":"2023-03-24T01:47:07","slug":"Python \u641c\u7d22\u7b97\u6cd5","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/1075.html","title":{"rendered":"Python \u641c\u7d22\u7b97\u6cd5"},"content":{"rendered":"


\n <\/head>
\n <\/p>\n

\n

Python \u641c\u7d22\u7b97\u6cd5<\/h1>\n

Python \u641c\u7d22\u7b97\u6cd5\u8be6\u7ec6\u64cd\u4f5c\u6559\u7a0b<\/span>\n <\/div>\n

\n \u5f53\u60a8\u5c06\u6570\u636e\u5b58\u50a8\u5728\u4e0d\u540c\u7684\u6570\u636e\u7ed3\u6784\u4e2d\u65f6\uff0c\u641c\u7d22\u662f\u975e\u5e38\u57fa\u672c\u7684\u5fc5\u8981\u6761\u4ef6\u3002\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u904d\u5386\u6570\u636e\u7ed3\u6784\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5e76\u5c06\u5176\u4e0e\u60a8\u8981\u641c\u7d22\u7684\u503c\u5339\u914d\u3002\u8fd9\u79f0\u4e3a\u7ebf\u6027\u641c\u7d22\u3002\u5b83\u6548\u7387\u4f4e\u4e0b\uff0c\u5f88\u5c11\u4f7f\u7528\uff0c\u4f46\u662f\u4e3a\u6b64\u521b\u5efa\u4e00\u4e2a\u7a0b\u5e8f\u53ef\u4ee5\u4f7f\u6211\u4eec\u4e86\u89e3\u5982\u4f55\u5b9e\u73b0\u4e00\u4e9b\u9ad8\u7ea7\u641c\u7d22\u7b97\u6cd5\u3002\n <\/div>\n

\u7ebf\u6027\u641c\u7d22<\/h2>\n
\n \u5728\u8fd9\u79cd\u7c7b\u578b\u7684\u641c\u7d22\u4e2d\uff0c\u5bf9\u6240\u6709\u9879\u76ee\u8fdb\u884c\u9010\u4e2a\u641c\u7d22\u3002\u68c0\u67e5\u6bcf\u4e2a\u9879\u76ee\uff0c\u5982\u679c\u627e\u5230\u5339\u914d\u9879\uff0c\u5219\u8fd4\u56de\u8be5\u7279\u5b9a\u9879\u76ee\uff0c\u5426\u5219\u7ee7\u7eed\u641c\u7d22\u76f4\u5230\u6570\u636e\u7ed3\u6784\u7ed3\u675f\u3002\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-15<\/span>
def <\/span>linear_search(values, search_for):
    search_at = 0
    search_res = False<\/span>
# Match the value with <\/span>each data element
<\/span>     while <\/span>search_at < len(values) and <\/span>search_res is <\/span>False<\/span>:
        if <\/span>values[search_at] == search_for:
            search_res = True<\/span>
        else:<\/span>
            search_at = search_at + 1
    return <\/span>search_res
l = [64, 34, 25, 12, 22, 11, 90]
print(linear_search(l, 12))
print(linear_search(l, 91))
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a\n <\/div>\n
\n
 True
False<\/span>
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u63d2\u503c\u641c\u7d22<\/h2>\n
\n \u8be5\u641c\u7d22\u7b97\u6cd5\u5728\u6240\u9700\u503c\u7684\u63a2\u6d4b\u4f4d\u7f6e\u4e0a\u8d77\u4f5c\u7528\u3002\u4e3a\u4e86\u4f7f\u8be5\u7b97\u6cd5\u6b63\u5e38\u5de5\u4f5c\uff0c\u6570\u636e\u6536\u96c6\u5e94\u91c7\u7528\u6392\u5e8f\u5f62\u5f0f\u5e76\u5e73\u5747\u5206\u914d\u3002\u6700\u521d\uff0c\u63a2\u9488\u4f4d\u7f6e\u662f\u96c6\u5408\u4e2d\u6700\u4e2d\u95f4\u4e00\u9879\u7684\u4f4d\u7f6e\uff0c\u5982\u679c\u53d1\u751f\u5339\u914d\uff0c\u5219\u8fd4\u56de\u8be5\u9879\u7684\u7d22\u5f15\u3002\u5982\u679c\u4e2d\u95f4\u9879\u76ee\u5927\u4e8e\u8be5\u9879\u76ee\uff0c\u5219\u518d\u6b21\u5728\u4e2d\u95f4\u9879\u76ee\u53f3\u4fa7\u7684\u5b50\u6570\u7ec4\u4e2d\u8ba1\u7b97\u63a2\u9488\u4f4d\u7f6e\u3002\u5426\u5219\uff0c\u5c06\u5728\u4e2d\u95f4\u9879\u76ee\u5de6\u4fa7\u7684\u5b50\u6570\u7ec4\u4e2d\u641c\u7d22\u8be5\u9879\u76ee\u3002\u8be5\u8fc7\u7a0b\u4e5f\u5c06\u5728\u5b50\u9635\u5217\u4e0a\u7ee7\u7eed\u8fdb\u884c\uff0c\u76f4\u5230\u5b50\u9635\u5217\u7684\u5927\u5c0f\u51cf\u5c0f\u5230\u96f6\u4e3a\u6b62\u3002
\n
\u6709\u4e00\u4e2a\u7279\u5b9a\u7684\u516c\u5f0f\u53ef\u4ee5\u8ba1\u7b97\u4e2d\u95f4\u4f4d\u7f6e\uff0c\u8be5\u516c\u5f0f\u5728\u4e0b\u9762\u7684\u7a0b\u5e8f\u4e2d\u663e\u793a\u3002\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-15<\/span>
def <\/span>intpolsearch(values,x ):
    idx0 = 0
    idxn = (len(values) - 1)
    while <\/span>idx0 <= idxn and <\/span>x >= values[idx0] and <\/span>x <= values[idxn]:
# Find the mid point
<\/span> mid = idx0 +\\
               int(((float(idxn - idx0)\/( values[idxn] - values[idx0]))
                    * ( x - values[idx0])))
# Compare the value at mid point with <\/span>search value
<\/span>         if <\/span>values[mid] == x:
            return <\/span>\"Found \"<\/span>+str(x)+\" at index \"<\/span>+str(mid)
        if <\/span>values[mid] < x:
            idx0 = mid + 1
    return <\/span>\"Searched element not <\/span>in <\/span>the list\"<\/span>
l = [2, 6, 11, 19, 27, 31, 45, 121]
print(intpolsearch(l, 2))
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a\n <\/div>\n
\n
Found 2 at index 0
<\/span><\/code><\/pre>\n<\/p><\/div>\n

<\/body>
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"Python \u641c\u7d22\u7b97\u6cd5zh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[126],"tags":[],"class_list":["post-1075","post","type-post","status-publish","format-standard","hentry","category-python3"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1075"}],"collection":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/comments?post=1075"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1075\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=1075"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=1075"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=1075"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}