{"id":984786,"date":"2024-12-27T07:31:15","date_gmt":"2024-12-26T23:31:15","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/984786.html"},"modified":"2024-12-27T07:31:17","modified_gmt":"2024-12-26T23:31:17","slug":"python%e7%9a%84%e6%95%b0%e7%bb%84%e5%a6%82%e4%bd%95%e6%8f%90%e5%8f%96","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/984786.html","title":{"rendered":"python\u7684\u6570\u7ec4\u5982\u4f55\u63d0\u53d6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24212245\/4e2dc2cd-f56b-40be-8728-142753d5d81e.webp\" alt=\"python\u7684\u6570\u7ec4\u5982\u4f55\u63d0\u53d6\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u63d0\u53d6\u6570\u7ec4\u5143\u7d20\u7684\u5e38\u7528\u65b9\u6cd5\u5305\u62ec\uff1a\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u3001\u4f7f\u7528\u5207\u7247\u3001\u6761\u4ef6\u7b5b\u9009\u3001\u4f7f\u7528NumPy\u5e93\u3002<\/strong>\u5728\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\uff0c<strong>\u7d22\u5f15\u8bbf\u95ee<\/strong>\u662f\u6700\u5e38\u7528\u7684\u4e00\u79cd\u65b9\u5f0f\uff0c\u5141\u8bb8\u5f00\u53d1\u8005\u901a\u8fc7\u5143\u7d20\u7684\u4f4d\u7f6e\u6765\u83b7\u53d6\u5176\u503c\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5217\u8868\u662f\u4e00\u79cd\u5e38\u7528\u7684\u6570\u7ec4\u6570\u636e\u7ed3\u6784\u3002\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u5217\u8868\u4e2d\u7684\u5143\u7d20\u662f\u6700\u57fa\u672c\u7684\u64cd\u4f5c\u4e4b\u4e00\u3002\u7d22\u5f15\u4ece0\u5f00\u59cb\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u8d1f\u6570\u7d22\u5f15\u4ece\u5217\u8868\u672b\u5c3e\u5f00\u59cb\u3002<\/p>\n<\/p>\n<p><h4>1. \u6b63\u5411\u7d22\u5f15<\/h4>\n<\/p>\n<p><p>\u6b63\u5411\u7d22\u5f15\u4ece0\u5f00\u59cb\uff0c\u5230\u5217\u8868\u957f\u5ea6\u51cf1\u7ed3\u675f\u3002\u4f7f\u7528\u6b63\u5411\u7d22\u5f15\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u5217\u8868\u4e2d\u7684\u67d0\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [10, 20, 30, 40, 50]<\/p>\n<p>element = my_list[2]  # \u8bbf\u95ee\u7b2c\u4e09\u4e2a\u5143\u7d20\uff0c\u7ed3\u679c\u4e3a30<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8d1f\u5411\u7d22\u5f15<\/h4>\n<\/p>\n<p><p>\u8d1f\u5411\u7d22\u5f15\u4ece-1\u5f00\u59cb\uff0c\u8868\u793a\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff0c-2\u8868\u793a\u5012\u6570\u7b2c\u4e8c\u4e2a\u5143\u7d20\uff0c\u4ee5\u6b64\u7c7b\u63a8\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">element = my_list[-1]  # \u8bbf\u95ee\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff0c\u7ed3\u679c\u4e3a50<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5207\u7247<\/h3>\n<\/p>\n<p><p>\u5207\u7247\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u83b7\u53d6\u5217\u8868\u4e2d\u7684\u4e00\u4e2a\u5b50\u96c6\u3002\u5207\u7247\u4f7f\u7528\u5192\u53f7\u201c:\u201d\u6765\u5206\u5272\u8d77\u59cb\u548c\u7ed3\u675f\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><h4>1. \u57fa\u672c\u5207\u7247<\/h4>\n<\/p>\n<p><p>\u5207\u7247\u53ef\u4ee5\u7528\u6765\u63d0\u53d6\u5217\u8868\u4e2d\u7684\u4e00\u90e8\u5206\uff0c\u5305\u542b\u8d77\u59cb\u7d22\u5f15\u4f46\u4e0d\u5305\u542b\u7ed3\u675f\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">sub_list = my_list[1:4]  # \u63d0\u53d6\u7b2c\u4e8c\u5230\u7b2c\u56db\u4e2a\u5143\u7d20\uff0c\u7ed3\u679c\u4e3a[20, 30, 40]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u6b65\u957f<\/h4>\n<\/p>\n<p><p>\u5207\u7247\u8fd8\u53ef\u4ee5\u6307\u5b9a\u6b65\u957f\uff0c\u901a\u8fc7\u5728\u5207\u7247\u4e2d\u6dfb\u52a0\u7b2c\u4e09\u4e2a\u53c2\u6570\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">sub_list = my_list[0:5:2]  # \u6bcf\u9694\u4e00\u4e2a\u5143\u7d20\u63d0\u53d6\uff0c\u7ed3\u679c\u4e3a[10, 30, 50]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u6761\u4ef6\u7b5b\u9009<\/h3>\n<\/p>\n<p><p>\u6761\u4ef6\u7b5b\u9009\u53ef\u4ee5\u7528\u4e8e\u6839\u636e\u6761\u4ef6\u63d0\u53d6\u5143\u7d20\uff0c\u901a\u5e38\u7ed3\u5408\u5217\u8868\u63a8\u5bfc\u5f0f\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">filtered_list = [x for x in my_list if x &gt; 25]  # \u63d0\u53d6\u5927\u4e8e25\u7684\u5143\u7d20\uff0c\u7ed3\u679c\u4e3a[30, 40, 50]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528NumPy\u5e93<\/h3>\n<\/p>\n<p><p>NumPy\u662f\u4e00\u4e2a\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u7684Python\u5e93\uff0c\u63d0\u4f9b\u4e86\u5bf9\u6570\u7ec4\u7684\u652f\u6301\u3002NumPy\u6570\u7ec4\u6bd4Python\u5217\u8868\u5177\u6709\u66f4\u9ad8\u7684\u6548\u7387\u548c\u66f4\u591a\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h4>1. \u521b\u5efaNumPy\u6570\u7ec4<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u5b89\u88c5NumPy\u5e93\u5e76\u521b\u5efa\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>np_array = np.array([10, 20, 30, 40, 50])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u7d22\u5f15\u548c\u5207\u7247<\/h4>\n<\/p>\n<p><p>NumPy\u6570\u7ec4\u7684\u7d22\u5f15\u548c\u5207\u7247\u4e0e\u5217\u8868\u7c7b\u4f3c\uff0c\u4f46\u529f\u80fd\u66f4\u5f3a\u5927\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">element = np_array[2]  # \u8bbf\u95ee\u7b2c\u4e09\u4e2a\u5143\u7d20\uff0c\u7ed3\u679c\u4e3a30<\/p>\n<p>sub_array = np_array[1:4]  # \u63d0\u53d6\u7b2c\u4e8c\u5230\u7b2c\u56db\u4e2a\u5143\u7d20\uff0c\u7ed3\u679c\u4e3aarray([20, 30, 40])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u5e03\u5c14\u7d22\u5f15<\/h4>\n<\/p>\n<p><p>NumPy\u652f\u6301\u5e03\u5c14\u7d22\u5f15\uff0c\u53ef\u4ee5\u6839\u636e\u6761\u4ef6\u63d0\u53d6\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">filtered_array = np_array[np_array &gt; 25]  # \u63d0\u53d6\u5927\u4e8e25\u7684\u5143\u7d20\uff0c\u7ed3\u679c\u4e3aarray([30, 40, 50])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u63d0\u53d6\u6570\u7ec4\u5143\u7d20\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u4ece\u7b80\u5355\u7684\u7d22\u5f15\u5230\u590d\u6742\u7684\u6761\u4ef6\u7b5b\u9009\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u7528\u9014\u548c\u4f18\u52bf\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u6570\u7ec4\u64cd\u4f5c\uff0c\u5217\u8868\u63d0\u4f9b\u4e86\u8db3\u591f\u7684\u7075\u6d3b\u6027\u548c\u529f\u80fd\uff1b\u800c\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u79d1\u5b66\u8ba1\u7b97\u4efb\u52a1\uff0cNumPy\u5e93\u5219\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7684\u6027\u80fd\u548c\u66f4\u4e30\u5bcc\u7684\u529f\u80fd\u3002\u6839\u636e\u5177\u4f53\u7684\u9700\u6c42\u548c\u5e94\u7528\u573a\u666f\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6548\u7387\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u63d0\u53d6\u6570\u7ec4\u7684\u7279\u5b9a\u5143\u7d20\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u7d22\u5f15\u6765\u63d0\u53d6\u6570\u7ec4\u4e2d\u7684\u7279\u5b9a\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u6570\u7ec4<code>arr = [10, 20, 30, 40, 50]<\/code>\uff0c\u60f3\u63d0\u53d6\u7b2c\u4e09\u4e2a\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528<code>arr[2]<\/code>\uff0c\u8fd4\u56de\u7684\u7ed3\u679c\u5c06\u662f<code>30<\/code>\u3002\u53e6\u5916\uff0cPython\u652f\u6301\u8d1f\u7d22\u5f15\uff0c<code>arr[-1]<\/code>\u5c06\u8fd4\u56de\u6570\u7ec4\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff0c\u5373<code>50<\/code>\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u8fdb\u884c\u6570\u7ec4\u5207\u7247\uff1f<\/strong><br \/>\u5207\u7247\u662f\u63d0\u53d6\u6570\u7ec4\u4e2d\u4e00\u90e8\u5206\u5143\u7d20\u7684\u6709\u6548\u65b9\u6cd5\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5192\u53f7<code>:<\/code>\u6765\u5b9e\u73b0\u5207\u7247\u3002\u4f8b\u5982\uff0c<code>arr[1:4]<\/code>\u5c06\u63d0\u53d6\u4ece\u7d22\u5f151\u5230\u7d22\u5f153\u7684\u5143\u7d20\uff0c\u8fd4\u56de\u7684\u7ed3\u679c\u662f<code>[20, 30, 40]<\/code>\u3002\u8fd8\u53ef\u4ee5\u7701\u7565\u8d77\u59cb\u6216\u7ed3\u675f\u7d22\u5f15\uff0c<code>arr[:3]<\/code>\u5c06\u8fd4\u56de\u4ece\u5f00\u59cb\u5230\u7d22\u5f152\u7684\u6240\u6709\u5143\u7d20\uff0c\u7ed3\u679c\u4e3a<code>[10, 20, 30]<\/code>\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u6761\u4ef6\u63d0\u53d6\u6570\u7ec4\u5143\u7d20\uff1f<\/strong><br \/>\u5982\u679c\u9700\u8981\u6839\u636e\u7279\u5b9a\u6761\u4ef6\u63d0\u53d6\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3002\u4f8b\u5982\uff0c\u82e5\u60f3\u4ece\u6570\u7ec4\u4e2d\u63d0\u53d6\u6240\u6709\u5076\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u4ee3\u7801\uff1a<code>even_numbers = [x for x in arr if x % 2 == 0]<\/code>\u3002\u8fd9\u5c06\u8fd4\u56de\u4e00\u4e2a\u65b0\u6570\u7ec4\uff0c\u5305\u542b\u6240\u6709\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20\uff0c\u5982<code>[10, 20, 30, 40]<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u7075\u6d3b\u4e14\u9ad8\u6548\uff0c\u9002\u5408\u5904\u7406\u8f83\u5927\u6570\u7ec4\u7684\u6761\u4ef6\u7b5b\u9009\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u63d0\u53d6\u6570\u7ec4\u5143\u7d20\u7684\u5e38\u7528\u65b9\u6cd5\u5305\u62ec\uff1a\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u3001\u4f7f\u7528\u5207\u7247\u3001\u6761\u4ef6\u7b5b\u9009\u3001\u4f7f\u7528NumPy\u5e93\u3002\u5728\u8fd9\u4e9b\u65b9\u6cd5\u4e2d [&hellip;]","protected":false},"author":3,"featured_media":984789,"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\/984786"}],"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=984786"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/984786\/revisions"}],"predecessor-version":[{"id":984792,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/984786\/revisions\/984792"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/984789"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=984786"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=984786"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=984786"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}