{"id":2498,"date":"2025-05-31T17:26:07","date_gmt":"2025-06-04T10:07:06","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=2498"},"modified":"2025-06-04T17:26:07","modified_gmt":"2025-06-04T10:07:06","slug":"python%e4%b8%b2%e5%88%97list","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/python%e4%b8%b2%e5%88%97list\/","title":{"rendered":"Python \u4e32\u5217 (List) \u5b8c\u6574\u6307\u5357\uff1a2025 \u6700\u65b0\u64cd\u4f5c\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta charset=\"UTF-8\" \/><br \/>\n<meta name=\"description\" content=\"\u672c\u6587\u5c07\u4ecb\u7d39 Python \u4e32\u5217\uff08list\uff09\u7684\u57fa\u790e\u77e5\u8b58\uff0c\u4e26\u63d0\u4f9b 25 \u7a2e\u5e38\u7528\u7684\u4e32\u5217\u64cd\u4f5c\uff0c\u5e6b\u52a9\u60a8\u57282025\u5e74\u638c\u63e1Python\u7a0b\u5f0f\u8a2d\u8a08\u7684\u6838\u5fc3\u6280\u8853\u3002\" \/><br \/>\n<title>Python \u4e32\u5217\uff1a\u638c\u63e1\u57fa\u790e\u64cd\u4f5c\uff0c\u9032\u884c 25 \u9805\u5e38\u7528\u64cd\u4f5c<\/title><\/p>\n<h1>Python \u4e32\u5217 (List) \u5b8c\u6574\u6307\u5357\uff1a2025 \u6700\u65b0\u64cd\u4f5c\u8207\u6700\u4f73\u5be6\u8e10<\/h1>\n<h2>\u4ec0\u9ebc\u662f Python \u4e32\u5217<\/h2>\n<p>\u5728 Python \u4e2d\uff0c\u4e32\u5217\uff08list\uff09\u662f\u4e00\u7a2e\u6709\u5e8f\u7684\u8cc7\u6599\u96c6\u5408\uff0c\u7528\u65bc\u5132\u5b58\u591a\u500b\u8cc7\u6599\u9805\u76ee\u3002\u4e32\u5217\u7684\u5143\u7d20\u53ef\u4ee5\u662f\u4efb\u610f\u985e\u578b\uff0c\u4e14\u53ef\u4ee5\u5728\u57f7\u884c\u671f\u9593\u52d5\u614b\u589e\u6e1b\u3002\u9019\u4f7f\u5f97\u4e32\u5217\u6210\u70ba Python \u4e2d\u6975\u70ba\u91cd\u8981\u7684\u8cc7\u6599\u7d50\u69cb\u4e4b\u4e00\u3002<\/p>\n<pre class=\"brush: python\">\nmixed_list = [1, 'abc', 3.14, [1, 2, 3]]\nprint(mixed_list)  # \u8f38\u51fa: [1, 'abc', 3.14, [1, 2, 3]]\n<\/pre>\n<h2>\u5efa\u7acb\u4e32\u5217<\/h2>\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528\u65b9\u62ec\u865f\uff08[]\uff09\u4f86\u5efa\u7acb\u4e32\u5217\uff0c\u4e26\u5728\u65b9\u62ec\u865f\u5167\u52a0\u5165\u591a\u500b\u8cc7\u6599\u9805\u76ee\uff0c\u4ee5\u9017\u865f\uff08,\uff09\u5340\u9694\u3002\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528\u4e32\u5217\u76f8\u95dc\u7684\u51fd\u6578\uff0c\u4f8b\u5982 list() \u4f86\u5efa\u7acb\u4e32\u5217\u3002<\/p>\n<pre class=\"brush: python\">\nempty_list = []  # \u5efa\u7acb\u7a7a\u4e32\u5217\nprint(empty_list)  # \u8f38\u51fa: []\n\nnums = [1, 2, 3, 4, 5]  # \u5efa\u7acb\u6578\u5b57\u4e32\u5217\nprint(nums)  # \u8f38\u51fa: [1, 2, 3, 4, 5]\n\nwords = list('Hello')  # \u4f7f\u7528 list() \u8f49\u63db\u5b57\u4e32\u70ba\u4e32\u5217\nprint(words)  # \u8f38\u51fa: ['H', 'e', 'l', 'l', 'o']\n<\/pre>\n<h2>\u5b58\u53d6\u4e32\u5217\u5143\u7d20<\/h2>\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528 for \u8ff4\u5708\u4f86\u8b80\u53d6\u4e32\u5217\u5143\u7d20\u3002\u4f8b\u5982\uff1a<\/p>\n<pre class=\"brush: python\">\nfor element in nums:\n    print(element)  # \u8f38\u51fa\u6bcf\u500b\u5143\u7d20\n<\/pre>\n<p>\u6216\u8005\u4f7f\u7528 while \u8ff4\u5708\u4f86\u8b80\u53d6\u4e32\u5217\u5143\u7d20\uff1a<\/p>\n<pre class=\"brush: python\">\nindex = 0\nwhile index < len(nums):\n    print(nums[index])  # \u8f38\u51fa\u6bcf\u500b\u5143\u7d20\n    index += 1\n<\/pre>\n<h2>\u4fee\u6539\u4e32\u5217\u5143\u7d20<\/h2>\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528\u7d22\u5f15\uff08[]\uff09\u904b\u7b97\u5b50\u4f86\u4fee\u6539\u4e32\u5217\u67d0\u500b\u4f4d\u7f6e\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff1a<\/p>\n<pre class=\"brush: python\">\nnums[0] = 10  # \u5c07\u7b2c\u4e00\u500b\u5143\u7d20\u4fee\u6539\u70ba 10\nprint(nums)  # \u8f38\u51fa: [10, 2, 3, 4, 5]\n<\/pre>\n<h2>\u65b0\u589e\u4e32\u5217\u5143\u7d20<\/h2>\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528 append() \u51fd\u6578\u4f86\u5728\u4e32\u5217\u672b\u5c3e\u65b0\u589e\u5143\u7d20\u3002\u793a\u4f8b\uff1a<\/p>\n<pre class=\"brush: python\">\nnums.append(6)  # \u5728\u672b\u5c3e\u65b0\u589e 6\nprint(nums)  # \u8f38\u51fa: [10, 2, 3, 4, 5, 6]\n<\/pre>\n<p>\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528 insert() \u51fd\u6578\u4f86\u5728\u4e32\u5217\u4efb\u610f\u4f4d\u7f6e\u65b0\u589e\u5143\u7d20\uff1a<\/p>\n<pre class=\"brush: python\">\nnums.insert(1, 15)  # \u5728\u7d22\u5f15 1 \u8655\u65b0\u589e 15\nprint(nums)  # \u8f38\u51fa: [10, 15, 2, 3, 4, 5, 6]\n<\/pre>\n<h2>\u522a\u9664\u4e32\u5217\u5143\u7d20<\/h2>\n<p>\u8981\u522a\u9664\u4e32\u5217\u4e2d\u7684\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528 del \u904b\u7b97\u5b50\uff1a<\/p>\n<pre class=\"brush: python\">\ndel nums[0]  # \u522a\u9664\u7b2c\u4e00\u500b\u5143\u7d20\nprint(nums)  # \u8f38\u51fa: [15, 2, 3, 4, 5, 6]\n<\/pre>\n<p>\u4f7f\u7528 remove() \u51fd\u6578\u522a\u9664\u4e32\u5217\u4e2d\u7b2c\u4e00\u500b\u51fa\u73fe\u7684\u6307\u5b9a\u5143\u7d20\uff1a<\/p>\n<pre class=\"brush: python\">\nnums.remove(3)  # \u522a\u9664\u5143\u7d20 3\nprint(nums)  # \u8f38\u51fa: [15, 2, 4, 5, 6]\n<\/pre>\n<h2>\u4e32\u5217\u7684\u5e38\u898b\u64cd\u4f5c<\/h2>\n<p>Python \u7684\u4e32\u5217\uff08list\uff09\u662f\u4e00\u7a2e\u5e38\u7528\u7684\u8cc7\u6599\u7d50\u69cb\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b58\u5132\u3001\u8a2a\u554f\u3001\u4fee\u6539\u3001\u589e\u52a0\u3001\u522a\u9664\u5143\u7d20\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u898b\u7684\u64cd\u4f5c\uff1a<\/p>\n<p>- \u4f7f\u7528 <strong>len()<\/strong> \u51fd\u6578\u6c42\u51fa\u4e32\u5217\u7684\u9577\u5ea6\u3002<br \/>\n- \u4f7f\u7528 <strong>count()<\/strong> \u65b9\u6cd5\u8a08\u7b97\u4e32\u5217\u4e2d\u67d0\u500b\u5143\u7d20\u7684\u51fa\u73fe\u6b21\u6578\u3002<br \/>\n- \u4f7f\u7528 <strong>index()<\/strong> \u65b9\u6cd5\u5c0b\u627e\u4e32\u5217\u4e2d\u67d0\u500b\u5143\u7d20\u7684\u4f4d\u7f6e\u3002<br \/>\n- \u4f7f\u7528 <strong>append()<\/strong> \u65b9\u6cd5\u5728\u4e32\u5217\u7684\u672b\u5c3e\u65b0\u589e\u5143\u7d20\u3002<br \/>\n- \u4f7f\u7528 <strong>insert()<\/strong> \u65b9\u6cd5\u5728\u4e32\u5217\u7684\u4efb\u610f\u4f4d\u7f6e\u65b0\u589e\u5143\u7d20\u3002<br \/>\n- \u4f7f\u7528 <strong>remove()<\/strong> \u65b9\u6cd5\u522a\u9664\u4e32\u5217\u4e2d\u67d0\u500b\u5143\u7d20\u3002<br \/>\n- \u4f7f\u7528 <strong>pop()<\/strong> \u65b9\u6cd5\u5f9e\u4e32\u5217\u4e2d\u53d6\u51fa\u5143\u7d20\uff08\u9810\u8a2d\u70ba\u6700\u5f8c\u4e00\u500b\uff09\u3002<br \/>\n- \u4f7f\u7528 <strong>clear()<\/strong> \u65b9\u6cd5\u6e05\u7a7a\u4e32\u5217\u4e2d\u7684\u6240\u6709\u5143\u7d20\u3002<br \/>\n- \u4f7f\u7528 <strong>extend()<\/strong> \u65b9\u6cd5\u5c07\u53e6\u4e00\u500b\u4e32\u5217\u7684\u5143\u7d20\u52a0\u5165\u5230\u4e32\u5217\u4e2d\u3002<br \/>\n- \u4f7f\u7528 <strong>copy()<\/strong> \u65b9\u6cd5\u8907\u88fd\u4e32\u5217\u3002<br \/>\n- \u4f7f\u7528 <strong>+<\/strong> \u904b\u7b97\u5b50\u5c07\u5169\u500b\u4e32\u5217\u5408\u4f75\u3002<br \/>\n- \u4f7f\u7528 <strong>*<\/strong> \u904b\u7b97\u5b50\u91cd\u8907\u4e32\u5217\u3002<br \/>\n- \u4f7f\u7528 in \u548c not in \u904b\u7b97\u5b50\u6aa2\u67e5\u5143\u7d20\u662f\u5426\u5728\u4e32\u5217\u4e2d\u3002<br \/>\n- \u4f7f\u7528 <strong>del<\/strong> \u8a9e\u53e5\u522a\u9664\u4e32\u5217\u4e2d\u7684\u5143\u7d20\u3002<br \/>\n- \u4f7f\u7528 <strong>for<\/strong> \u8ff4\u5708\u904d\u6b77\u4e32\u5217\u4e2d\u7684\u5143\u7d20\u3002<br \/>\n- \u4f7f\u7528 <strong>enumerate()<\/strong> \u51fd\u6578\u5728\u8ff4\u5708\u4e2d\u540c\u6642\u53d6\u5f97\u5143\u7d20\u7684\u7d22\u5f15\u548c\u503c\u3002<br \/>\n- \u4f7f\u7528 <strong>range()<\/strong> \u51fd\u6578\u751f\u6210\u6578\u503c\u4e32\u5217\u3002<br \/>\n- \u4f7f\u7528 <strong>zip()<\/strong> \u51fd\u6578\u5c07\u591a\u500b\u4e32\u5217\u5408\u4f75\u6210\u4e00\u500b\u4e32\u5217\u3002<br \/>\n- \u4f7f\u7528 <strong>map()<\/strong> \u51fd\u6578\u5c0d\u4e32\u5217\u4e2d\u7684\u6bcf\u500b\u5143\u7d20\u57f7\u884c\u6307\u5b9a\u7684\u51fd\u6578\u3002<br \/>\n- \u4f7f\u7528 <strong>filter()<\/strong> \u51fd\u6578\u904e\u6ffe\u4e32\u5217\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<p>\u9019\u4e9b\u64cd\u4f5c\u5c07\u5e6b\u52a9\u4f60\u66f4\u6709\u6548\u5730\u5229\u7528 Python \u4e32\u5217\uff0c\u70ba\u4f60\u7684\u7a0b\u5f0f\u8a2d\u8a08\u589e\u6dfb\u9748\u6d3b\u6027\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528\u793a\u4f8b<\/h2>\n<p>\u5728\u65e5\u5e38\u7684 Python \u958b\u767c\u4e2d\uff0c\u4e32\u5217\u5e38\u88ab\u61c9\u7528\u65bc\u6578\u64da\u8655\u7406\u3001API \u6578\u64da\u89e3\u6790\u7b49\u5834\u666f\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528\u4e32\u5217\u7684\u793a\u4f8b\u4f86\u8a08\u7b97\u5e73\u65b9\u6578\uff1a<\/p>\n<pre class=\"brush: python\">\nnumbers = [1, 2, 3, 4, 5]\nsquares = [number ** 2 for number in numbers]  # \u4f7f\u7528\u5217\u8868\u751f\u6210\u5f0f\u8a08\u7b97\u5e73\u65b9\nprint(squares)  # \u8f38\u51fa: [1, 4, 9, 16, 25]\n<\/pre>\n<p>\u5982\u679c\u4f60\u5728\u5b78\u7fd2 Python \u4e32\u5217\u6642\u9047\u5230\u554f\u984c\uff0c\u53ef\u4ee5\u53c3\u8003 [\u9019\u7bc7\u6559\u5b78\u6587\u7ae0](https:\/\/vocus.cc\/article\/60d5ee4f4c5c41001cfb3a55) \u7372\u53d6\u66f4\u591a\u5e6b\u52a9\u3002<\/p>\n<h2>\u7d50\u8a9e<\/h2>\n<p>\u638c\u63e1 Python \u4e32\u5217\u7684\u64cd\u4f5c\u5c07\u4f7f\u4f60\u5728\u7a0b\u5f0f\u8a2d\u8a08\u7684\u8def\u4e0a\u5982\u864e\u6dfb\u7ffc\u3002\u5e0c\u671b\u9019\u7bc7\u6587\u7ae0\u80fd\u5920\u5e6b\u52a9\u4f60\u66f4\u6df1\u5165\u5730\u7406\u89e3 Python \u4e32\u5217\u7684\u904b\u4f5c\u65b9\u5f0f\u3002<\/p>\n<h2>Q&A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>Q1: Python \u4e32\u5217\u8207\u5143\u7d44\u6709\u4ec0\u9ebc\u4e0d\u540c\uff1f<\/h3>\n<p>A1: \u4e32\u5217\u662f\u53ef\u8b8a\u7684\u8cc7\u6599\u7d50\u69cb\uff0c\u53ef\u4ee5\u96a8\u6642\u4fee\u6539\u5167\u5bb9\uff0c\u800c\u5143\u7d44\u662f\u4e0d\u53ef\u8b8a\u7684\uff0c\u4e00\u65e6\u5275\u5efa\u5f8c\u7121\u6cd5\u4fee\u6539\u3002<\/p>\n<h3>Q2: \u5982\u4f55\u5feb\u901f\u6e05\u7a7a\u4e00\u500b\u4e32\u5217\uff1f<\/h3>\n<p>A2: \u53ef\u4ee5\u4f7f\u7528 <code>clear()<\/code> \u65b9\u6cd5\u6216\u91cd\u8a2d\u70ba\u7a7a\u4e32\u5217 <code>my_list = []<\/code> \u4f86\u6e05\u7a7a\u4e32\u5217\u3002<\/p>\n<h3>Q3: \u5982\u4f55\u5f9e\u4e32\u5217\u4e2d\u96a8\u6a5f\u9078\u64c7\u4e00\u500b\u5143\u7d20\uff1f<\/h3>\n<p>A3: \u53ef\u4ee5\u4f7f\u7528 <code>random.choice()<\/code> \u51fd\u6578\u4f86\u96a8\u6a5f\u9078\u64c7\u4e32\u5217\u4e2d\u7684\u4e00\u500b\u5143\u7d20\u3002\u8a18\u5f97\u5148\u532f\u5165 <code>random<\/code> \u6a21\u7d44\u3002<br \/>\n---<\/p>\n<p>\u5e0c\u671b\u9019\u6a23\u7684\u5167\u5bb9\u512a\u5316\u7b26\u5408\u60a8\u7684\u9700\u6c42\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python \u4e32\u5217\uff1a\u638c\u63e1\u57fa\u790e\u64cd\u4f5c\uff0c\u9032\u884c 25 \u9805\u5e38&#8230;<\/p>\n","protected":false},"author":1,"featured_media":2518,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[18],"tags":[],"class_list":["post-2498","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2022\/12\/DALL\u00b7E-2022-12-28-14.25.55-\u62f7\u8c9d2.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-Ei","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2498","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=2498"}],"version-history":[{"count":7,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2498\/revisions"}],"predecessor-version":[{"id":13069,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2498\/revisions\/13069"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/2518"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=2498"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=2498"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=2498"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}