{"id":953839,"date":"2024-12-27T01:48:37","date_gmt":"2024-12-26T17:48:37","guid":{"rendered":""},"modified":"2024-12-27T01:48:39","modified_gmt":"2024-12-26T17:48:39","slug":"python%e5%a6%82%e4%bd%95%e5%a3%b0%e6%98%8e%e5%a4%9a%e4%b8%aa%e5%88%97%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/953839.html","title":{"rendered":"python\u5982\u4f55\u58f0\u660e\u591a\u4e2a\u5217\u8868"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25094219\/27195307-5221-4349-9d64-288ee42b03ff.webp\" alt=\"python\u5982\u4f55\u58f0\u660e\u591a\u4e2a\u5217\u8868\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u58f0\u660e\u591a\u4e2a\u5217\u8868\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5982\u4f7f\u7528\u9017\u53f7\u5206\u9694\u3001\u5217\u8868\u89e3\u6790\u3001\u5b57\u5178\u7b49\u3002\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u662f\u901a\u8fc7\u9017\u53f7\u5206\u9694\u8fdb\u884c\u58f0\u660e<\/strong>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u793a\u4f8b\u4ee3\u7801\u548c\u5176\u4ed6\u76f8\u5173\u6280\u672f\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u9017\u53f7\u5206\u9694\u58f0\u660e\u591a\u4e2a\u5217\u8868<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5728\u4e00\u884c\u4e2d\u4f7f\u7528\u9017\u53f7\u5206\u9694\u58f0\u660e\u591a\u4e2a\u5217\u8868\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u660e\u4e86\uff0c\u9002\u7528\u4e8e\u9700\u8981\u540c\u65f6\u521d\u59cb\u5316\u591a\u4e2a\u5217\u8868\u7684\u573a\u666f\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list1, list2, list3 = [], [], []<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u540c\u65f6\u58f0\u660e\u4e86\u4e09\u4e2a\u7a7a\u5217\u8868 <code>list1<\/code>\u3001<code>list2<\/code> \u548c <code>list3<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u9002\u5408\u5728\u9700\u8981\u540c\u65f6\u521b\u5efa\u591a\u4e2a\u72ec\u7acb\u7684\u5217\u8868\u65f6\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u58f0\u660e\u4e09\u4e2a\u7a7a\u5217\u8868<\/p>\n<p>list1, list2, list3 = [], [], []<\/p>\n<h2><strong>\u5411list1\u6dfb\u52a0\u5143\u7d20<\/strong><\/h2>\n<p>list1.append(&#39;apple&#39;)<\/p>\n<p>list1.append(&#39;banana&#39;)<\/p>\n<h2><strong>\u5411list2\u6dfb\u52a0\u5143\u7d20<\/strong><\/h2>\n<p>list2.append(1)<\/p>\n<p>list2.append(2)<\/p>\n<h2><strong>\u5411list3\u6dfb\u52a0\u5143\u7d20<\/strong><\/h2>\n<p>list3.append(True)<\/p>\n<p>list3.append(False)<\/p>\n<p>print(&quot;List 1:&quot;, list1)<\/p>\n<p>print(&quot;List 2:&quot;, list2)<\/p>\n<p>print(&quot;List 3:&quot;, list3)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790\u58f0\u660e\u591a\u4e2a\u5217\u8868<\/p>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u5c24\u5176\u5728\u9700\u8981\u57fa\u4e8e\u67d0\u79cd\u89c4\u5219\u751f\u6210\u591a\u4e2a\u5217\u8868\u65f6\u975e\u5e38\u5b9e\u7528\u3002\u901a\u8fc7\u5217\u8868\u89e3\u6790\uff0c\u53ef\u4ee5\u5728\u4e00\u884c\u4ee3\u7801\u4e2d\u521d\u59cb\u5316\u591a\u4e2a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u521b\u5efa\u591a\u4e2a\u5217\u8868\uff0c\u6bcf\u4e2a\u5217\u8868\u5305\u542b\u4e00\u7cfb\u5217\u6570\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">lists = [[i for i in range(5)] for _ in range(3)]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u751f\u6210\u4e09\u4e2a\u5217\u8868\uff0c\u6bcf\u4e2a\u5217\u8868\u5305\u542b\u4ece0\u52304\u7684\u6574\u6570\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u5217\u8868\u89e3\u6790\u751f\u6210\u4e09\u4e2a\u5217\u8868<\/p>\n<p>lists = [[i for i in range(5)] for _ in range(3)]<\/p>\n<p>for index, lst in enumerate(lists):<\/p>\n<p>    print(f&quot;List {index + 1}: {lst}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u5b57\u5178\u7ba1\u7406\u591a\u4e2a\u5217\u8868<\/p>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u7ba1\u7406\u591a\u4e2a\u5217\u8868\u5e76\u4e14\u5e0c\u671b\u80fd\u591f\u901a\u8fc7\u952e\u503c\u5bf9\u65b9\u5f0f\u8bbf\u95ee\u5b83\u4eec\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u3002\u5b57\u5178\u4e0d\u4ec5\u53ef\u4ee5\u66f4\u597d\u5730\u7ba1\u7406\u591a\u4e2a\u5217\u8868\uff0c\u8fd8\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">lists = {<\/p>\n<p>    &#39;fruits&#39;: [&#39;apple&#39;, &#39;banana&#39;],<\/p>\n<p>    &#39;numbers&#39;: [1, 2, 3],<\/p>\n<p>    &#39;booleans&#39;: [True, False]<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u5b57\u5178\u7ba1\u7406\u591a\u4e2a\u5217\u8868<\/p>\n<p>lists = {<\/p>\n<p>    &#39;fruits&#39;: [&#39;apple&#39;, &#39;banana&#39;],<\/p>\n<p>    &#39;numbers&#39;: [1, 2, 3],<\/p>\n<p>    &#39;booleans&#39;: [True, False]<\/p>\n<p>}<\/p>\n<h2><strong>\u8bbf\u95ee\u5b57\u5178\u4e2d\u7684\u5217\u8868<\/strong><\/h2>\n<p>print(&quot;Fruits:&quot;, lists[&#39;fruits&#39;])<\/p>\n<p>print(&quot;Numbers:&quot;, lists[&#39;numbers&#39;])<\/p>\n<p>print(&quot;Booleans:&quot;, lists[&#39;booleans&#39;])<\/p>\n<h2><strong>\u5411\u5b57\u5178\u4e2d\u7684\u5217\u8868\u6dfb\u52a0\u5143\u7d20<\/strong><\/h2>\n<p>lists[&#39;fruits&#39;].append(&#39;cherry&#39;)<\/p>\n<p>lists[&#39;numbers&#39;].append(4)<\/p>\n<p>lists[&#39;booleans&#39;].append(None)<\/p>\n<p>print(&quot;Updated Fruits:&quot;, lists[&#39;fruits&#39;])<\/p>\n<p>print(&quot;Updated Numbers:&quot;, lists[&#39;numbers&#39;])<\/p>\n<p>print(&quot;Updated Booleans:&quot;, lists[&#39;booleans&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528\u7c7b\u5c01\u88c5\u591a\u4e2a\u5217\u8868<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5c06\u591a\u4e2a\u5217\u8868\u5c01\u88c5\u5728\u4e00\u4e2a\u7c7b\u4e2d\u53ef\u4ee5\u63d0\u4f9b\u66f4\u597d\u7684\u7ed3\u6784\u548c\u529f\u80fd\u3002\u901a\u8fc7\u7c7b\u53ef\u4ee5\u5b9a\u4e49\u65b9\u6cd5\u6765\u64cd\u4f5c\u8fd9\u4e9b\u5217\u8868\uff0c\u4ece\u800c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u6269\u5c55\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MultiList:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.fruits = []<\/p>\n<p>        self.numbers = []<\/p>\n<p>        self.booleans = []<\/p>\n<p>    def add_fruit(self, fruit):<\/p>\n<p>        self.fruits.append(fruit)<\/p>\n<p>    def add_number(self, number):<\/p>\n<p>        self.numbers.append(number)<\/p>\n<p>    def add_boolean(self, boolean):<\/p>\n<p>        self.booleans.append(boolean)<\/p>\n<p>    def display_lists(self):<\/p>\n<p>        print(&quot;Fruits:&quot;, self.fruits)<\/p>\n<p>        print(&quot;Numbers:&quot;, self.numbers)<\/p>\n<p>        print(&quot;Booleans:&quot;, self.booleans)<\/p>\n<h2><strong>\u4f7f\u7528\u7c7b\u7ba1\u7406\u591a\u4e2a\u5217\u8868<\/strong><\/h2>\n<p>multi_list = MultiList()<\/p>\n<p>multi_list.add_fruit(&#39;apple&#39;)<\/p>\n<p>multi_list.add_fruit(&#39;banana&#39;)<\/p>\n<p>multi_list.add_number(1)<\/p>\n<p>multi_list.add_number(2)<\/p>\n<p>multi_list.add_boolean(True)<\/p>\n<p>multi_list.add_boolean(False)<\/p>\n<p>multi_list.display_lists()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u4f7f\u7528NumPy\u6570\u7ec4\u58f0\u660e\u591a\u4e2a\u5217\u8868<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u9700\u8981\u5904\u7406\u5927\u91cf\u6570\u503c\u6570\u636e\u7684\u60c5\u51b5\uff0c\u4f7f\u7528NumPy\u6570\u7ec4\u53ef\u80fd\u6bd4\u4f7f\u7528\u5217\u8868\u66f4\u9ad8\u6548\u3002NumPy\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u7ec4\u64cd\u4f5c\u529f\u80fd\uff0c\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u548c\u64cd\u4f5c\u591a\u4e2a\u6570\u503c\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u4f7f\u7528NumPy\u521b\u5efa\u591a\u4e2a\u6570\u7ec4<\/strong><\/h2>\n<p>array1 = np.array([1, 2, 3])<\/p>\n<p>array2 = np.array([4, 5, 6])<\/p>\n<p>array3 = np.array([7, 8, 9])<\/p>\n<p>print(&quot;Array 1:&quot;, array1)<\/p>\n<p>print(&quot;Array 2:&quot;, array2)<\/p>\n<p>print(&quot;Array 3:&quot;, array3)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u79cd\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u5728Python\u4e2d\u58f0\u660e\u591a\u4e2a\u5217\u8868\u3002\u6839\u636e\u5177\u4f53\u60c5\u51b5\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u4e0d\u4ec5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u6548\u7387\u548c\u53ef\u8bfb\u6027\uff0c\u8fd8\u53ef\u4ee5\u589e\u5f3a\u4ee3\u7801\u7684\u7075\u6d3b\u6027\u548c\u53ef\u6269\u5c55\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u540c\u65f6\u58f0\u660e\u591a\u4e2a\u5217\u8868\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u9017\u53f7\u5206\u9694\u6765\u540c\u65f6\u58f0\u660e\u591a\u4e2a\u5217\u8868\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u521b\u5efa\u591a\u4e2a\u5217\u8868\uff1a  <\/p>\n<pre><code class=\"language-python\">list1, list2, list3 = [], [], []\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u60a8\u5c31\u540c\u65f6\u521b\u5efa\u4e86\u4e09\u4e2a\u7a7a\u5217\u8868\u3002\u8fd8\u53ef\u4ee5\u7528\u76f8\u540c\u7684\u65b9\u5f0f\u521d\u59cb\u5316\u5b83\u4eec\uff0c\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">list1, list2, list3 = [1, 2, 3], [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;], [True, False]\n<\/code><\/pre>\n<p><strong>\u5728\u58f0\u660e\u591a\u4e2a\u5217\u8868\u65f6\uff0c\u5982\u4f55\u786e\u4fdd\u5b83\u4eec\u7684\u5185\u5bb9\u4e00\u81f4\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u521b\u5efa\u591a\u4e2a\u5185\u5bb9\u4e00\u81f4\u7684\u5217\u8868\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a  <\/p>\n<pre><code class=\"language-python\">list1 = list2 = list3 = [0] * 5  # \u521b\u5efa\u4e09\u4e2a\u5305\u542b\u4e94\u4e2a\u96f6\u7684\u5217\u8868\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u786e\u4fdd\u6240\u6709\u5217\u8868\u90fd\u6307\u5411\u540c\u4e00\u4e2a\u5bf9\u8c61\uff0c\u6539\u53d8\u4e00\u4e2a\u5217\u8868\u7684\u5185\u5bb9\u4f1a\u5f71\u54cd\u5176\u4ed6\u4e24\u4e2a\u5217\u8868\u3002\u5982\u679c\u9700\u8981\u72ec\u7acb\u7684\u5217\u8868\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\uff1a  <\/p>\n<pre><code class=\"language-python\">list1, list2, list3 = [[0] * 5 for _ in range(3)]  # \u521b\u5efa\u4e09\u4e2a\u72ec\u7acb\u7684\u5217\u8868\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5728\u58f0\u660e\u591a\u4e2a\u5217\u8868\u65f6\u4f7f\u7528\u4e0d\u540c\u7684\u6570\u636e\u7c7b\u578b\uff1f<\/strong><br \/>\u53ef\u4ee5\u5728\u4e00\u884c\u4e2d\u58f0\u660e\u591a\u4e2a\u5217\u8868\uff0c\u6bcf\u4e2a\u5217\u8868\u5305\u542b\u4e0d\u540c\u7684\u6570\u636e\u7c7b\u578b\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">integers = [1, 2, 3]\nstrings = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]\nbooleans = [True, False, True]\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u7075\u6d3b\u5730\u7ba1\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\uff0c\u5e76\u4e14\u5728\u540e\u7eed\u64cd\u4f5c\u4e2d\u5f88\u5bb9\u6613\u8bbf\u95ee\u548c\u4fee\u6539\u8fd9\u4e9b\u5217\u8868\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u58f0\u660e\u591a\u4e2a\u5217\u8868\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5982\u4f7f\u7528\u9017\u53f7\u5206\u9694\u3001\u5217\u8868\u89e3\u6790\u3001\u5b57\u5178\u7b49\u3002\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u662f\u901a\u8fc7\u9017\u53f7\u5206\u9694\u8fdb\u884c\u58f0\u660e [&hellip;]","protected":false},"author":3,"featured_media":953843,"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\/953839"}],"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=953839"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/953839\/revisions"}],"predecessor-version":[{"id":953847,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/953839\/revisions\/953847"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/953843"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=953839"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=953839"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=953839"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}