{"id":1157430,"date":"2025-01-13T18:28:34","date_gmt":"2025-01-13T10:28:34","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1157430.html"},"modified":"2025-01-13T18:28:36","modified_gmt":"2025-01-13T10:28:36","slug":"%e5%a6%82%e4%bd%95%e6%8f%90%e9%ab%98python%e8%af%ad%e6%b3%95%e6%80%a7%e8%83%bd-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1157430.html","title":{"rendered":"\u5982\u4f55\u63d0\u9ad8python\u8bed\u6cd5\u6027\u80fd"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25195611\/bf5fb91f-1f33-4327-8960-e708edcc8654.webp\" alt=\"\u5982\u4f55\u63d0\u9ad8python\u8bed\u6cd5\u6027\u80fd\" \/><\/p>\n<p><p> <strong>\u63d0\u9ad8Python\u8bed\u6cd5\u6027\u80fd\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\u3001\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u5faa\u73af\u3001\u5229\u7528\u5185\u5efa\u51fd\u6570\u548c\u6a21\u5757\u3001\u91c7\u7528\u5e76\u884c\u5904\u7406\u3001\u4f7f\u7528\u7f13\u5b58\u673a\u5236\u3001\u7f16\u5199\u9ad8\u6548\u7684\u7b97\u6cd5\u3001\u4f7f\u7528JIT\u7f16\u8bd1\u5668\u7b49\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\u5bf9\u6027\u80fd\u7684\u63d0\u5347\u975e\u5e38\u663e\u8457\u3002\u9009\u62e9\u6b63\u786e\u7684\u6570\u636e\u7ed3\u6784\u80fd\u591f\u663e\u8457\u51cf\u5c11\u65f6\u95f4\u548c\u7a7a\u95f4\u590d\u6742\u5ea6\uff0c\u4ece\u800c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6267\u884c\u6548\u7387\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u96c6\u5408\uff08set\uff09\u4ee3\u66ff\u5217\u8868\uff08list\uff09\u8fdb\u884c\u67e5\u627e\u64cd\u4f5c\uff0c\u53ef\u4ee5\u5c06\u65f6\u95f4\u590d\u6742\u5ea6\u4eceO(n)\u964d\u4f4e\u5230O(1)\uff0c\u4ece\u800c\u5927\u5e45\u63d0\u5347\u6027\u80fd\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784<\/h3>\n<\/p>\n<p><p>\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\u662f\u63d0\u9ad8Python\u6027\u80fd\u7684\u57fa\u7840\u3002\u4e0d\u540c\u7684\u6570\u636e\u7ed3\u6784\u6709\u4e0d\u540c\u7684\u65f6\u95f4\u548c\u7a7a\u95f4\u590d\u6742\u5ea6\uff0c\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6267\u884c\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5217\u8868\u548c\u96c6\u5408<\/h4>\n<\/p>\n<p><p>\u5217\u8868\uff08list\uff09\u548c\u96c6\u5408\uff08set\uff09\u662fPython\u4e2d\u975e\u5e38\u5e38\u7528\u7684\u6570\u636e\u7ed3\u6784\u3002\u5217\u8868\u9002\u7528\u4e8e\u9700\u8981\u4fdd\u6301\u5143\u7d20\u987a\u5e8f\u5e76\u4e14\u5305\u542b\u91cd\u590d\u5143\u7d20\u7684\u573a\u666f\uff0c\u800c\u96c6\u5408\u5219\u9002\u7528\u4e8e\u9700\u8981\u5feb\u901f\u67e5\u627e\u548c\u53bb\u91cd\u7684\u573a\u666f\u3002<strong>\u4f7f\u7528\u96c6\u5408\u8fdb\u884c\u67e5\u627e\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(1)\uff0c\u800c\u5217\u8868\u5219\u4e3aO(n)<\/strong>\u3002\u56e0\u6b64\uff0c\u5728\u9700\u8981\u9891\u7e41\u67e5\u627e\u7684\u573a\u666f\u4e2d\uff0c\u4f7f\u7528\u96c6\u5408\u4f1a\u6bd4\u5217\u8868\u66f4\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u5217\u8868\u8fdb\u884c\u67e5\u627e<\/p>\n<p>my_list = [1, 2, 3, 4, 5]<\/p>\n<p>if 3 in my_list:<\/p>\n<p>    print(&quot;Found&quot;)<\/p>\n<h2><strong>\u4f7f\u7528\u96c6\u5408\u8fdb\u884c\u67e5\u627e<\/strong><\/h2>\n<p>my_set = {1, 2, 3, 4, 5}<\/p>\n<p>if 3 in my_set:<\/p>\n<p>    print(&quot;Found&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5b57\u5178<\/h4>\n<\/p>\n<p><p>\u5b57\u5178\uff08dictionary\uff09\u662f\u53e6\u4e00\u79cd\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784\uff0c\u9002\u7528\u4e8e\u952e\u503c\u5bf9\u5b58\u50a8\u548c\u5feb\u901f\u67e5\u627e\u3002\u4e0e\u5217\u8868\u76f8\u6bd4\uff0c\u5b57\u5178\u5728\u67e5\u627e\u3001\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\u4e0a\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5747\u4e3aO(1)\uff0c\u56e0\u6b64\u5728\u9700\u8981\u9891\u7e41\u8fdb\u884c\u8fd9\u4e9b\u64cd\u4f5c\u65f6\uff0c\u4f7f\u7528\u5b57\u5178\u4f1a\u66f4\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u5b57\u5178\u8fdb\u884c\u67e5\u627e<\/p>\n<p>my_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3}<\/p>\n<p>if &#39;b&#39; in my_dict:<\/p>\n<p>    print(&quot;Found&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u5faa\u73af<\/h3>\n<\/p>\n<p><p>\u5faa\u73af\u662f\u5f71\u54cd\u7a0b\u5e8f\u6027\u80fd\u7684\u4e00\u4e2a\u91cd\u8981\u56e0\u7d20\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\u3002\u4e0d\u5fc5\u8981\u7684\u5faa\u73af\u4f1a\u589e\u52a0\u7a0b\u5e8f\u7684\u6267\u884c\u65f6\u95f4\uff0c\u56e0\u6b64\u9700\u8981\u5c3d\u91cf\u907f\u514d\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528\u5185\u5efa\u51fd\u6570<\/h4>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u8bb8\u591a\u5185\u5efa\u51fd\u6570\uff0c\u5982<code>map<\/code>\u3001<code>filter<\/code>\u548c<code>reduce<\/code>\uff0c\u8fd9\u4e9b\u51fd\u6570\u5728\u5e95\u5c42\u4f7f\u7528\u4e86C\u8bed\u8a00\u5b9e\u73b0\uff0c\u901a\u5e38\u6bd4\u624b\u52a8\u7f16\u5199\u7684\u5faa\u73af\u66f4\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u5faa\u73af<\/p>\n<p>result = []<\/p>\n<p>for i in range(10):<\/p>\n<p>    result.append(i * 2)<\/p>\n<h2><strong>\u4f7f\u7528\u5185\u5efa\u51fd\u6570<\/strong><\/h2>\n<p>result = list(map(lambda x: x * 2, range(10)))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5217\u8868\u63a8\u5bfc\u5f0f<\/h4>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u4e14\u9ad8\u6548\u7684\u751f\u6210\u5217\u8868\u7684\u65b9\u5f0f\uff0c\u901a\u5e38\u6bd4\u4f7f\u7528\u5faa\u73af\u66f4\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u5faa\u73af<\/p>\n<p>result = []<\/p>\n<p>for i in range(10):<\/p>\n<p>    result.append(i * 2)<\/p>\n<h2><strong>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/strong><\/h2>\n<p>result = [i * 2 for i in range(10)]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5229\u7528\u5185\u5efa\u51fd\u6570\u548c\u6a21\u5757<\/h3>\n<\/p>\n<p><p>Python\u6807\u51c6\u5e93\u63d0\u4f9b\u4e86\u8bb8\u591a\u5185\u5efa\u51fd\u6570\u548c\u6a21\u5757\uff0c\u8fd9\u4e9b\u51fd\u6570\u548c\u6a21\u5757\u5728\u5e95\u5c42\u8fdb\u884c\u4e86\u9ad8\u5ea6\u4f18\u5316\uff0c\u901a\u5e38\u6bd4\u624b\u52a8\u7f16\u5199\u7684\u4ee3\u7801\u66f4\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528<code>itertools<\/code>\u6a21\u5757<\/h4>\n<\/p>\n<p><p><code>itertools<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e9b\u9ad8\u6548\u7684\u8fed\u4ee3\u5668\uff0c\u53ef\u4ee5\u7528\u4e8e\u5904\u7406\u5927\u91cf\u6570\u636e\u3002\u4f8b\u5982\uff0c<code>itertools.ch<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n<\/code>\u53ef\u4ee5\u5c06\u591a\u4e2a\u8fed\u4ee3\u5668\u8fde\u63a5\u8d77\u6765\uff0c\u907f\u514d\u4e86\u5c06\u6570\u636e\u5168\u90e8\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import itertools<\/p>\n<h2><strong>\u4f7f\u7528itertools.chain<\/strong><\/h2>\n<p>result = list(itertools.chain([1, 2, 3], [4, 5, 6]))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528<code>collections<\/code>\u6a21\u5757<\/h4>\n<\/p>\n<p><p><code>collections<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e9b\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5982<code>deque<\/code>\u3001<code>Counter<\/code>\u548c<code>defaultdict<\/code>\uff0c\u8fd9\u4e9b\u6570\u636e\u7ed3\u6784\u5728\u67d0\u4e9b\u573a\u666f\u4e0b\u6bd4\u5185\u5efa\u7684\u6570\u636e\u7ed3\u6784\u66f4\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import deque<\/p>\n<h2><strong>\u4f7f\u7528deque<\/strong><\/h2>\n<p>my_deque = deque([1, 2, 3])<\/p>\n<p>my_deque.append(4)<\/p>\n<p>my_deque.popleft()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u91c7\u7528\u5e76\u884c\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5e76\u884c\u5904\u7406\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6267\u884c\u6548\u7387\uff0c\u7279\u522b\u662f\u5728\u5904\u7406CPU\u5bc6\u96c6\u578b\u4efb\u52a1\u65f6\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5e76\u884c\u5904\u7406\u7684\u65b9\u5f0f\uff0c\u5982\u591a\u7ebf\u7a0b\u3001\u591a\u8fdb\u7a0b\u548c\u534f\u7a0b\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u591a\u7ebf\u7a0b<\/h4>\n<\/p>\n<p><p>\u591a\u7ebf\u7a0b\u9002\u7528\u4e8eI\/O\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u5982\u7f51\u7edc\u8bf7\u6c42\u548c\u6587\u4ef6\u64cd\u4f5c\u3002Python\u7684<code>threading<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u7ebf\u7a0b\u7684\u652f\u6301\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>def task():<\/p>\n<p>    print(&quot;Task executed&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u5e76\u542f\u52a8\u7ebf\u7a0b<\/strong><\/h2>\n<p>thread = threading.Thread(target=task)<\/p>\n<p>thread.start()<\/p>\n<p>thread.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u591a\u8fdb\u7a0b<\/h4>\n<\/p>\n<p><p>\u591a\u8fdb\u7a0b\u9002\u7528\u4e8eCPU\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u5982\u8ba1\u7b97\u5bc6\u96c6\u578b\u64cd\u4f5c\u3002Python\u7684<code>multiprocessing<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u8fdb\u7a0b\u7684\u652f\u6301\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import multiprocessing<\/p>\n<p>def task():<\/p>\n<p>    print(&quot;Task executed&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u5e76\u542f\u52a8\u8fdb\u7a0b<\/strong><\/h2>\n<p>process = multiprocessing.Process(target=task)<\/p>\n<p>process.start()<\/p>\n<p>process.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u7f13\u5b58\u673a\u5236<\/h3>\n<\/p>\n<p><p>\u7f13\u5b58\u673a\u5236\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6267\u884c\u6548\u7387\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u9891\u7e41\u8bbf\u95ee\u76f8\u540c\u6570\u636e\u7684\u573a\u666f\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u7f13\u5b58\u673a\u5236\uff0c\u5982<code>functools.lru_cache<\/code>\u548c<code>cachetools<\/code>\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528<code>functools.lru_cache<\/code><\/h4>\n<\/p>\n<p><p><code>functools.lru_cache<\/code>\u662f\u4e00\u4e2a\u5185\u5efa\u7684\u7f13\u5b58\u88c5\u9970\u5668\uff0c\u53ef\u4ee5\u81ea\u52a8\u7f13\u5b58\u51fd\u6570\u7684\u8fd4\u56de\u7ed3\u679c\uff0c\u907f\u514d\u91cd\u590d\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import functools<\/p>\n<p>@functools.lru_cache(maxsize=128)<\/p>\n<p>def expensive_function(x):<\/p>\n<p>    return x * x<\/p>\n<h2><strong>\u8c03\u7528\u51fd\u6570\uff0c\u7ed3\u679c\u4f1a\u88ab\u7f13\u5b58<\/strong><\/h2>\n<p>result = expensive_function(4)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528<code>cachetools<\/code>\u6a21\u5757<\/h4>\n<\/p>\n<p><p><code>cachetools<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u7f13\u5b58\u7b56\u7565\uff0c\u5982LRU\uff08\u6700\u8fd1\u6700\u5c11\u4f7f\u7528\uff09\u548cTTL\uff08\u65f6\u95f4\u5230\u671f\uff09\u7f13\u5b58\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u9009\u62e9\u5408\u9002\u7684\u7f13\u5b58\u7b56\u7565\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cachetools<\/p>\n<h2><strong>\u521b\u5efaLRU\u7f13\u5b58<\/strong><\/h2>\n<p>cache = cachetools.LRUCache(maxsize=128)<\/p>\n<h2><strong>\u7f13\u5b58\u7ed3\u679c<\/strong><\/h2>\n<p>cache[&#39;key&#39;] = &#39;value&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u7f16\u5199\u9ad8\u6548\u7684\u7b97\u6cd5<\/h3>\n<\/p>\n<p><p>\u9ad8\u6548\u7684\u7b97\u6cd5\u662f\u63d0\u9ad8\u7a0b\u5e8f\u6027\u80fd\u7684\u5173\u952e\u3002\u5728\u7f16\u5199\u4ee3\u7801\u65f6\uff0c\u9700\u8981\u9009\u62e9\u5408\u9002\u7684\u7b97\u6cd5\uff0c\u907f\u514d\u4f7f\u7528\u4f4e\u6548\u7684\u7b97\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u9009\u62e9\u5408\u9002\u7684\u6392\u5e8f\u7b97\u6cd5<\/h4>\n<\/p>\n<p><p>\u4e0d\u540c\u7684\u6392\u5e8f\u7b97\u6cd5\u6709\u4e0d\u540c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff0c\u9009\u62e9\u5408\u9002\u7684\u6392\u5e8f\u7b97\u6cd5\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6027\u80fd\u3002\u4f8b\u5982\uff0c\u5feb\u901f\u6392\u5e8f\u7684\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(n log n)\uff0c\u800c\u5192\u6ce1\u6392\u5e8f\u5219\u4e3aO(n^2)\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u5feb\u901f\u6392\u5e8f<\/p>\n<p>def quick_sort(arr):<\/p>\n<p>    if len(arr) &lt;= 1:<\/p>\n<p>        return arr<\/p>\n<p>    pivot = arr[len(arr) \/\/ 2]<\/p>\n<p>    left = [x for x in arr if x &lt; pivot]<\/p>\n<p>    middle = [x for x in arr if x == pivot]<\/p>\n<p>    right = [x for x in arr if x &gt; pivot]<\/p>\n<p>    return quick_sort(left) + middle + quick_sort(right)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528\u52a8\u6001\u89c4\u5212<\/h4>\n<\/p>\n<p><p>\u52a8\u6001\u89c4\u5212\u662f\u4e00\u79cd\u9ad8\u6548\u7684\u7b97\u6cd5\u8bbe\u8ba1\u6280\u5de7\uff0c\u9002\u7528\u4e8e\u89e3\u51b3\u5177\u6709\u91cd\u53e0\u5b50\u95ee\u9898\u548c\u6700\u4f18\u5b50\u7ed3\u6784\u6027\u8d28\u7684\u95ee\u9898\u3002\u901a\u8fc7\u5c06\u95ee\u9898\u5206\u89e3\u4e3a\u5b50\u95ee\u9898\u5e76\u7f13\u5b58\u5b50\u95ee\u9898\u7684\u89e3\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u52a8\u6001\u89c4\u5212\u89e3\u51b3\u6590\u6ce2\u90a3\u5951\u6570\u5217<\/p>\n<p>def fibonacci(n, cache={}):<\/p>\n<p>    if n in cache:<\/p>\n<p>        return cache[n]<\/p>\n<p>    if n &lt;= 1:<\/p>\n<p>        return n<\/p>\n<p>    cache[n] = fibonacci(n - 1) + fibonacci(n - 2)<\/p>\n<p>    return cache[n]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528JIT\u7f16\u8bd1\u5668<\/h3>\n<\/p>\n<p><p>JIT\uff08Just-In-Time\uff09\u7f16\u8bd1\u5668\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u5c06Python\u4ee3\u7801\u7f16\u8bd1\u4e3a\u673a\u5668\u7801\uff0c\u4ece\u800c\u663e\u8457\u63d0\u9ad8\u6267\u884c\u901f\u5ea6\u3002PyPy\u662f\u4e00\u4e2a\u652f\u6301JIT\u7f16\u8bd1\u7684Python\u89e3\u91ca\u5668\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8Python\u4ee3\u7801\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5PyPy<\/h4>\n<\/p>\n<p><p>PyPy\u662f\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684Python\u89e3\u91ca\u5668\uff0c\u652f\u6301JIT\u7f16\u8bd1\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5PyPy\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\"># \u5b89\u88c5PyPy<\/p>\n<p>sudo apt-get install pypy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528PyPy\u8fd0\u884c\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u5b89\u88c5PyPy\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528PyPy\u89e3\u91ca\u5668\u8fd0\u884cPython\u4ee3\u7801\uff0c\u4ece\u800c\u83b7\u5f97\u663e\u8457\u7684\u6027\u80fd\u63d0\u5347\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\"># \u4f7f\u7528PyPy\u8fd0\u884c\u4ee3\u7801<\/p>\n<p>pypy my_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u4f18\u5316I\/O\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>I\/O\u64cd\u4f5c\uff08\u5982\u6587\u4ef6\u8bfb\u5199\u3001\u7f51\u7edc\u8bf7\u6c42\uff09\u662f\u5f71\u54cd\u7a0b\u5e8f\u6027\u80fd\u7684\u53e6\u4e00\u4e2a\u91cd\u8981\u56e0\u7d20\u3002\u901a\u8fc7\u4f18\u5316I\/O\u64cd\u4f5c\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6267\u884c\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528\u5f02\u6b65I\/O<\/h4>\n<\/p>\n<p><p>\u5f02\u6b65I\/O\u53ef\u4ee5\u907f\u514dI\/O\u64cd\u4f5c\u7684\u963b\u585e\uff0c\u4ece\u800c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5e76\u53d1\u6027\u80fd\u3002Python\u7684<code>asyncio<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u5f02\u6b65I\/O\u7684\u652f\u6301\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import asyncio<\/p>\n<p>async def fetch_data():<\/p>\n<p>    print(&quot;Fetching data&quot;)<\/p>\n<p>    await asyncio.sleep(1)<\/p>\n<p>    print(&quot;Data fetched&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u5e76\u8fd0\u884c\u4e8b\u4ef6\u5faa\u73af<\/strong><\/h2>\n<p>loop = asyncio.get_event_loop()<\/p>\n<p>loop.run_until_complete(fetch_data())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u6279\u91cf\u8bfb\u5199<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\uff0c\u6279\u91cf\u8bfb\u5199\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8I\/O\u64cd\u4f5c\u7684\u6548\u7387\u3002\u4f8b\u5982\uff0c\u5728\u8bfb\u5199\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u4e00\u6b21\u6027\u8bfb\u53d6\u6216\u5199\u5165\u8f83\u5927\u7684\u6570\u636e\u5757\uff0c\u800c\u4e0d\u662f\u9010\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6279\u91cf\u8bfb\u53d6\u6587\u4ef6<\/p>\n<p>with open(&#39;large_file.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    data = file.read()<\/p>\n<h2><strong>\u6279\u91cf\u5199\u5165\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;output_file.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    file.write(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u51cf\u5c11\u5185\u5b58\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>\u5185\u5b58\u4f7f\u7528\u4e5f\u662f\u5f71\u54cd\u7a0b\u5e8f\u6027\u80fd\u7684\u4e00\u4e2a\u91cd\u8981\u56e0\u7d20\uff0c\u901a\u8fc7\u51cf\u5c11\u5185\u5b58\u4f7f\u7528\uff0c\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6267\u884c\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528\u751f\u6210\u5668<\/h4>\n<\/p>\n<p><p>\u751f\u6210\u5668\u662f\u4e00\u79cd\u9ad8\u6548\u7684\u8fed\u4ee3\u5668\uff0c\u53ef\u4ee5\u5728\u904d\u5386\u6570\u636e\u65f6\u6309\u9700\u751f\u6210\u6570\u636e\uff0c\u4ece\u800c\u907f\u514d\u5c06\u6240\u6709\u6570\u636e\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u751f\u6210\u5668<\/p>\n<p>def generate_numbers(n):<\/p>\n<p>    for i in range(n):<\/p>\n<p>        yield i<\/p>\n<h2><strong>\u904d\u5386\u751f\u6210\u5668<\/strong><\/h2>\n<p>for num in generate_numbers(10):<\/p>\n<p>    print(num)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528<code>sys.getsizeof<\/code>\u76d1\u63a7\u5185\u5b58<\/h4>\n<\/p>\n<p><p><code>sys.getsizeof<\/code>\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u76d1\u63a7\u5bf9\u8c61\u7684\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\uff0c\u4ece\u800c\u5e2e\u52a9\u8bc6\u522b\u548c\u4f18\u5316\u5185\u5b58\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<h2><strong>\u76d1\u63a7\u5bf9\u8c61\u7684\u5185\u5b58\u4f7f\u7528<\/strong><\/h2>\n<p>my_list = [1, 2, 3, 4, 5]<\/p>\n<p>print(sys.getsizeof(my_list))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u907f\u514d\u5168\u5c40\u53d8\u91cf<\/h3>\n<\/p>\n<p><p>\u5168\u5c40\u53d8\u91cf\u4f1a\u589e\u52a0\u7a0b\u5e8f\u7684\u8026\u5408\u6027\uff0c\u5f71\u54cd\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\uff0c\u540c\u65f6\u4e5f\u4f1a\u5f71\u54cd\u6027\u80fd\u3002\u5c3d\u91cf\u907f\u514d\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\uff0c\u63a8\u8350\u4f7f\u7528\u51fd\u6570\u53c2\u6570\u548c\u8fd4\u56de\u503c\u6765\u4f20\u9012\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528\u51fd\u6570\u53c2\u6570\u548c\u8fd4\u56de\u503c<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528\u51fd\u6570\u53c2\u6570\u548c\u8fd4\u56de\u503c\uff0c\u53ef\u4ee5\u51cf\u5c11\u5bf9\u5168\u5c40\u53d8\u91cf\u7684\u4f9d\u8d56\uff0c\u4ece\u800c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u51fd\u6570\u53c2\u6570\u548c\u8fd4\u56de\u503c<\/p>\n<p>def process_data(data):<\/p>\n<p>    return [x * 2 for x in data]<\/p>\n<h2><strong>\u8c03\u7528\u51fd\u6570<\/strong><\/h2>\n<p>result = process_data([1, 2, 3, 4, 5])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528\u7c7b\u548c\u5bf9\u8c61<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528\u7c7b\u548c\u5bf9\u8c61\uff0c\u53ef\u4ee5\u5c06\u6570\u636e\u548c\u65b9\u6cd5\u5c01\u88c5\u5728\u4e00\u8d77\uff0c\u51cf\u5c11\u5168\u5c40\u53d8\u91cf\u7684\u4f7f\u7528\uff0c\u4ece\u800c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u7c7b\u548c\u5bf9\u8c61<\/p>\n<p>class DataProcessor:<\/p>\n<p>    def __init__(self, data):<\/p>\n<p>        self.data = data<\/p>\n<p>    def process(self):<\/p>\n<p>        return [x * 2 for x in self.data]<\/p>\n<h2><strong>\u521b\u5efa\u5bf9\u8c61\u5e76\u8c03\u7528\u65b9\u6cd5<\/strong><\/h2>\n<p>processor = DataProcessor([1, 2, 3, 4, 5])<\/p>\n<p>result = processor.process()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u4f7f\u7528\u5408\u9002\u7684\u64cd\u4f5c\u7b26<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u64cd\u4f5c\u7b26\uff0c\u4e0d\u540c\u7684\u64cd\u4f5c\u7b26\u6709\u4e0d\u540c\u7684\u6027\u80fd\u7279\u70b9\u3002\u9009\u62e9\u5408\u9002\u7684\u64cd\u4f5c\u7b26\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u4ee3\u7801\u7684\u6267\u884c\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528<code>in<\/code>\u64cd\u4f5c\u7b26<\/h4>\n<\/p>\n<p><p><code>in<\/code>\u64cd\u4f5c\u7b26\u7528\u4e8e\u68c0\u67e5\u5143\u7d20\u662f\u5426\u5728\u96c6\u5408\u4e2d\uff0c\u76f8\u6bd4\u4e8e\u4f7f\u7528\u5faa\u73af\u67e5\u627e\uff0c<code>in<\/code>\u64cd\u4f5c\u7b26\u66f4\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528in\u64cd\u4f5c\u7b26<\/p>\n<p>my_list = [1, 2, 3, 4, 5]<\/p>\n<p>if 3 in my_list:<\/p>\n<p>    print(&quot;Found&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528<code>and<\/code>\u548c<code>or<\/code>\u64cd\u4f5c\u7b26<\/h4>\n<\/p>\n<p><p><code>and<\/code>\u548c<code>or<\/code>\u64cd\u4f5c\u7b26\u7528\u4e8e\u903b\u8f91\u8fd0\u7b97\uff0c\u76f8\u6bd4\u4e8e\u4f7f\u7528\u5d4c\u5957\u7684if\u8bed\u53e5\uff0c<code>and<\/code>\u548c<code>or<\/code>\u64cd\u4f5c\u7b26\u66f4\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528and\u548cor\u64cd\u4f5c\u7b26<\/p>\n<p>a = 5<\/p>\n<p>b = 10<\/p>\n<p>if a &gt; 0 and b &gt; 0:<\/p>\n<p>    print(&quot;Both positive&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e8c\u3001\u4f7f\u7528\u9ad8\u6548\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u64cd\u4f5c\u5728Python\u4e2d\u975e\u5e38\u5e38\u89c1\uff0c\u9009\u62e9\u9ad8\u6548\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\u65b9\u6cd5\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6267\u884c\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528<code>join<\/code>\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p><code>join<\/code>\u65b9\u6cd5\u7528\u4e8e\u8fde\u63a5\u5b57\u7b26\u4e32\uff0c\u76f8\u6bd4\u4e8e\u4f7f\u7528<code>+<\/code>\u64cd\u4f5c\u7b26\uff0c<code>join<\/code>\u65b9\u6cd5\u66f4\u9ad8\u6548\uff0c\u7279\u522b\u662f\u5728\u8fde\u63a5\u5927\u91cf\u5b57\u7b26\u4e32\u65f6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528join\u65b9\u6cd5<\/p>\n<p>strings = [&#39;Hello&#39;, &#39;World&#39;]<\/p>\n<p>result = &#39; &#39;.join(strings)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528<code>format<\/code>\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p><code>format<\/code>\u65b9\u6cd5\u7528\u4e8e\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff0c\u76f8\u6bd4\u4e8e\u4f7f\u7528<code>%<\/code>\u64cd\u4f5c\u7b26\uff0c<code>format<\/code>\u65b9\u6cd5\u66f4\u7075\u6d3b\u548c\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528format\u65b9\u6cd5<\/p>\n<p>name = &#39;John&#39;<\/p>\n<p>age = 30<\/p>\n<p>result = &#39;My name is {} and I am {} years old&#39;.format(name, age)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e09\u3001\u4f18\u5316\u6b63\u5219\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u5904\u7406\u5b57\u7b26\u4e32\u7684\u5f3a\u5927\u5de5\u5177\uff0c\u4f46\u590d\u6742\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u4f1a\u5f71\u54cd\u6027\u80fd\u3002\u901a\u8fc7\u4f18\u5316\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6267\u884c\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528\u9884\u7f16\u8bd1\u7684\u6b63\u5219\u8868\u8fbe\u5f0f<\/h4>\n<\/p>\n<p><p>\u5728\u9891\u7e41\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u573a\u666f\u4e2d\uff0c\u4f7f\u7528\u9884\u7f16\u8bd1\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<h2><strong>\u4f7f\u7528\u9884\u7f16\u8bd1\u7684\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong><\/h2>\n<p>pattern = re.compile(r&#39;\\d+&#39;)<\/p>\n<p>result = pattern.findall(&#39;123 abc 456 def&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u907f\u514d\u4f7f\u7528\u590d\u6742\u7684\u6b63\u5219\u8868\u8fbe\u5f0f<\/h4>\n<\/p>\n<p><p>\u590d\u6742\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u4f1a\u5f71\u54cd\u6027\u80fd\uff0c\u5c3d\u91cf\u4f7f\u7528\u7b80\u5355\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u63d0\u9ad8\u5339\u914d\u6548\u7387\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u7b80\u5316\u6b63\u5219\u8868\u8fbe\u5f0f<\/p>\n<p>import re<\/p>\n<h2><strong>\u7b80\u5355\u7684\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong><\/h2>\n<p>pattern = re.compile(r&#39;\\d+&#39;)<\/p>\n<p>result = pattern.findall(&#39;123 abc 456 def&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u56db\u3001\u4f7f\u7528\u5408\u9002\u7684\u5e76\u53d1\u6a21\u578b<\/h3>\n<\/p>\n<p><p>\u9009\u62e9\u5408\u9002\u7684\u5e76\u53d1\u6a21\u578b\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6267\u884c\u6548\u7387\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5e76\u53d1\u6a21\u578b\uff0c\u5982\u591a\u7ebf\u7a0b\u3001\u591a\u8fdb\u7a0b\u548c\u534f\u7a0b\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u9009\u62e9\u5408\u9002\u7684\u5e76\u53d1\u6a21\u578b<\/h4>\n<\/p>\n<p><p>\u6839\u636e\u4efb\u52a1\u7684\u6027\u8d28\u9009\u62e9\u5408\u9002\u7684\u5e76\u53d1\u6a21\u578b\u3002\u4f8b\u5982\uff0c\u591a\u7ebf\u7a0b\u9002\u7528\u4e8eI\/O\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u800c\u591a\u8fdb\u7a0b\u9002\u7528\u4e8eCPU\u5bc6\u96c6\u578b\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import multiprocessing<\/p>\n<p>def io_task():<\/p>\n<p>    print(&quot;I\/O task executed&quot;)<\/p>\n<p>def cpu_task():<\/p>\n<p>    print(&quot;CPU task executed&quot;)<\/p>\n<h2><strong>\u4f7f\u7528\u591a\u7ebf\u7a0b\u5904\u7406I\/O\u5bc6\u96c6\u578b\u4efb\u52a1<\/strong><\/h2>\n<p>thread = threading.Thread(target=io_task)<\/p>\n<p>thread.start()<\/p>\n<p>thread.join()<\/p>\n<h2><strong>\u4f7f\u7528\u591a\u8fdb\u7a0b\u5904\u7406CPU\u5bc6\u96c6\u578b\u4efb\u52a1<\/strong><\/h2>\n<p>process = multiprocessing.Process(target=cpu_task)<\/p>\n<p>process.start()<\/p>\n<p>process.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528<code>asyncio<\/code>\u6a21\u5757<\/h4>\n<\/p>\n<p><p><code>asyncio<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u5f02\u6b65\u5e76\u53d1\u5904\u7406\uff0c\u9002\u7528\u4e8eI\/O\u5bc6\u96c6\u578b\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import asyncio<\/p>\n<p>async def io_task():<\/p>\n<p>    print(&quot;I\/O task executed&quot;)<\/p>\n<p>    await asyncio.sleep(1)<\/p>\n<h2><strong>\u521b\u5efa\u5e76\u8fd0\u884c\u4e8b\u4ef6\u5faa\u73af<\/strong><\/h2>\n<p>loop = asyncio.get_event_loop()<\/p>\n<p>loop.run_until_complete(io_task())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e94\u3001\u4f18\u5316\u6570\u636e\u5e93\u67e5\u8be2<\/h3>\n<\/p>\n<p><p>\u6570\u636e\u5e93\u67e5\u8be2\u662f\u5f71\u54cd\u7a0b\u5e8f\u6027\u80fd\u7684\u4e00\u4e2a\u91cd\u8981\u56e0\u7d20\uff0c\u901a\u8fc7\u4f18\u5316\u6570\u636e\u5e93\u67e5\u8be2\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6267\u884c\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528\u7d22\u5f15<\/h4>\n<\/p>\n<p><p>\u7d22\u5f15\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6570\u636e\u5e93\u67e5\u8be2\u7684\u6027\u80fd\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\u3002\u786e\u4fdd\u5bf9\u5e38\u7528\u7684\u67e5\u8be2\u5b57\u6bb5\u521b\u5efa\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-sql\">-- \u521b\u5efa\u7d22\u5f15<\/p>\n<p>CREATE INDEX idx_name ON users(name);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u6279\u91cf\u67e5\u8be2<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\uff0c\u6279\u91cf\u67e5\u8be2\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6570\u636e\u5e93\u67e5\u8be2\u7684\u6548\u7387\uff0c\u907f\u514d\u9891\u7e41\u7684\u6570\u636e\u5e93\u8fde\u63a5\u548c\u67e5\u8be2\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6279\u91cf\u67e5\u8be2<\/p>\n<p>import sqlite3<\/p>\n<p>conn = sqlite3.connect(&#39;example.db&#39;)<\/p>\n<p>cursor = conn.cursor()<\/p>\n<h2><strong>\u6279\u91cf\u63d2\u5165\u6570\u636e<\/strong><\/h2>\n<p>data = [(1, &#39;Alice&#39;), (2, &#39;Bob&#39;), (3, &#39;Charlie&#39;)]<\/p>\n<p>cursor.executemany(&#39;INSERT INTO users VALUES (?, ?)&#39;, data)<\/p>\n<p>conn.commit()<\/p>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u516d\u3001\u4f7f\u7528\u5408\u9002\u7684\u5e8f\u5217\u5316\u683c\u5f0f<\/h3>\n<\/p>\n<p><p>\u5e8f\u5217\u5316\u662f\u5c06\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5b57\u8282\u6d41\u7684\u8fc7\u7a0b\uff0c\u9009\u62e9\u5408\u9002\u7684\u5e8f\u5217\u5316\u683c\u5f0f\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6267\u884c\u6548\u7387\u3002\u5e38\u7528\u7684\u5e8f\u5217\u5316\u683c\u5f0f\u5305\u62ecJSON\u3001Pickle\u548cMessagePack\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528<code>json<\/code>\u6a21\u5757<\/h4>\n<\/p>\n<p><p><code>json<\/code>\u6a21\u5757\u7528\u4e8e\u5904\u7406JSON\u683c\u5f0f\u7684\u6570\u636e\uff0c\u9002\u7528\u4e8e\u6570\u636e\u4ea4\u6362\u548c\u5b58\u50a8\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>\u5e8f\u5217\u5316\u5bf9\u8c61<\/strong><\/h2>\n<p>data = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}<\/p>\n<p>json_data = json.dumps(data)<\/p>\n<h2><strong>\u53cd\u5e8f\u5217\u5316\u5bf9\u8c61<\/strong><\/h2>\n<p>data = json.loads(json_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528`pickle<\/h4>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>1. \u5982\u4f55\u8bc4\u4f30\u6211\u7684Python\u4ee3\u7801\u6027\u80fd\uff1f<\/strong><br \/>\u8981\u8bc4\u4f30Python\u4ee3\u7801\u7684\u6027\u80fd\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>time<\/code>\u6a21\u5757\u6216\u66f4\u9ad8\u7ea7\u7684\u5de5\u5177\u5982<code>cProfile<\/code>\u3002\u901a\u8fc7\u8fd9\u4e9b\u5de5\u5177\uff0c\u53ef\u4ee5\u6d4b\u91cf\u51fd\u6570\u6267\u884c\u7684\u65f6\u95f4\u548c\u8d44\u6e90\u6d88\u8017\uff0c\u5e2e\u52a9\u8bc6\u522b\u6027\u80fd\u74f6\u9888\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>line_profiler<\/code>\u53ef\u4ee5\u9010\u884c\u5206\u6790\u4ee3\u7801\uff0c\u63d0\u4f9b\u66f4\u8be6\u7ec6\u7684\u6027\u80fd\u6570\u636e\u3002<\/p>\n<p><strong>2. \u662f\u5426\u6709\u63a8\u8350\u7684Python\u6027\u80fd\u4f18\u5316\u5de5\u5177\u6216\u5e93\uff1f<\/strong><br \/>\u6709\u591a\u4e2a\u5de5\u5177\u548c\u5e93\u53ef\u4ee5\u5e2e\u52a9\u4f18\u5316Python\u6027\u80fd\u3002<code>NumPy<\/code>\u548c<code>Pandas<\/code>\u662f\u7528\u4e8e\u9ad8\u6548\u6570\u503c\u8ba1\u7b97\u548c\u6570\u636e\u5904\u7406\u7684\u5e93\uff0c\u9002\u5408\u5904\u7406\u5927\u6570\u636e\u96c6\u3002<code>Cython<\/code>\u53ef\u4ee5\u5c06Python\u4ee3\u7801\u7f16\u8bd1\u4e3aC\u8bed\u8a00\uff0c\u4ece\u800c\u63d0\u9ad8\u6267\u884c\u901f\u5ea6\u3002\u6b64\u5916\uff0c<code>multiprocessing<\/code>\u6a21\u5757\u5141\u8bb8\u5728\u591a\u4e2aCPU\u6838\u5fc3\u4e0a\u5e76\u884c\u6267\u884c\u4ee3\u7801\uff0c\u6781\u5927\u63d0\u5347\u6027\u80fd\u3002<\/p>\n<p><strong>3. \u5982\u4f55\u901a\u8fc7\u4ee3\u7801\u7ed3\u6784\u6765\u4f18\u5316Python\u6027\u80fd\uff1f<\/strong><br \/>\u4f18\u5316\u4ee3\u7801\u7ed3\u6784\u662f\u4e00\u79cd\u6709\u6548\u7684\u63d0\u5347\u6027\u80fd\u7684\u65b9\u6cd5\u3002\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u800c\u975e\u4f20\u7edf\u7684\u5faa\u73af\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u6267\u884c\u901f\u5ea6\u3002\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u5168\u5c40\u53d8\u91cf\u4f7f\u7528\uff0c\u56e0\u4e3a\u5b83\u4eec\u4f1a\u5f71\u54cd\u8bbf\u95ee\u901f\u5ea6\u3002\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5982\u5b57\u5178\u548c\u96c6\u5408\uff0c\u53ef\u4ee5\u63d0\u5347\u67e5\u627e\u548c\u5b58\u50a8\u6570\u636e\u7684\u6548\u7387\uff0c\u786e\u4fdd\u4ee3\u7801\u66f4\u52a0\u9ad8\u6548\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u63d0\u9ad8Python\u8bed\u6cd5\u6027\u80fd\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\u3001\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u5faa\u73af\u3001\u5229\u7528\u5185\u5efa\u51fd\u6570\u548c\u6a21\u5757\u3001\u91c7\u7528\u5e76\u884c\u5904 [&hellip;]","protected":false},"author":3,"featured_media":1157436,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1157430"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1157430"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1157430\/revisions"}],"predecessor-version":[{"id":1157439,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1157430\/revisions\/1157439"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1157436"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1157430"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1157430"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1157430"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}