{"id":993572,"date":"2024-12-27T08:47:10","date_gmt":"2024-12-27T00:47:10","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/993572.html"},"modified":"2024-12-27T08:47:12","modified_gmt":"2024-12-27T00:47:12","slug":"python-%e5%a6%82%e4%bd%95%e6%89%93%e5%8d%b0list%e5%90%8d%e5%ad%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/993572.html","title":{"rendered":"python \u5982\u4f55\u6253\u5370list\u540d\u5b57"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25070918\/d8480012-6059-4f53-99cc-79505ffbe336.webp\" alt=\"python \u5982\u4f55\u6253\u5370list\u540d\u5b57\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u8981\u6253\u5370\u5217\u8868\u7684\u540d\u5b57\uff0c\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u81ea\u5b9a\u4e49\u51fd\u6570\u3001\u4f7f\u7528\u5b57\u5178\u952e\u503c\u5bf9\u3001\u6216\u8005\u4f7f\u7528\u7c7b\u548c\u5bf9\u8c61\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002<\/strong> \u8fd9\u4e9b\u65b9\u6cd5\u90fd\u9700\u8981\u4e0d\u540c\u7a0b\u5ea6\u7684\u7f16\u7a0b\u6280\u5de7\u548c\u903b\u8f91\u601d\u7ef4\u6765\u5b9e\u73b0\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u4e2a\u65b9\u6cd5\uff1a\u4f7f\u7528\u5b57\u5178\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><p>Python\u672c\u8eab\u6ca1\u6709\u63d0\u4f9b\u76f4\u63a5\u83b7\u53d6\u53d8\u91cf\u540d\u7684\u529f\u80fd\uff0c\u56e0\u4e3a\u53d8\u91cf\u540d\u5728\u7f16\u8bd1\u540e\u53ea\u662f\u5185\u5b58\u4e2d\u7684\u4e00\u4e2a\u5f15\u7528\u3002\u7136\u800c\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5c06\u5217\u8868\u4f5c\u4e3a\u5b57\u5178\u7684\u503c\uff0c\u5e76\u5c06\u53d8\u91cf\u540d\u4f5c\u4e3a\u5b57\u5178\u7684\u952e\uff0c\u6765\u95f4\u63a5\u5b9e\u73b0\u6253\u5370\u5217\u8868\u540d\u5b57\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<hr>\n<p><p>\u4e00\u3001\u4f7f\u7528\u5b57\u5178\u952e\u503c\u5bf9<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u5b57\u5178\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u53d8\u91cf\u540d\u4f5c\u4e3a\u952e\uff0c\u5c06\u5217\u8868\u4f5c\u4e3a\u503c\u5b58\u50a8\u5728\u5b57\u5178\u4e2d\u3002\u8fd9\u6837\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u901a\u8fc7\u904d\u5386\u5b57\u5178\u6765\u83b7\u53d6\u5217\u8868\u7684\u540d\u5b57\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efa\u5b57\u5178\u6765\u5b58\u50a8\u53d8\u91cf\u540d\u548c\u5217\u8868<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u5b57\u5178\uff0c\u5176\u4e2d\u952e\u662f\u5217\u8868\u7684\u540d\u5b57\uff08\u4ee5\u5b57\u7b26\u4e32\u5f62\u5f0f\u5b58\u50a8\uff09\uff0c\u800c\u503c\u662f\u5b9e\u9645\u7684\u5217\u8868\u5bf9\u8c61\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u8ba9\u6211\u4eec\u901a\u8fc7\u952e\u6765\u8bbf\u95ee\u548c\u6253\u5370\u5217\u8868\u7684\u540d\u5b57\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_lists = {<\/p>\n<p>    &#39;list1&#39;: [1, 2, 3],<\/p>\n<p>    &#39;list2&#39;: [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6253\u5370\u5b57\u5178\u4e2d\u7684\u952e\u503c\u5bf9<\/strong><br \/>\u4e00\u65e6\u6211\u4eec\u5c06\u5217\u8868\u5b58\u50a8\u5728\u5b57\u5178\u4e2d\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u8f7b\u677e\u5730\u904d\u5386\u5b57\u5178\uff0c\u5e76\u6253\u5370\u51fa\u53d8\u91cf\u540d\u548c\u5176\u5bf9\u5e94\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for name, lst in my_lists.items():<\/p>\n<p>    print(f&quot;List name: {name}, List contents: {lst}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4f1a\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>List name: list1, List contents: [1, 2, 3]<\/p>\n<p>List name: list2, List contents: [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f18\u70b9\u4e0e\u5c40\u9650\u6027<\/strong>  <\/p>\n<\/p>\n<ul>\n<li><strong>\u4f18\u70b9<\/strong>\uff1a\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u76f4\u89c2\uff0c\u6613\u4e8e\u5b9e\u73b0\u548c\u7406\u89e3\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u8f7b\u677e\u7ba1\u7406\u548c\u8bbf\u95ee\u591a\u4e2a\u5217\u8868\u3002<\/li>\n<li><strong>\u5c40\u9650\u6027<\/strong>\uff1a\u9700\u8981\u624b\u52a8\u7ef4\u62a4\u5b57\u5178\uff0c\u5e76\u4e14\u65e0\u6cd5\u81ea\u52a8\u8bc6\u522b\u4ee3\u7801\u4e2d\u7684\u6240\u6709\u5217\u8868\u53d8\u91cf\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<hr>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u7c7b\u548c\u5bf9\u8c61<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7ba1\u7406\u548c\u6253\u5370\u5217\u8868\u7684\u540d\u5b57\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5217\u8868\u5c01\u88c5\u5728\u4e00\u4e2a\u7c7b\u4e2d\u3002\u901a\u8fc7\u7c7b\u7684\u5b9e\u4f8b\uff0c\u53ef\u4ee5\u8f7b\u677e\u8bbf\u95ee\u5217\u8868\u53ca\u5176\u540d\u5b57\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b9a\u4e49\u7c7b\u6765\u5c01\u88c5\u5217\u8868<\/strong><br \/>\u521b\u5efa\u4e00\u4e2a\u7c7b\uff0c\u5176\u4e2d\u5305\u542b\u5217\u8868\u548c\u5176\u540d\u79f0\u4f5c\u4e3a\u5c5e\u6027\u3002\u901a\u8fc7\u7c7b\u7684\u5b9e\u4f8b\uff0c\u53ef\u4ee5\u8f7b\u677e\u7ba1\u7406\u591a\u4e2a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class NamedList:<\/p>\n<p>    def __init__(self, name, lst):<\/p>\n<p>        self.name = name<\/p>\n<p>        self.lst = lst<\/p>\n<p>    def __str__(self):<\/p>\n<p>        return f&quot;List name: {self.name}, List contents: {self.lst}&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u521b\u5efa\u7c7b\u7684\u5b9e\u4f8b\u5e76\u6253\u5370<\/strong><br \/>\u901a\u8fc7\u7c7b\u7684\u5b9e\u4f8b\uff0c\u53ef\u4ee5\u521b\u5efa\u591a\u4e2a\u5217\u8868\u5bf9\u8c61\uff0c\u5e76\u6253\u5370\u5176\u540d\u79f0\u548c\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list1 = NamedList(&#39;list1&#39;, [1, 2, 3])<\/p>\n<p>list2 = NamedList(&#39;list2&#39;, [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;])<\/p>\n<p>print(list1)<\/p>\n<p>print(list2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>List name: list1, List contents: [1, 2, 3]<\/p>\n<p>List name: list2, List contents: [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f18\u70b9\u4e0e\u5c40\u9650\u6027<\/strong>  <\/p>\n<\/p>\n<ul>\n<li><strong>\u4f18\u70b9<\/strong>\uff1a\u901a\u8fc7\u9762\u5411\u5bf9\u8c61\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u7ec4\u7ec7\u548c\u7ba1\u7406\u4ee3\u7801\u3002\u5bf9\u4e8e\u590d\u6742\u7684\u7a0b\u5e8f\uff0c\u8fd9\u79cd\u65b9\u5f0f\u66f4\u5177\u53ef\u6269\u5c55\u6027\u3002<\/li>\n<li><strong>\u5c40\u9650\u6027<\/strong>\uff1a\u9700\u8981\u989d\u5916\u7684\u7c7b\u5b9a\u4e49\uff0c\u53ef\u80fd\u5bf9\u4e8e\u7b80\u5355\u7684\u4efb\u52a1\u663e\u5f97\u8fc7\u4e8e\u590d\u6742\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<hr>\n<p><p>\u4e09\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u867d\u7136\u4e0d\u80fd\u76f4\u63a5\u6253\u5370\u5217\u8868\u7684\u540d\u5b57\uff0c\u4f46\u901a\u8fc7\u4f7f\u7528\u5b57\u5178\u6216\u7c7b\uff0c\u6211\u4eec\u53ef\u4ee5\u95f4\u63a5\u5b9e\u73b0\u8fd9\u4e00\u9700\u6c42\u3002\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u4e2a\u4eba\u7684\u7f16\u7a0b\u4e60\u60ef\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u4f7f\u7528\u5b57\u5178<\/strong>\uff1a\u9002\u5408\u7b80\u5355\u7684\u5e94\u7528\u573a\u666f\uff0c\u6613\u4e8e\u5b9e\u73b0\u3002<\/li>\n<li><strong>\u4f7f\u7528\u7c7b\u548c\u5bf9\u8c61<\/strong>\uff1a\u9002\u5408\u590d\u6742\u7684\u7a0b\u5e8f\u8bbe\u8ba1\uff0c\u5177\u6709\u826f\u597d\u7684\u6269\u5c55\u6027\u548c\u7ec4\u7ec7\u6027\u3002<\/li>\n<\/ul>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u597d\u5730\u7ba1\u7406\u548c\u8bbf\u95ee\u5217\u8868\uff0c\u63d0\u5347\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u80fd\u6709\u6548\u5730\u89e3\u51b3\u95ee\u9898\uff0c\u5b9e\u73b0\u6253\u5370\u5217\u8868\u540d\u5b57\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8f93\u51fa\u5217\u8868\u7684\u540d\u79f0\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u76f4\u63a5\u8f93\u51fa\u5217\u8868\u7684\u540d\u79f0\u5e76\u4e0d\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u64cd\u4f5c\uff0c\u56e0\u4e3a\u53d8\u91cf\u540d\u5728\u8fd0\u884c\u65f6\u5e76\u4e0d\u662f\u5bf9\u8c61\u7684\u4e00\u90e8\u5206\u3002\u901a\u5e38\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u6216\u5176\u4ed6\u7ed3\u6784\u6765\u5173\u8054\u540d\u79f0\u4e0e\u503c\uff0c\u4ee5\u4fbf\u5b9e\u73b0\u7c7b\u4f3c\u7684\u529f\u80fd\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u65b9\u6cd5\u6765\u663e\u793a\u5217\u8868\u7684\u5185\u5bb9\u800c\u4e0d\u662f\u540d\u79f0\uff1f<\/strong><br \/>\u8981\u663e\u793a\u5217\u8868\u7684\u5185\u5bb9\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528<code>print()<\/code>\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u6709\u4e00\u4e2a\u540d\u4e3a<code>my_list<\/code>\u7684\u5217\u8868\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>print(my_list)<\/code>\u6765\u8f93\u51fa\u5217\u8868\u4e2d\u7684\u6240\u6709\u5143\u7d20\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\u4ee5\u4fbf\u4e8e\u663e\u793a\u5217\u8868\u7684\u540d\u79f0\u548c\u5185\u5bb9\uff1f<\/strong><br \/>\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u5c06\u5217\u8868\u540d\u79f0\u548c\u5185\u5bb9\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u5b83\u3002\u901a\u8fc7\u4f7f\u7528\u5b57\u5178\u6216\u5c06\u540d\u79f0\u4f5c\u4e3a\u5b57\u7b26\u4e32\u4f20\u9012\uff0c\u60a8\u53ef\u4ee5\u5728\u51fd\u6570\u4e2d\u6253\u5370\u51fa\u540d\u79f0\u548c\u5185\u5bb9\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">def print_list(name, lst):\n    print(f&quot;List Name: {name}&quot;)\n    print(f&quot;List Content: {lst}&quot;)\n\nmy_list = [1, 2, 3]\nprint_list(&quot;my_list&quot;, my_list)\n<\/code><\/pre>\n<p><strong>\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u83b7\u53d6\u53d8\u91cf\u540d\u79f0\u5417\uff1f<\/strong><br \/>\u867d\u7136Python\u6ca1\u6709\u76f4\u63a5\u83b7\u53d6\u53d8\u91cf\u540d\u79f0\u7684\u529f\u80fd\uff0c\u4f46\u53ef\u4ee5\u4f7f\u7528<code>locals()<\/code>\u6216<code>globals()<\/code>\u51fd\u6570\u67e5\u627e\u5f53\u524d\u4f5c\u7528\u57df\u5185\u7684\u53d8\u91cf\u3002\u9700\u6ce8\u610f\uff0c\u8fd9\u79cd\u65b9\u6cd5\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6df7\u6dc6\uff0c\u56e0\u6b64\u5e94\u8c28\u614e\u4f7f\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u8981\u6253\u5370\u5217\u8868\u7684\u540d\u5b57\uff0c\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u81ea\u5b9a\u4e49\u51fd\u6570\u3001\u4f7f\u7528\u5b57\u5178\u952e\u503c\u5bf9\u3001\u6216\u8005\u4f7f\u7528\u7c7b\u548c\u5bf9\u8c61\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002 \u8fd9 [&hellip;]","protected":false},"author":3,"featured_media":993577,"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\/993572"}],"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=993572"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/993572\/revisions"}],"predecessor-version":[{"id":993580,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/993572\/revisions\/993580"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/993577"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=993572"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=993572"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=993572"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}