{"id":1168623,"date":"2025-01-15T16:00:25","date_gmt":"2025-01-15T08:00:25","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1168623.html"},"modified":"2025-01-15T16:00:27","modified_gmt":"2025-01-15T08:00:27","slug":"python%e5%a6%82%e4%bd%95%e8%8e%b7%e5%8f%96%e5%85%83%e7%bb%84%e7%9a%84%e5%80%bc","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1168623.html","title":{"rendered":"python\u5982\u4f55\u83b7\u53d6\u5143\u7ec4\u7684\u503c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26065821\/080f534b-f2a2-4e03-b1c7-cab99e52b31f.webp\" alt=\"python\u5982\u4f55\u83b7\u53d6\u5143\u7ec4\u7684\u503c\" \/><\/p>\n<p><p> <strong>Python\u83b7\u53d6\u5143\u7ec4\u7684\u503c\u7684\u65b9\u6cd5\u5305\u62ec\u7d22\u5f15\u3001\u5207\u7247\u3001\u904d\u5386\u3001\u89e3\u5305\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u7b49\u3002<\/strong>\u5176\u4e2d\uff0c\u7d22\u5f15\u548c\u89e3\u5305\u662f\u6700\u5e38\u7528\u7684\u4e24\u79cd\u65b9\u6cd5\u3002<strong>\u7d22\u5f15\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7\u4f4d\u7f6e\u83b7\u53d6\u7279\u5b9a\u5143\u7d20\uff0c\u89e3\u5305\u53ef\u4ee5\u5c06\u5143\u7ec4\u4e2d\u7684\u5143\u7d20\u5206\u522b\u8d4b\u503c\u7ed9\u591a\u4e2a\u53d8\u91cf\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u7d22\u5f15<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5143\u7ec4\u662f\u4e00\u4e2a\u6709\u5e8f\u7684\u3001\u4e0d\u53ef\u53d8\u7684\u5e8f\u5217\u3002\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u6765\u83b7\u53d6\u5143\u7ec4\u4e2d\u7684\u7279\u5b9a\u5143\u7d20\u3002\u7d22\u5f15\u4ece0\u5f00\u59cb\uff0c\u8d1f\u7d22\u5f15\u4ece-1\u5f00\u59cb\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">t = (1, 2, 3, 4, 5)<\/p>\n<p>print(t[0])  # \u8f93\u51fa1<\/p>\n<p>print(t[-1])  # \u8f93\u51fa5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u7d22\u5f15\uff0c\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u5143\u7ec4\u4e2d\u7684\u7279\u5b9a\u4f4d\u7f6e\u7684\u5143\u7d20\u3002\u5982\u679c\u7d22\u5f15\u8d85\u51fa\u8303\u56f4\uff0c\u4f1a\u5f15\u53d1<code>IndexError<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5207\u7247<\/h3>\n<\/p>\n<p><p>\u5207\u7247\u662f\u83b7\u53d6\u5143\u7ec4\u4e2d\u4e00\u90e8\u5206\u5143\u7d20\u7684\u53e6\u4e00\u79cd\u65b9\u6cd5\u3002\u4f7f\u7528\u5207\u7247\u53ef\u4ee5\u65b9\u4fbf\u5730\u83b7\u53d6\u5143\u7ec4\u7684\u5b50\u5143\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">t = (1, 2, 3, 4, 5)<\/p>\n<p>print(t[1:3])  # \u8f93\u51fa(2, 3)<\/p>\n<p>print(t[:2])  # \u8f93\u51fa(1, 2)<\/p>\n<p>print(t[3:])  # \u8f93\u51fa(4, 5)<\/p>\n<p>print(t[-3:])  # \u8f93\u51fa(3, 4, 5)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5207\u7247\u64cd\u4f5c\u4e0d\u4f1a\u5f15\u53d1<code>IndexError<\/code>\uff0c\u5373\u4f7f\u5207\u7247\u7684\u8303\u56f4\u8d85\u51fa\u5143\u7ec4\u7684\u957f\u5ea6\uff0c\u8fd4\u56de\u7684\u4e5f\u53ea\u662f\u5c3d\u53ef\u80fd\u591a\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u904d\u5386<\/h3>\n<\/p>\n<p><p>\u904d\u5386\u5143\u7ec4\u53ef\u4ee5\u901a\u8fc7<code>for<\/code>\u5faa\u73af\u6765\u5b9e\u73b0\uff0c\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u9010\u4e2a\u8bbf\u95ee\u5143\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">t = (1, 2, 3, 4, 5)<\/p>\n<p>for item in t:<\/p>\n<p>    print(item)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u904d\u5386\u5143\u7ec4\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>enumerate<\/code>\u51fd\u6570\u540c\u65f6\u83b7\u53d6\u5143\u7d20\u7684\u7d22\u5f15\u548c\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">t = (1, 2, 3, 4, 5)<\/p>\n<p>for index, value in enumerate(t):<\/p>\n<p>    print(f&quot;Index: {index}, Value: {value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u89e3\u5305<\/h3>\n<\/p>\n<p><p>\u89e3\u5305\u662f\u5c06\u5143\u7ec4\u4e2d\u7684\u5143\u7d20\u5206\u522b\u8d4b\u503c\u7ed9\u591a\u4e2a\u53d8\u91cf\u7684\u8fc7\u7a0b\u3002\u8fd9\u79cd\u65b9\u6cd5\u7279\u522b\u9002\u7528\u4e8e\u5143\u7ec4\u7684\u957f\u5ea6\u5df2\u77e5\u4e14\u9700\u8981\u5206\u522b\u5904\u7406\u6bcf\u4e2a\u5143\u7d20\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">t = (1, 2, 3)<\/p>\n<p>a, b, c = t<\/p>\n<p>print(a, b, c)  # \u8f93\u51fa1 2 3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u51fd\u6570\u8fd4\u56de\u591a\u4e2a\u503c\u65f6\uff0c\u5e38\u7528\u5143\u7ec4\u89e3\u5305\u6765\u83b7\u53d6\u5404\u4e2a\u8fd4\u56de\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def get_coordinates():<\/p>\n<p>    return (10, 20)<\/p>\n<p>x, y = get_coordinates()<\/p>\n<p>print(f&quot;x: {x}, y: {y}&quot;)  # \u8f93\u51fax: 10, y: 20<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5185\u7f6e\u51fd\u6570\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u64cd\u4f5c\u5143\u7ec4\uff0c\u4f8b\u5982<code>len<\/code>\u3001<code>max<\/code>\u3001<code>min<\/code>\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">t = (1, 2, 3, 4, 5)<\/p>\n<p>print(len(t))  # \u8f93\u51fa5<\/p>\n<p>print(max(t))  # \u8f93\u51fa5<\/p>\n<p>print(min(t))  # \u8f93\u51fa1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e9b\u5185\u7f6e\u51fd\u6570\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5feb\u901f\u83b7\u53d6\u5143\u7ec4\u7684\u957f\u5ea6\u3001\u6700\u5927\u503c\u548c\u6700\u5c0f\u503c\u7b49\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5d4c\u5957\u5143\u7ec4\u7684\u8bbf\u95ee<\/h3>\n<\/p>\n<p><p>\u5143\u7ec4\u53ef\u4ee5\u5305\u542b\u5176\u4ed6\u5143\u7ec4\u4f5c\u4e3a\u5143\u7d20\uff0c\u8fd9\u79cd\u60c5\u51b5\u79f0\u4e3a\u5d4c\u5957\u5143\u7ec4\u3002\u8bbf\u95ee\u5d4c\u5957\u5143\u7ec4\u4e2d\u7684\u5143\u7d20\u9700\u8981\u591a\u5c42\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">t = (1, 2, (3, 4, 5), 6)<\/p>\n<p>print(t[2])  # \u8f93\u51fa(3, 4, 5)<\/p>\n<p>print(t[2][1])  # \u8f93\u51fa4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5d4c\u5957\u5143\u7ec4\u7684\u8bbf\u95ee\u9700\u8981\u9010\u5c42\u7d22\u5f15\uff0c\u76f4\u81f3\u8bbf\u95ee\u5230\u6700\u7ec8\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u5143\u7ec4\u7684\u4e0d\u53ef\u53d8\u6027<\/h3>\n<\/p>\n<p><p>\u5143\u7ec4\u7684\u4e00\u4e2a\u91cd\u8981\u7279\u70b9\u662f\u4e0d\u53ef\u53d8\u6027\u3002\u521b\u5efa\u5143\u7ec4\u540e\uff0c\u4e0d\u80fd\u4fee\u6539\u5176\u5143\u7d20\u7684\u503c\u3002\u8fd9\u610f\u5473\u7740\u4e0d\u80fd\u901a\u8fc7\u7d22\u5f15\u4fee\u6539\u5143\u7ec4\u4e2d\u7684\u67d0\u4e2a\u5143\u7d20\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u91cd\u65b0\u8d4b\u503c\u6574\u4e2a\u5143\u7ec4\u6765\u5b9e\u73b0\u201c\u4fee\u6539\u201d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">t = (1, 2, 3)<\/p>\n<h2><strong>t[0] = 4  # \u8fd9\u5c06\u5f15\u53d1TypeError<\/strong><\/h2>\n<p>t = (4, 2, 3)  # \u91cd\u65b0\u8d4b\u503c\u6574\u4e2a\u5143\u7ec4<\/p>\n<p>print(t)  # \u8f93\u51fa(4, 2, 3)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u5143\u7ec4\u548c\u5217\u8868\u7684\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u867d\u7136\u5143\u7ec4\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u4f46\u53ef\u4ee5\u5c06\u5176\u8f6c\u6362\u4e3a\u5217\u8868\uff0c\u8fdb\u884c\u4fee\u6539\u540e\u518d\u8f6c\u6362\u56de\u5143\u7ec4\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5728\u9700\u8981\u4fee\u6539\u5143\u7ec4\u5185\u5bb9\u65f6\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">t = (1, 2, 3)<\/p>\n<p>l = list(t)<\/p>\n<p>l[0] = 4<\/p>\n<p>t = tuple(l)<\/p>\n<p>print(t)  # \u8f93\u51fa(4, 2, 3)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u7e41\u7410\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u5143\u7ec4\u4f5c\u4e3a\u5b57\u5178\u7684\u952e<\/h3>\n<\/p>\n<p><p>\u7531\u4e8e\u5143\u7ec4\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u53ef\u4ee5\u5c06\u5176\u7528\u4f5c\u5b57\u5178\u7684\u952e\u3002\u8fd9\u5728\u9700\u8981\u590d\u5408\u952e\u7684\u60c5\u51b5\u4e0b\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">d = {}<\/p>\n<p>key = (1, 2)<\/p>\n<p>d[key] = &quot;value&quot;<\/p>\n<p>print(d)  # \u8f93\u51fa{(1, 2): &#39;value&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5143\u7ec4\u7684\u4e0d\u53ef\u53d8\u6027\u4f7f\u5176\u6210\u4e3a\u5b57\u5178\u952e\u7684\u7406\u60f3\u9009\u62e9\uff0c\u56e0\u4e3a\u5b57\u5178\u952e\u5fc5\u987b\u662f\u4e0d\u53ef\u53d8\u7684\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u5143\u7ec4\u7684\u6bd4\u8f83<\/h3>\n<\/p>\n<p><p>\u5143\u7ec4\u53ef\u4ee5\u901a\u8fc7\u6bd4\u8f83\u8fd0\u7b97\u7b26\u8fdb\u884c\u6bd4\u8f83\u3002\u6bd4\u8f83\u662f\u57fa\u4e8e\u5b57\u5178\u5e8f\u8fdb\u884c\u7684\uff0c\u5373\u9996\u5148\u6bd4\u8f83\u7b2c\u4e00\u4e2a\u5143\u7d20\uff0c\u5982\u679c\u76f8\u7b49\u5219\u6bd4\u8f83\u7b2c\u4e8c\u4e2a\u5143\u7d20\uff0c\u4ee5\u6b64\u7c7b\u63a8\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">t1 = (1, 2, 3)<\/p>\n<p>t2 = (1, 2, 4)<\/p>\n<p>print(t1 &lt; t2)  # \u8f93\u51faTrue<\/p>\n<p>print(t1 == t2)  # \u8f93\u51faFalse<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u6bd4\u8f83\u65b9\u5f0f\u4f7f\u5f97\u5143\u7ec4\u5728\u6392\u5e8f\u548c\u5176\u4ed6\u9700\u8981\u987a\u5e8f\u6bd4\u8f83\u7684\u64cd\u4f5c\u4e2d\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u5143\u7ec4\u7684\u6210\u5458\u68c0\u6d4b<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>in<\/code>\u548c<code>not in<\/code>\u8fd0\u7b97\u7b26\u68c0\u67e5\u5143\u7ec4\u4e2d\u662f\u5426\u5305\u542b\u67d0\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">t = (1, 2, 3, 4, 5)<\/p>\n<p>print(3 in t)  # \u8f93\u51faTrue<\/p>\n<p>print(6 not in t)  # \u8f93\u51faTrue<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u7b80\u6d01\uff0c\u53ef\u4ee5\u5feb\u901f\u68c0\u67e5\u5143\u7ec4\u662f\u5426\u5305\u542b\u67d0\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e8c\u3001\u5143\u7ec4\u7684\u5e38\u89c1\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u5143\u7ec4\u8fd8\u652f\u6301\u4e00\u4e9b\u5e38\u89c1\u64cd\u4f5c\uff0c\u4f8b\u5982\u8fde\u63a5\u548c\u91cd\u590d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">t1 = (1, 2, 3)<\/p>\n<p>t2 = (4, 5, 6)<\/p>\n<p>print(t1 + t2)  # \u8f93\u51fa(1, 2, 3, 4, 5, 6)<\/p>\n<p>print(t1 * 2)  # \u8f93\u51fa(1, 2, 3, 1, 2, 3)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e9b\u64cd\u4f5c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5728\u4e0d\u4fee\u6539\u539f\u59cb\u5143\u7ec4\u7684\u60c5\u51b5\u4e0b\u751f\u6210\u65b0\u7684\u5143\u7ec4\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e09\u3001\u5143\u7ec4\u7684\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u5143\u7ec4\u5728Python\u4e2d\u6709\u5f88\u591a\u5e94\u7528\u573a\u666f\uff0c\u5305\u62ec\u4f46\u4e0d\u9650\u4e8e\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u8fd4\u56de\u591a\u4e2a\u503c<\/strong>\uff1a\u51fd\u6570\u53ef\u4ee5\u901a\u8fc7\u8fd4\u56de\u5143\u7ec4\u6765\u8fd4\u56de\u591a\u4e2a\u503c\u3002<\/li>\n<li><strong>\u6570\u636e\u5b58\u50a8<\/strong>\uff1a\u5143\u7ec4\u53ef\u4ee5\u7528\u6765\u5b58\u50a8\u4e0d\u9700\u8981\u4fee\u6539\u7684\u6570\u636e\uff0c\u4f8b\u5982\u914d\u7f6e\u548c\u5e38\u91cf\u3002<\/li>\n<li><strong>\u4f5c\u4e3a\u5b57\u5178\u7684\u952e<\/strong>\uff1a\u5143\u7ec4\u53ef\u4ee5\u4f5c\u4e3a\u5b57\u5178\u7684\u590d\u5408\u952e\u3002<\/li>\n<li><strong>\u7b80\u5316\u4ee3\u7801<\/strong>\uff1a\u89e3\u5305\u548c\u904d\u5386\u7b49\u64cd\u4f5c\u53ef\u4ee5\u7b80\u5316\u4ee3\u7801\uff0c\u4f7f\u5176\u66f4\u7b80\u6d01\u3002<\/li>\n<\/ul>\n<p><h3>\u5341\u56db\u3001\u5143\u7ec4\u7684\u6027\u80fd<\/h3>\n<\/p>\n<p><p>\u7531\u4e8e\u5143\u7ec4\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u5176\u6027\u80fd\u901a\u5e38\u6bd4\u5217\u8868\u8981\u597d\u3002\u5728\u9700\u8981\u9ad8\u6548\u5b58\u50a8\u548c\u8bbf\u95ee\u6570\u636e\u7684\u573a\u666f\u4e2d\uff0c\u5143\u7ec4\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import timeit<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5927\u5217\u8868\u548c\u5143\u7ec4<\/strong><\/h2>\n<p>lst = list(range(1000000))<\/p>\n<p>tpl = tuple(range(1000000))<\/p>\n<h2><strong>\u6d4b\u8bd5\u5217\u8868\u8bbf\u95ee\u65f6\u95f4<\/strong><\/h2>\n<p>lst_time = timeit.timeit(stmt=&quot;lst[500000]&quot;, setup=&quot;from __m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__ import lst&quot;, number=1000000)<\/p>\n<p>print(f&quot;List access time: {lst_time}&quot;)<\/p>\n<h2><strong>\u6d4b\u8bd5\u5143\u7ec4\u8bbf\u95ee\u65f6\u95f4<\/strong><\/h2>\n<p>tpl_time = timeit.timeit(stmt=&quot;tpl[500000]&quot;, setup=&quot;from __main__ import tpl&quot;, number=1000000)<\/p>\n<p>print(f&quot;Tuple access time: {tpl_time}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u4ee3\u7801\uff0c\u53ef\u4ee5\u6bd4\u8f83\u5217\u8868\u548c\u5143\u7ec4\u7684\u8bbf\u95ee\u65f6\u95f4\uff0c\u901a\u5e38\u60c5\u51b5\u4e0b\u5143\u7ec4\u7684\u8bbf\u95ee\u901f\u5ea6\u4f1a\u66f4\u5feb\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e94\u3001\u5143\u7ec4\u7684\u5185\u5b58\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>\u7531\u4e8e\u5143\u7ec4\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u5176\u5185\u5b58\u4f7f\u7528\u901a\u5e38\u6bd4\u5217\u8868\u8981\u5c11\u3002\u5728\u9700\u8981\u9ad8\u6548\u5229\u7528\u5185\u5b58\u7684\u573a\u666f\u4e2d\uff0c\u5143\u7ec4\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>lst = list(range(1000000))<\/p>\n<p>tpl = tuple(range(1000000))<\/p>\n<p>print(f&quot;List size: {sys.getsizeof(lst)} bytes&quot;)<\/p>\n<p>print(f&quot;Tuple size: {sys.getsizeof(tpl)} bytes&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u4ee3\u7801\uff0c\u53ef\u4ee5\u6bd4\u8f83\u5217\u8868\u548c\u5143\u7ec4\u7684\u5185\u5b58\u4f7f\u7528\uff0c\u901a\u5e38\u60c5\u51b5\u4e0b\u5143\u7ec4\u7684\u5185\u5b58\u4f7f\u7528\u4f1a\u66f4\u5c11\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u516d\u3001\u5143\u7ec4\u7684\u4e0d\u53ef\u53d8\u6027\u5e26\u6765\u7684\u597d\u5904<\/h3>\n<\/p>\n<p><p>\u5143\u7ec4\u7684\u4e0d\u53ef\u53d8\u6027\u5e26\u6765\u4e86\u5f88\u591a\u597d\u5904\uff0c\u5305\u62ec\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u5b89\u5168\u6027<\/strong>\uff1a\u4e0d\u53ef\u53d8\u5bf9\u8c61\u66f4\u5b89\u5168\uff0c\u56e0\u4e3a\u5b83\u4eec\u4e0d\u80fd\u88ab\u610f\u5916\u4fee\u6539\u3002<\/li>\n<li><strong>\u54c8\u5e0c\u503c<\/strong>\uff1a\u4e0d\u53ef\u53d8\u5bf9\u8c61\u53ef\u4ee5\u88ab\u54c8\u5e0c\uff0c\u56e0\u6b64\u53ef\u4ee5\u7528\u4f5c\u5b57\u5178\u7684\u952e\u3002<\/li>\n<li><strong>\u7ebf\u7a0b\u5b89\u5168<\/strong>\uff1a\u4e0d\u53ef\u53d8\u5bf9\u8c61\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\uff0c\u56e0\u4e3a\u5b83\u4eec\u4e0d\u80fd\u88ab\u4fee\u6539\u3002<\/li>\n<\/ul>\n<p><h3>\u5341\u4e03\u3001\u5143\u7ec4\u7684\u4e0d\u8db3<\/h3>\n<\/p>\n<p><p>\u867d\u7136\u5143\u7ec4\u6709\u5f88\u591a\u4f18\u70b9\uff0c\u4f46\u5b83\u4eec\u4e5f\u6709\u4e00\u4e9b\u4e0d\u8db3\u4e4b\u5904\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u4e0d\u53ef\u53d8\u6027<\/strong>\uff1a\u4e0d\u53ef\u53d8\u6027\u867d\u7136\u5e26\u6765\u4e86\u5f88\u591a\u597d\u5904\uff0c\u4f46\u4e5f\u4f7f\u5f97\u5143\u7ec4\u4e0d\u80fd\u76f4\u63a5\u4fee\u6539\uff0c\u9700\u8981\u91cd\u65b0\u8d4b\u503c\u3002<\/li>\n<li><strong>\u529f\u80fd\u6709\u9650<\/strong>\uff1a\u4e0e\u5217\u8868\u76f8\u6bd4\uff0c\u5143\u7ec4\u7684\u529f\u80fd\u8f83\u4e3a\u6709\u9650\uff0c\u4e0d\u80fd\u8fdb\u884c\u63d2\u5165\u3001\u5220\u9664\u7b49\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<p><h3>\u5341\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0cPython\u83b7\u53d6\u5143\u7ec4\u7684\u503c\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u7d22\u5f15\u3001\u5207\u7247\u3001\u904d\u5386\u3001\u89e3\u5305\u548c\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u7b49\u3002\u5143\u7ec4\u4f5c\u4e3a\u4e00\u79cd\u4e0d\u53ef\u53d8\u7684\u5e8f\u5217\uff0c\u6709\u5176\u72ec\u7279\u7684\u4f18\u52bf\u548c\u5e94\u7528\u573a\u666f\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\u548c\u64cd\u4f5c\u65b9\u6cd5\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u6548\u7387\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bbf\u95ee\u5143\u7ec4\u4e2d\u7684\u7279\u5b9a\u5143\u7d20\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u8bbf\u95ee\u5143\u7ec4\u7684\u5143\u7d20\u975e\u5e38\u7b80\u5355\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u7d22\u5f15\u6765\u83b7\u53d6\u7279\u5b9a\u7684\u503c\uff0c\u7d22\u5f15\u4ece0\u5f00\u59cb\u3002\u4f8b\u5982\uff0c\u82e5\u6709\u4e00\u4e2a\u5143\u7ec4<code>my_tuple = (1, 2, 3, 4)<\/code>\uff0c\u8981\u83b7\u53d6\u7b2c\u4e8c\u4e2a\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528<code>my_tuple[1]<\/code>\uff0c\u7ed3\u679c\u5c06\u662f<code>2<\/code>\u3002\u540c\u65f6\uff0cPython\u8fd8\u652f\u6301\u8d1f\u7d22\u5f15\uff0c\u4f60\u53ef\u4ee5\u7528<code>my_tuple[-1]<\/code>\u6765\u83b7\u53d6\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff0c\u7ed3\u679c\u662f<code>4<\/code>\u3002<\/p>\n<p><strong>\u5143\u7ec4\u4e0e\u5217\u8868\u7684\u533a\u522b\u662f\u4ec0\u4e48\uff0c\u4e3a\u4ec0\u4e48\u9009\u62e9\u5143\u7ec4\uff1f<\/strong><br \/>\u5143\u7ec4\u548c\u5217\u8868\u90fd\u662fPython\u4e2d\u7684\u5e8f\u5217\u7c7b\u578b\uff0c\u4f46\u5b83\u4eec\u6709\u51e0\u4e2a\u5173\u952e\u533a\u522b\u3002\u5143\u7ec4\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u8fd9\u610f\u5473\u7740\u4e00\u65e6\u521b\u5efa\u5c31\u4e0d\u80fd\u4fee\u6539\u5176\u5185\u5bb9\uff0c\u800c\u5217\u8868\u662f\u53ef\u53d8\u7684\uff0c\u53ef\u4ee5\u968f\u610f\u6dfb\u52a0\u3001\u5220\u9664\u6216\u66f4\u6539\u5143\u7d20\u3002\u8fd9\u79cd\u4e0d\u53ef\u53d8\u6027\u4f7f\u5f97\u5143\u7ec4\u5728\u9700\u8981\u4fdd\u62a4\u6570\u636e\u4e0d\u88ab\u610f\u5916\u4fee\u6539\u65f6\u975e\u5e38\u6709\u7528\u3002\u6b64\u5916\uff0c\u5143\u7ec4\u901a\u5e38\u5360\u7528\u7684\u5185\u5b58\u66f4\u5c11\uff0c\u8bbf\u95ee\u901f\u5ea6\u4e5f\u66f4\u5feb\uff0c\u56e0\u6b64\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\uff0c\u4f7f\u7528\u5143\u7ec4\u53ef\u80fd\u66f4\u9ad8\u6548\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06\u5143\u7ec4\u8f6c\u6362\u4e3a\u5176\u4ed6\u6570\u636e\u7c7b\u578b\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u5c06\u5143\u7ec4\u8f6c\u6362\u4e3a\u5176\u4ed6\u6570\u636e\u7c7b\u578b\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>list()<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06\u5143\u7ec4\u8f6c\u6362\u4e3a\u5217\u8868\uff0c\u4f7f\u7528<code>set()<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06\u5176\u8f6c\u6362\u4e3a\u96c6\u5408\uff0c\u4f7f\u7528<code>str()<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002\u6bd4\u5982\uff0c\u82e5\u6709\u5143\u7ec4<code>my_tuple = (1, 2, 3)<\/code>\uff0c\u5219\u4f7f\u7528<code>list(my_tuple)<\/code>\u5c06\u5176\u8f6c\u6362\u4e3a\u5217\u8868<code>[1, 2, 3]<\/code>\u3002\u8fd9\u6837\u7684\u8f6c\u6362\u5728\u9700\u8981\u4e0d\u540c\u6570\u636e\u64cd\u4f5c\u65f6\u975e\u5e38\u65b9\u4fbf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u83b7\u53d6\u5143\u7ec4\u7684\u503c\u7684\u65b9\u6cd5\u5305\u62ec\u7d22\u5f15\u3001\u5207\u7247\u3001\u904d\u5386\u3001\u89e3\u5305\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u7b49\u3002\u5176\u4e2d\uff0c\u7d22\u5f15\u548c\u89e3\u5305\u662f\u6700\u5e38\u7528\u7684\u4e24\u79cd\u65b9\u6cd5\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1168630,"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\/1168623"}],"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=1168623"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1168623\/revisions"}],"predecessor-version":[{"id":1168631,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1168623\/revisions\/1168631"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1168630"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1168623"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1168623"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1168623"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}