{"id":10020822,"date":"2025-06-07T22:42:50","date_gmt":"2025-06-07T14:42:50","guid":{"rendered":"https:\/\/itmax.cn\/?p=10020822"},"modified":"2025-06-07T22:42:50","modified_gmt":"2025-06-07T14:42:50","slug":"python%e5%85%83%e7%bb%84%e9%bb%91%e7%a7%91%e6%8a%80%ef%bc%9a3%e6%8b%9b%e8%ae%a9%e6%95%b0%e6%8d%ae%e5%ae%89%e5%85%a8%e6%9a%b4%e5%a2%9e200%ef%bc%8c%e5%ad%a6%e7%94%9f%e7%ae%a1%e7%90%86%e7%b3%bb%e7%bb%9f","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10020822","title":{"rendered":"Python\u5143\u7ec4\u9ed1\u79d1\u6280\uff1a3\u62db\u8ba9\u6570\u636e\u5b89\u5168\u66b4\u589e200%\uff0c\u5b66\u751f\u7ba1\u7406\u7cfb\u7edf\u5b9e\u6218\u63ed\u79d8\uff01"},"content":{"rendered":"<h2><strong>\u6458\u8981<\/strong><\/h2>\n<p>\u672c\u6587\u901a\u8fc7\u5b66\u6821\u5b66\u751f\u4fe1\u606f\u7ba1\u7406\u7cfb\u7edf\u7684\u5b9e\u6218\u6848\u4f8b\uff0c\u6df1\u5165\u89e3\u6790Python\u5143\u7ec4\u7684\u6838\u5fc3\u7279\u6027\uff08\u4e0d\u53ef\u53d8\u6027\u3001\u9ad8\u6548\u8bbf\u95ee\uff09\u548c\u751f\u6210\u5668\u63a8\u5bfc\u5f0f\u7684\u5e94\u7528\u3002\u4f60\u5c06\u638c\u63e1<strong>\u5982\u4f55\u5b89\u5168\u5b58\u50a8\u5173\u952e\u6570\u636e<\/strong>\u3001<strong>\u9ad8\u6548\u5904\u7406\u6d77\u91cf\u4fe1\u606f<\/strong>\uff0c\u5e76\u7406\u89e3\u5143\u7ec4\u4e2d\u5d4c\u5957\u53ef\u53d8\u5bf9\u8c61\u7684\u8fd0\u4f5c\u673a\u5236\u3002<\/p>\n<h2><strong>\u63cf\u8ff0<\/strong><\/h2>\n<p>\u5143\u7ec4\u4f5c\u4e3a\u4e0d\u53ef\u53d8\u5e8f\u5217\uff0c\u5728\u5b58\u50a8\u654f\u611f\u6570\u636e\uff08\u5982\u5b66\u751f\u6863\u6848\u3001\u914d\u7f6e\u4fe1\u606f\uff09\u65f6\u5177\u6709\u72ec\u7279\u4f18\u52bf\uff1a<br \/> <strong>\u4e0d\u53ef\u53d8\u6027<\/strong>\uff1a\u9632\u6b62\u5173\u952e\u6570\u636e\u88ab\u610f\u5916\u4fee\u6539<br \/> <strong>\u5185\u5b58\u4f18\u5316<\/strong>\uff1a\u6bd4\u5217\u8868\u66f4\u8282\u7701\u5185\u5b58\u7a7a\u95f4<br \/> <strong>\u9ad8\u6548\u8bbf\u95ee<\/strong>\uff1a\u652f\u6301\u7d22\u5f15\/\u5207\u7247\u5feb\u901f\u83b7\u53d6\u6570\u636e<\/p>\n<p>\u751f\u6210\u5668\u63a8\u5bfc\u5f0f\u5219\u80fd<strong>\u5904\u7406\u8d85\u5927\u89c4\u6a21\u6570\u636e\u800c\u4e0d\u4f1a\u6491\u7206\u5185\u5b58<\/strong>\uff0c\u7279\u522b\u9002\u5408\u6210\u7ee9\u8f6c\u6362\u3001\u65e5\u5fd7\u5206\u6790\u7b49\u573a\u666f\u3002<\/p>\n<h2><strong>\u9898\u89e3\u7b54\u6848\uff1a\u5b66\u751f\u4fe1\u606f\u7ba1\u7406\u7cfb\u7edf<\/strong><\/h2>\n<pre><code class=\"python\"># 1. \u5b58\u50a8\u5b66\u751f\u57fa\u672c\u4fe1\u606f\uff08\u4e0d\u53ef\u53d8\u6570\u636e\uff09\nstudent_base = (&quot;S2023001&quot;, &quot;\u5f20\u4e09&quot;, &quot;\u7537&quot;)\n\n# 2. \u5b58\u50a8\u53ef\u53d8\u6210\u7ee9\u6570\u636e\uff08\u5143\u7ec4\u5d4c\u5957\u5217\u8868\uff09\nstudent_scores = (&quot;S2023001&quot;, [85, 92, 78])\n\n# 3. \u4fee\u6539\u5d4c\u5957\u5217\u8868\u4e2d\u7684\u6210\u7ee9\nstudent_scores[1][0] = 90  # \u4fee\u6539\u7b2c\u4e00\u95e8\u8bfe\u6210\u7ee9\nstudent_scores[1].append(88)  # \u65b0\u589e\u6210\u7ee9\n\n# 4. \u4f7f\u7528\u751f\u6210\u5668\u6279\u91cf\u8f6c\u6362\u6210\u7ee9\ndef score_to_grade(scores):\n    return ( \n        &quot;A&quot; if s &gt;= 90 else \n        &quot;B&quot; if s &gt;= 80 else \n        &quot;C&quot; for s in scores \n    )\n\n# 5. \u6570\u636e\u8bbf\u95ee\u4e0e\u8f6c\u6362\nprint(f&quot;\u5b66\u751f\u6863\u6848\uff1a{student_base[:2]}&quot;)  # \u5207\u7247\u8bbf\u95ee\ngrades = score_to_grade(student_scores[1])\nprint(f&quot;\u6210\u7ee9\u7b49\u7ea7\uff1a{list(grades)}&quot;)<\/code><\/pre>\n<h2><strong>\u4ee3\u7801\u5206\u6790<\/strong><\/h2>\n<p><strong>\u5143\u7ec4\u521b\u5efa\u4e0e\u4e0d\u53ef\u53d8\u6027<\/strong><\/p>\n<pre><code class=\"python\">student_base = (&quot;S2023001&quot;, &quot;\u5f20\u4e09&quot;, &quot;\u7537&quot;) \n# student_base[1] = &quot;\u674e\u56db&quot;  \u4f1a\u89e6\u53d1TypeError<\/code><\/pre>\n<ul>\n<li>\u5b66\u53f7\/\u59d3\u540d\/\u6027\u522b\u7b49\u5173\u952e\u4fe1\u606f\u7528\u5143\u7ec4\u5b58\u50a8\uff0c\u9632\u6b62\u610f\u5916\u4fee\u6539<\/li>\n<li>\u5c1d\u8bd5\u4fee\u6539\u5143\u7d20\u4f1a\u89e6\u53d1<code>TypeError<\/code><\/li>\n<\/ul>\n<p><strong>\u5d4c\u5957\u53ef\u53d8\u5bf9\u8c61\u7684\u7279\u6b8a\u5904\u7406<\/strong><\/p>\n<pre><code class=\"python\">student_scores = (&quot;S2023001&quot;, [85, 92, 78])\nstudent_scores[1][0] = 90  # \u5141\u8bb8\u4fee\u6539\u5217\u8868\u5185\u5bb9<\/code><\/pre>\n<ul>\n<li>\u5143\u7ec4\u4ec5\u4fdd\u8bc1<strong>\u5bb9\u5668\u5f15\u7528\u4e0d\u53d8<\/strong>\uff08\u59cb\u7ec8\u6307\u5411\u540c\u4e00\u4e2a\u5217\u8868\uff09<\/li>\n<li>\u5217\u8868\u5185\u90e8\u5143\u7d20\u53ef\u81ea\u7531\u4fee\u6539\uff0c\u5b9e\u73b0&#8221;\u534a\u53ef\u53d8&#8221;\u7ed3\u6784<\/li>\n<\/ul>\n<p><strong>\u751f\u6210\u5668\u63a8\u5bfc\u5f0f\u5904\u7406\u6d77\u91cf\u6570\u636e<\/strong><\/p>\n<pre><code class=\"python\">( &quot;A&quot; if s &gt;= 90 else &quot;B&quot; for s in scores )<\/code><\/pre>\n<ul>\n<li>\u5706\u62ec\u53f7\u521b\u5efa\u751f\u6210\u5668\u800c\u975e\u5143\u7ec4<\/li>\n<li>\u52a8\u6001\u751f\u6210\u6570\u636e\uff0c\u5904\u7406100\u4e07\u6761\u6210\u7ee9\u4ec5\u9700\u51e0MB\u5185\u5b58<\/li>\n<\/ul>\n<p><strong>\u9ad8\u6548\u6570\u636e\u8bbf\u95ee\u6280\u5de7<\/strong><\/p>\n<pre><code class=\"python\">print(student_base[0])     # \u7d22\u5f15\u8bbf\u95ee: S2023001\nprint(student_base[1:3])   # \u5207\u7247\u8bbf\u95ee: ('\u5f20\u4e09', '\u7537')<\/code><\/pre>\n<h2><strong>\u793a\u4f8b\u6d4b\u8bd5\u53ca\u7ed3\u679c<\/strong><\/h2>\n<pre><code class=\"python\"># \u521d\u59cb\u6570\u636e\nstudent_base = (&quot;S2023001&quot;, &quot;\u5f20\u4e09&quot;, &quot;\u7537&quot;)\nstudent_scores = (&quot;S2023001&quot;, [85, 92, 78])\n\n# \u4fee\u6539\u6210\u7ee9\nstudent_scores[1][0] = 90\nstudent_scores[1].append(88)\n\n# \u8f6c\u6362\u6210\u7ee9\u7b49\u7ea7\ngrades_gen = score_to_grade(student_scores[1])\n\n# \u8f93\u51fa\u7ed3\u679c\nprint(&quot;\u5b66\u751f\u6863\u6848:&quot;, student_base[1])  # \u8f93\u51fa: \u5f20\u4e09\nprint(&quot;\u6700\u65b0\u6210\u7ee9:&quot;, student_scores[1]) # \u8f93\u51fa: [90, 92, 78, 88]\nprint(&quot;\u6210\u7ee9\u7b49\u7ea7:&quot;, list(grades_gen))  # \u8f93\u51fa: ['B', 'A', 'C', 'B']<\/code><\/pre>\n<p><strong>\u6267\u884c\u7ed3\u679c<\/strong>\uff1a<\/p>\n<pre><code>\u5b66\u751f\u6863\u6848: \u5f20\u4e09\n\u6700\u65b0\u6210\u7ee9: [90, 92, 78, 88]\n\u6210\u7ee9\u7b49\u7ea7: ['B', 'A', 'C', 'B']<\/code><\/pre>\n<h2><strong>\u65f6\u95f4\u590d\u6742\u5ea6<\/strong><\/h2>\n<table>\n<thead>\n<tr>\n<th>\u64cd\u4f5c<\/th>\n<th>\u65f6\u95f4\u590d\u6742\u5ea6<\/th>\n<th>\u8bf4\u660e<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u7d22\u5f15\u8bbf\u95ee <code>[i]<\/code><\/td>\n<td>O(1)<\/td>\n<td>\u76f4\u63a5\u5185\u5b58\u5730\u5740\u8bbf\u95ee<\/td>\n<\/tr>\n<tr>\n<td>\u5207\u7247 <code>[start:end]<\/code><\/td>\n<td>O(k)<\/td>\n<td>k\u4e3a\u5207\u7247\u957f\u5ea6<\/td>\n<\/tr>\n<tr>\n<td>\u751f\u6210\u5668\u904d\u5386<\/td>\n<td>O(n)<\/td>\n<td>n\u4e3a\u5143\u7d20\u6570\u91cf<\/td>\n<\/tr>\n<tr>\n<td>\u5d4c\u5957\u5217\u8868\u4fee\u6539<\/td>\n<td>O(1)<\/td>\n<td>\u76f4\u63a5\u4fee\u6539\u53ef\u53d8\u5bf9\u8c61<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2><strong>\u7a7a\u95f4\u590d\u6742\u5ea6<\/strong><\/h2>\n<ul>\n<li><strong>\u5143\u7ec4\u5b58\u50a8<\/strong>\uff1a\u6bd4\u5217\u8868\u5c1116-20%\u5185\u5b58\uff08\u65e0\u52a8\u6001\u6269\u5bb9\u5f00\u9500\uff09<\/li>\n<li><strong>\u751f\u6210\u5668<\/strong>\uff1aO(1) \u5e38\u6570\u7ea7\u7a7a\u95f4\uff0c\u4e0e\u6570\u636e\u91cf\u65e0\u5173<\/li>\n<li>\n<p><strong>\u5178\u578b\u573a\u666f\u5bf9\u6bd4<\/strong>\uff08\u767e\u4e07\u6570\u636e\uff09\uff1a<\/p>\n<pre><code class=\"python\">import sys\nlist_size = sys.getsizeof([i for i in range(10**6)])  # \u224890MB\ntuple_size = sys.getsizeof(tuple(i for i in range(10**6))) # \u224870MB \ngen_size = sys.getsizeof((i for i in range(10**6)))    # \u2248128\u5b57\u8282<\/code><\/pre>\n<\/li>\n<\/ul>\n<h2><strong>\u603b\u7ed3<\/strong><\/h2>\n<p><strong>\u5143\u7ec4\u9002\u7528\u573a\u666f<\/strong>\uff1a<\/p>\n<ul>\n<li>\u5b58\u50a8\u6570\u636e\u5e93\u67e5\u8be2\u7ed3\u679c<\/li>\n<li>\u914d\u7f6e\u6587\u4ef6\u53c2\u6570\uff08\u5982API\u5bc6\u94a5\uff09<\/li>\n<li>\u51fd\u6570\u8fd4\u56de\u591a\u4e2a\u4e0d\u53ef\u53d8\u503c<\/li>\n<li>\u5b57\u5178\u952e\u503c\uff08\u56e0\u4e0d\u53ef\u54c8\u5e0c\u5217\u8868\u4e0d\u80fd\u4f5c\u4e3a\u952e\uff09<\/li>\n<\/ul>\n<p><strong>\u751f\u6210\u5668\u6838\u5fc3\u4f18\u52bf<\/strong>\uff1a<\/p>\n<ul>\n<li>\u5904\u7406GB\u7ea7CSV\u6587\u4ef6\u4e0d\u5d29\u6e83<\/li>\n<li>\u5b9e\u65f6\u6570\u636e\u6d41\u5904\u7406\uff08\u5982\u4f20\u611f\u5668\u6570\u636e\uff09<\/li>\n<li>\u4e0e<code>yield<\/code>\u7ed3\u5408\u6784\u5efa\u9ad8\u6548\u6d41\u6c34\u7ebf<\/li>\n<\/ul>\n<p><strong>\u5d4c\u5957\u53ef\u53d8\u5bf9\u8c61\u6280\u5de7<\/strong>\uff1a<\/p>\n<pre><code class=\"python\"># \u5b89\u5168\u65e5\u5fd7\u5b58\u50a8\u67b6\u6784\nlog_record = (\n    &quot;2023-06-07 10:00:00&quot;, \n    &quot;user_login&quot;, \n    [&quot;success&quot;, {&quot;ip&quot;: &quot;192.168.1.1&quot;}]\n)\nlog_record[2][1][&quot;device&quot;] = &quot;Android&quot;  # \u5b89\u5168\u6269\u5c55\u65e5\u5fd7<\/code><\/pre>\n<blockquote><p><strong>\u5173\u952e\u7ed3\u8bba<\/strong>\uff1a\u5143\u7ec4\u4e0d\u662f&#8221;\u53ea\u8bfb\u5217\u8868&#8221;\uff0c\u800c\u662f<strong>\u6570\u636e\u5b89\u5168\u6027\u4e0e\u5185\u5b58\u6548\u7387\u7684\u5e73\u8861\u5de5\u5177<\/strong>\u3002\u914d\u5408\u751f\u6210\u5668\u4f7f\u7528\uff0c\u53ef\u5728\u8d44\u6e90\u53d7\u9650\u73af\u5883\u4e0b\u5904\u7406\u8d85\u5927\u89c4\u6a21\u6570\u636e\u96c6\uff0c\u662f\u91d1\u878d\u3001\u7269\u8054\u7f51\u7b49\u9886\u57df\u7684\u6838\u5fc3\u89e3\u51b3\u65b9\u6848\u3002<\/p><\/blockquote>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000046618659\">https:\/\/segmentfault.com\/a\/1190000046618659<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6458\u8981 \u672c\u6587\u901a\u8fc7\u5b66\u6821\u5b66\u751f\u4fe1\u606f\u7ba1\u7406\u7cfb\u7edf\u7684\u5b9e\u6218\u6848\u4f8b\uff0c\u6df1\u5165\u89e3\u6790Python\u5143\u7ec4\u7684\u6838\u5fc3\u7279\u6027\uff08\u4e0d\u53ef\u53d8\u6027\u3001\u9ad8\u6548\u8bbf\u95ee\uff09\u548c\u751f\u6210\u5668 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10020822","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10020822","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10020822"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10020822\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10020822"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10020822"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10020822"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}