{"id":1078578,"date":"2025-01-08T12:12:54","date_gmt":"2025-01-08T04:12:54","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1078578.html"},"modified":"2025-01-08T12:12:57","modified_gmt":"2025-01-08T04:12:57","slug":"python%e5%a6%82%e4%bd%95%e5%ae%9a%e4%b9%89%e7%bb%93%e6%9e%84%e4%bd%93%e6%95%b0%e7%bb%84%e7%9a%84%e9%95%bf%e5%ba%a6-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1078578.html","title":{"rendered":"python\u5982\u4f55\u5b9a\u4e49\u7ed3\u6784\u4f53\u6570\u7ec4\u7684\u957f\u5ea6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24182052\/4b4a9882-f97d-4804-8185-557bcac6a0fb.webp\" alt=\"python\u5982\u4f55\u5b9a\u4e49\u7ed3\u6784\u4f53\u6570\u7ec4\u7684\u957f\u5ea6\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u5b9a\u4e49\u7ed3\u6784\u4f53\u6570\u7ec4\u7684\u957f\u5ea6\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u7c7b\u548c\u5217\u8868\u76f8\u7ed3\u5408\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002<\/strong> Python\u4e0d\u50cfC\u8bed\u8a00\u90a3\u6837\u6709\u76f4\u63a5\u7684\u7ed3\u6784\u4f53\u6982\u5ff5\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u7c7b\u6765\u6a21\u62df\u7ed3\u6784\u4f53\u7684\u884c\u4e3a\uff0c\u7136\u540e\u4f7f\u7528\u5217\u8868\u6765\u521b\u5efa\u6570\u7ec4\uff0c\u5e76\u901a\u8fc7\u5217\u8868\u7684\u957f\u5ea6\u5c5e\u6027\u6765\u63a7\u5236\u6570\u7ec4\u7684\u957f\u5ea6\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Python\u4e2d\u5b9a\u4e49\u7ed3\u6784\u4f53\u6570\u7ec4\u7684\u957f\u5ea6\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b9a\u4e49\u4e00\u4e2a\u7c7b\u6765\u6a21\u62df\u7ed3\u6784\u4f53\u7684\u884c\u4e3a\uff0c\u7136\u540e\u521b\u5efa\u4e00\u4e2a\u5217\u8868\u4f5c\u4e3a\u6570\u7ec4\uff0c\u5e76\u8bbe\u5b9a\u5176\u957f\u5ea6\u3002\u8fd9\u6837\u53ef\u4ee5\u7075\u6d3b\u5730\u63a7\u5236\u6570\u7ec4\u7684\u957f\u5ea6\uff0c\u540c\u65f6\u4fdd\u6301\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6613\u4e8e\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b9a\u4e49\u7ed3\u6784\u4f53\u7c7b<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e2a\u7c7b\u6765\u6a21\u62df\u7ed3\u6784\u4f53\u7684\u884c\u4e3a\u3002\u7c7b\u7684\u5c5e\u6027\u76f8\u5f53\u4e8e\u7ed3\u6784\u4f53\u7684\u6210\u5458\u53d8\u91cf\uff0c\u7c7b\u7684\u5b9e\u4f8b\u76f8\u5f53\u4e8e\u7ed3\u6784\u4f53\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyStruct:<\/p>\n<p>    def __init__(self, field1, field2):<\/p>\n<p>        self.field1 = field1<\/p>\n<p>        self.field2 = field2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>MyStruct<\/code>\u7c7b\u6709\u4e24\u4e2a\u6210\u5458\u53d8\u91cf<code>field1<\/code>\u548c<code>field2<\/code>\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u6dfb\u52a0\u66f4\u591a\u7684\u6210\u5458\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u521b\u5efa\u7ed3\u6784\u4f53\u6570\u7ec4<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u5217\u8868\u6765\u521b\u5efa\u7ed3\u6784\u4f53\u6570\u7ec4\uff0c\u5e76\u8bbe\u5b9a\u5176\u957f\u5ea6\u3002\u53ef\u4ee5\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u521d\u59cb\u5316\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">array_length = 10<\/p>\n<p>my_struct_array = [MyStruct(0, 0) for _ in range(array_length)]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>array_length<\/code>\u5b9a\u4e49\u4e86\u6570\u7ec4\u7684\u957f\u5ea6\uff0c\u7136\u540e\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u521b\u5efa\u4e00\u4e2a\u5305\u542b<code>array_length<\/code>\u4e2a<code>MyStruct<\/code>\u5b9e\u4f8b\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u64cd\u4f5c\u7ed3\u6784\u4f53\u6570\u7ec4<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u548c\u4fee\u6539\u7ed3\u6784\u4f53\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bbf\u95ee\u7b2c\u4e00\u4e2a\u7ed3\u6784\u4f53\u5143\u7d20<\/p>\n<p>struct_instance = my_struct_array[0]<\/p>\n<p>print(struct_instance.field1, struct_instance.field2)<\/p>\n<h2><strong>\u4fee\u6539\u7b2c\u4e00\u4e2a\u7ed3\u6784\u4f53\u5143\u7d20<\/strong><\/h2>\n<p>my_struct_array[0].field1 = 1<\/p>\n<p>my_struct_array[0].field2 = 2<\/p>\n<p>print(my_struct_array[0].field1, my_struct_array[0].field2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u7d22\u5f15\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bbf\u95ee\u548c\u4fee\u6539\u7ed3\u6784\u4f53\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u52a8\u6001\u8c03\u6574\u6570\u7ec4\u957f\u5ea6<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u5217\u8868\u7684append\u3001extend\u7b49\u65b9\u6cd5\uff0c\u53ef\u4ee5\u52a8\u6001\u8c03\u6574\u7ed3\u6784\u4f53\u6570\u7ec4\u7684\u957f\u5ea6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u7ed3\u6784\u4f53\u5143\u7d20<\/p>\n<p>my_struct_array.append(MyStruct(3, 4))<\/p>\n<h2><strong>\u6269\u5c55\u7ed3\u6784\u4f53\u6570\u7ec4<\/strong><\/h2>\n<p>additional_structs = [MyStruct(5, 6), MyStruct(7, 8)]<\/p>\n<p>my_struct_array.extend(additional_structs)<\/p>\n<h2><strong>\u6253\u5370\u6570\u7ec4\u957f\u5ea6<\/strong><\/h2>\n<p>print(&quot;\u6570\u7ec4\u957f\u5ea6:&quot;, len(my_struct_array))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u8c03\u6574\u7ed3\u6784\u4f53\u6570\u7ec4\u7684\u957f\u5ea6\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5b9a\u4e49\u7ed3\u6784\u4f53\u6570\u7ec4\u7684\u957f\u5ea6\u4ee5\u53ca\u5982\u4f55\u64cd\u4f5c\u7ed3\u6784\u4f53\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyStruct:<\/p>\n<p>    def __init__(self, field1, field2):<\/p>\n<p>        self.field1 = field1<\/p>\n<p>        self.field2 = field2<\/p>\n<h2><strong>\u5b9a\u4e49\u7ed3\u6784\u4f53\u6570\u7ec4\u7684\u957f\u5ea6<\/strong><\/h2>\n<p>array_length = 10<\/p>\n<p>my_struct_array = [MyStruct(0, 0) for _ in range(array_length)]<\/p>\n<h2><strong>\u8bbf\u95ee\u548c\u4fee\u6539\u7ed3\u6784\u4f53\u6570\u7ec4\u4e2d\u7684\u5143\u7d20<\/strong><\/h2>\n<p>print(&quot;\u521d\u59cb\u6570\u7ec4:&quot;)<\/p>\n<p>for i, struct in enumerate(my_struct_array):<\/p>\n<p>    print(f&quot;\u7ed3\u6784\u4f53{i}: field1={struct.field1}, field2={struct.field2}&quot;)<\/p>\n<h2><strong>\u4fee\u6539\u7b2c\u4e00\u4e2a\u7ed3\u6784\u4f53\u5143\u7d20<\/strong><\/h2>\n<p>my_struct_array[0].field1 = 1<\/p>\n<p>my_struct_array[0].field2 = 2<\/p>\n<p>print(&quot;\\n\u4fee\u6539\u540e\u7684\u6570\u7ec4:&quot;)<\/p>\n<p>for i, struct in enumerate(my_struct_array):<\/p>\n<p>    print(f&quot;\u7ed3\u6784\u4f53{i}: field1={struct.field1}, field2={struct.field2}&quot;)<\/p>\n<h2><strong>\u52a8\u6001\u8c03\u6574\u6570\u7ec4\u957f\u5ea6<\/strong><\/h2>\n<p>my_struct_array.append(MyStruct(3, 4))<\/p>\n<p>additional_structs = [MyStruct(5, 6), MyStruct(7, 8)]<\/p>\n<p>my_struct_array.extend(additional_structs)<\/p>\n<p>print(&quot;\\n\u6269\u5c55\u540e\u7684\u6570\u7ec4:&quot;)<\/p>\n<p>for i, struct in enumerate(my_struct_array):<\/p>\n<p>    print(f&quot;\u7ed3\u6784\u4f53{i}: field1={struct.field1}, field2={struct.field2}&quot;)<\/p>\n<p>print(&quot;\\n\u6570\u7ec4\u957f\u5ea6:&quot;, len(my_struct_array))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u901a\u8fc7\u5b9a\u4e49\u7c7b\u6765\u6a21\u62df\u7ed3\u6784\u4f53\u7684\u884c\u4e3a\uff0c\u5e76\u4f7f\u7528\u5217\u8868\u6765\u521b\u5efa\u548c\u64cd\u4f5c\u7ed3\u6784\u4f53\u6570\u7ec4\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u63a7\u5236\u6570\u7ec4\u7684\u957f\u5ea6\u3002\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9a\u4e49\u3001\u8bbf\u95ee\u3001\u4fee\u6539\u548c\u52a8\u6001\u8c03\u6574\u7ed3\u6784\u4f53\u6570\u7ec4\u7684\u957f\u5ea6\uff0c\u4ece\u800c\u5b9e\u73b0\u4e0eC\u8bed\u8a00\u4e2d\u7ed3\u6784\u4f53\u6570\u7ec4\u7c7b\u4f3c\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5b9a\u4e49\u7ed3\u6784\u4f53\u6570\u7ec4\u7684\u957f\u5ea6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u7ed3\u6784\u4f53\u901a\u5e38\u901a\u8fc7<code>struct<\/code>\u6a21\u5757\u6216\u7c7b\u7684\u65b9\u5f0f\u5b9a\u4e49\u3002\u5982\u679c\u4f7f\u7528\u7c7b\u5b9a\u4e49\u7ed3\u6784\u4f53\u6570\u7ec4\uff0c\u53ef\u4ee5\u901a\u8fc7\u5217\u8868\u6765\u5b9e\u73b0\uff0c\u5217\u8868\u7684\u957f\u5ea6\u662f\u52a8\u6001\u7684\uff0c\u4e0d\u9700\u8981\u4e8b\u5148\u5b9a\u4e49\u3002\u5982\u679c\u9700\u8981\u56fa\u5b9a\u957f\u5ea6\u7684\u7ed3\u6784\u4f53\u6570\u7ec4\uff0c\u53ef\u4ee5\u5728\u521b\u5efa\u65f6\u6307\u5b9a\u957f\u5ea6\uff0c\u6216\u8005\u4f7f\u7528NumPy\u5e93\u6765\u521b\u5efa\u5177\u6709\u56fa\u5b9a\u957f\u5ea6\u7684\u6570\u7ec4\u3002<\/p>\n<p><strong>Python\u4e2d\u7684\u7ed3\u6784\u4f53\u6570\u7ec4\u53ef\u4ee5\u5b58\u50a8\u54ea\u4e9b\u7c7b\u578b\u7684\u6570\u636e\uff1f<\/strong><br \/>\u7ed3\u6784\u4f53\u6570\u7ec4\u53ef\u4ee5\u5b58\u50a8\u591a\u79cd\u7c7b\u578b\u7684\u6570\u636e\uff0c\u5305\u62ec\u6574\u578b\u3001\u6d6e\u70b9\u578b\u3001\u5b57\u7b26\u4e32\u7b49\u3002\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e2a\u7c7b\u6765\u8868\u793a\u7ed3\u6784\u4f53\uff0c\u6bcf\u4e2a\u5b9e\u4f8b\u53ef\u4ee5\u5305\u542b\u4e0d\u540c\u7c7b\u578b\u7684\u5c5e\u6027\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u8868\u793a\u5b66\u751f\u7684\u7ed3\u6784\u4f53\uff0c\u5305\u542b\u59d3\u540d\u3001\u5e74\u9f84\u548c\u6210\u7ee9\u7b49\u5c5e\u6027\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u4fee\u6539\u7ed3\u6784\u4f53\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\uff1f<\/strong><br \/>\u4fee\u6539\u7ed3\u6784\u4f53\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u5217\u8868\u4e2d\u7684\u7279\u5b9a\u5143\u7d20\u3002\u82e5\u4f7f\u7528\u7c7b\u5b9a\u4e49\u7ed3\u6784\u4f53\uff0c\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7\u5c5e\u6027\u540d\u6765\u4fee\u6539\u3002\u4f8b\u5982\uff0c\u82e5\u6709\u4e00\u4e2a\u5b66\u751f\u7c7b\u7684\u5b9e\u4f8b\u5b58\u50a8\u5728\u5217\u8868\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>students[index].name = &quot;\u65b0\u540d\u5b57&quot;<\/code>\u7684\u65b9\u5f0f\u76f4\u63a5\u4fee\u6539\u7279\u5b9a\u5b66\u751f\u7684\u59d3\u540d\u3002<\/p>\n<p><strong>\u4f7f\u7528NumPy\u5b9a\u4e49\u7ed3\u6784\u4f53\u6570\u7ec4\u6709\u4ec0\u4e48\u4f18\u52bf\uff1f<\/strong><br \/>\u4f7f\u7528NumPy\u5b9a\u4e49\u7ed3\u6784\u4f53\u6570\u7ec4\u53ef\u4ee5\u63d0\u9ad8\u6027\u80fd\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\u3002NumPy\u63d0\u4f9b\u4e86\u591a\u7ef4\u6570\u7ec4\u5bf9\u8c61\u548c\u5927\u91cf\u7684\u6570\u5b66\u51fd\u6570\uff0c\u80fd\u591f\u9ad8\u6548\u5730\u8fdb\u884c\u6570\u7ec4\u64cd\u4f5c\u548c\u8ba1\u7b97\u3002\u540c\u65f6\uff0cNumPy\u7684\u6570\u7ec4\u5177\u6709\u56fa\u5b9a\u5927\u5c0f\uff0c\u8fd9\u6709\u52a9\u4e8e\u8282\u7701\u5185\u5b58\u7a7a\u95f4\u5e76\u52a0\u5feb\u6570\u636e\u5904\u7406\u901f\u5ea6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5b9a\u4e49\u7ed3\u6784\u4f53\u6570\u7ec4\u7684\u957f\u5ea6\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u7c7b\u548c\u5217\u8868\u76f8\u7ed3\u5408\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002 Python\u4e0d\u50cfC\u8bed\u8a00\u90a3\u6837\u6709\u76f4 [&hellip;]","protected":false},"author":3,"featured_media":1078591,"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\/1078578"}],"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=1078578"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1078578\/revisions"}],"predecessor-version":[{"id":1078592,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1078578\/revisions\/1078592"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1078591"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1078578"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1078578"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1078578"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}