{"id":991389,"date":"2024-12-27T08:28:09","date_gmt":"2024-12-27T00:28:09","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/991389.html"},"modified":"2024-12-27T08:28:12","modified_gmt":"2024-12-27T00:28:12","slug":"python%e5%a6%82%e4%bd%95%e8%b5%8b%e5%80%bc%e7%bb%99%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/991389.html","title":{"rendered":"python\u5982\u4f55\u8d4b\u503c\u7ed9\u6570\u7ec4"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25065707\/c6dc3391-652d-44b1-bd13-55aee8b991a1.webp\" alt=\"python\u5982\u4f55\u8d4b\u503c\u7ed9\u6570\u7ec4\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u8d4b\u503c\u7ed9\u6570\u7ec4\u53ef\u4ee5\u901a\u8fc7\u76f4\u63a5\u6307\u5b9a\u7d22\u5f15\u3001\u4f7f\u7528\u5207\u7247\u3001\u5229\u7528\u5faa\u73af\u6216\u5217\u8868\u89e3\u6790\u3001\u4ee5\u53ca\u4f7f\u7528NumPy\u5e93\u7b49\u65b9\u6cd5\u5b9e\u73b0<\/strong>\u3002\u901a\u8fc7\u76f4\u63a5\u6307\u5b9a\u7d22\u5f15\uff0c\u53ef\u4ee5\u5feb\u901f\u5730\u66f4\u6539\u6570\u7ec4\u4e2d\u7684\u7279\u5b9a\u5143\u7d20\uff1b\u4f7f\u7528\u5207\u7247\u53ef\u4ee5\u540c\u65f6\u66f4\u6539\u591a\u4e2a\u5143\u7d20\uff1b\u5faa\u73af\u548c\u5217\u8868\u89e3\u6790\u63d0\u4f9b\u4e86\u66f4\u7075\u6d3b\u7684\u8d4b\u503c\u65b9\u5f0f\uff0c\u800cNumPy\u5e93\u5219\u63d0\u4f9b\u4e86\u66f4\u9ad8\u6548\u3001\u66f4\u5f3a\u5927\u7684\u6570\u7ec4\u64cd\u4f5c\u529f\u80fd\u3002\u4e0b\u9762\u6211\u5c06\u8be6\u7ec6\u5c55\u5f00\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\u2014\u2014\u4f7f\u7528NumPy\u5e93\u8d4b\u503c\u7ed9\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><p>NumPy\u662fPython\u4e2d\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u5e93\uff0c\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e2a\u9ad8\u6548\u7684\u591a\u7ef4\u6570\u7ec4\u5bf9\u8c61\uff08ndarray\uff09\uff0c\u4ee5\u53ca\u8bb8\u591a\u7528\u4e8e\u64cd\u4f5c\u8fd9\u4e9b\u6570\u7ec4\u7684\u51fd\u6570\u3002\u901a\u8fc7NumPy\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u5bf9\u6570\u7ec4\u8fdb\u884c\u8d4b\u503c\u3001\u5207\u7247\u3001\u4ee5\u53ca\u5404\u79cd\u6570\u5b66\u8fd0\u7b97\u3002NumPy\u7684\u6570\u7ec4\u6bd4Python\u5185\u7f6e\u7684\u5217\u8868\u7ed3\u6784\u66f4\u9ad8\u6548\uff0c\u4e14\u4f7f\u7528\u8d77\u6765\u4e5f\u66f4\u52a0\u76f4\u89c2\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u76f4\u63a5\u6307\u5b9a\u7d22\u5f15\u8d4b\u503c<\/h3>\n<\/p>\n<p><p>\u76f4\u63a5\u6307\u5b9a\u7d22\u5f15\u8d4b\u503c\u662f\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u901a\u8fc7\u6307\u5b9a\u6570\u7ec4\u7684\u7d22\u5f15\uff0c\u53ef\u4ee5\u76f4\u63a5\u66f4\u6539\u6570\u7ec4\u4e2d\u7684\u7279\u5b9a\u5143\u7d20\u3002\u6570\u7ec4\u7684\u7d22\u5f15\u4ece0\u5f00\u59cb\u8ba1\u6570\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u7d22\u5f15\u6765\u8bbf\u95ee\u6570\u7ec4\u4e2d\u7684\u4efb\u4f55\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u6570\u7ec4<\/p>\n<p>arr = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u76f4\u63a5\u6307\u5b9a\u7d22\u5f15\u6765\u8d4b\u503c<\/strong><\/h2>\n<p>arr[2] = 10<\/p>\n<h2><strong>\u8f93\u51fa\uff1a[1, 2, 10, 4, 5]<\/strong><\/h2>\n<p>print(arr)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u901a\u8fc7<code>arr[2] = 10<\/code>\uff0c\u6211\u4eec\u5c06\u6570\u7ec4\u4e2d\u7d22\u5f15\u4e3a2\u7684\u5143\u7d20\u4ece3\u66f4\u6539\u4e3a10\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5207\u7247\u8d4b\u503c<\/h3>\n<\/p>\n<p><p>\u5207\u7247\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u7528\u4e8e\u540c\u65f6\u66f4\u6539\u6570\u7ec4\u4e2d\u7684\u591a\u4e2a\u5143\u7d20\u3002\u901a\u8fc7\u5207\u7247\uff0c\u60a8\u53ef\u4ee5\u9009\u62e9\u6570\u7ec4\u7684\u4e00\u4e2a\u5b50\u96c6\uff0c\u5e76\u5bf9\u5176\u8fdb\u884c\u8d4b\u503c\u64cd\u4f5c\u3002\u5207\u7247\u7684\u8bed\u6cd5\u4e3a<code>start:end:step<\/code>\uff0c\u5176\u4e2d<code>start<\/code>\u662f\u8d77\u59cb\u7d22\u5f15\uff0c<code>end<\/code>\u662f\u7ed3\u675f\u7d22\u5f15\uff08\u4e0d\u5305\u62ec\uff09\uff0c<code>step<\/code>\u662f\u6b65\u957f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u6570\u7ec4<\/p>\n<p>arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]<\/p>\n<h2><strong>\u4f7f\u7528\u5207\u7247\u8d4b\u503c\uff0c\u5c06\u7d22\u5f152\u52305\u7684\u5143\u7d20\u66ff\u6362\u4e3a\u65b0\u7684\u503c<\/strong><\/h2>\n<p>arr[2:6] = [10, 11, 12, 13]<\/p>\n<h2><strong>\u8f93\u51fa\uff1a[1, 2, 10, 11, 12, 13, 7, 8, 9]<\/strong><\/h2>\n<p>print(arr)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5207\u7247<code>arr[2:6]<\/code>\u9009\u62e9\u4e86\u6570\u7ec4\u4e2d\u7d22\u5f15\u4ece2\u52305\u7684\u5143\u7d20\uff0c\u5e76\u5c06\u5b83\u4eec\u66ff\u6362\u4e3a\u65b0\u7684\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5faa\u73af\u548c\u5217\u8868\u89e3\u6790\u8d4b\u503c<\/h3>\n<\/p>\n<p><p>\u5faa\u73af\u548c\u5217\u8868\u89e3\u6790\u63d0\u4f9b\u4e86\u66f4\u52a0\u7075\u6d3b\u7684\u8d4b\u503c\u65b9\u5f0f\uff0c\u7279\u522b\u9002\u7528\u4e8e\u590d\u6742\u7684\u8d4b\u503c\u903b\u8f91\u3002\u5728\u5faa\u73af\u4e2d\uff0c\u60a8\u53ef\u4ee5\u5bf9\u6570\u7ec4\u7684\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u7279\u5b9a\u7684\u5904\u7406\uff0c\u7136\u540e\u5c06\u7ed3\u679c\u8d4b\u503c\u56de\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u6570\u7ec4<\/p>\n<p>arr = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u4f7f\u7528\u5faa\u73af\u7ed9\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u52a01<\/strong><\/h2>\n<p>for i in range(len(arr)):<\/p>\n<p>    arr[i] += 1<\/p>\n<h2><strong>\u8f93\u51fa\uff1a[2, 3, 4, 5, 6]<\/strong><\/h2>\n<p>print(arr)<\/p>\n<h2><strong>\u4f7f\u7528\u5217\u8868\u89e3\u6790\u8fdb\u884c\u540c\u6837\u7684\u64cd\u4f5c<\/strong><\/h2>\n<p>arr = [x + 1 for x in arr]<\/p>\n<h2><strong>\u8f93\u51fa\uff1a[3, 4, 5, 6, 7]<\/strong><\/h2>\n<p>print(arr)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528\u4e00\u4e2a<code>for<\/code>\u5faa\u73af\u904d\u5386\u6570\u7ec4\uff0c\u5e76\u5bf9\u6bcf\u4e2a\u5143\u7d20\u52a01\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528\u5217\u8868\u89e3\u6790\u5b9e\u73b0\u4e86\u540c\u6837\u7684\u64cd\u4f5c\uff0c\u8fd9\u662f\u4e00\u4e2a\u66f4\u52a0\u7b80\u6d01\u7684\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528NumPy\u5e93\u8d4b\u503c<\/h3>\n<\/p>\n<p><p>NumPy\u662fPython\u4e2d\u5904\u7406\u6570\u7ec4\u6570\u636e\u7684\u6807\u51c6\u5e93\u3002\u4f7f\u7528NumPy\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u8fdb\u884c\u6570\u7ec4\u8d4b\u503c\u548c\u8ba1\u7b97\u3002NumPy\u7684\u6570\u7ec4\u5bf9\u8c61\u79f0\u4e3a<code>ndarray<\/code>\uff0c\u5b83\u6bd4Python\u5185\u7f6e\u7684\u5217\u8868\u7ed3\u6784\u66f4\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aNumPy\u6570\u7ec4<\/strong><\/h2>\n<p>arr = np.array([1, 2, 3, 4, 5])<\/p>\n<h2><strong>\u4f7f\u7528\u7d22\u5f15\u8d4b\u503c<\/strong><\/h2>\n<p>arr[2] = 10<\/p>\n<h2><strong>\u8f93\u51fa\uff1a[ 1  2 10  4  5]<\/strong><\/h2>\n<p>print(arr)<\/p>\n<h2><strong>\u4f7f\u7528\u5207\u7247\u8d4b\u503c<\/strong><\/h2>\n<p>arr[1:4] = [20, 30, 40]<\/p>\n<h2><strong>\u8f93\u51fa\uff1a[ 1 20 30 40  5]<\/strong><\/h2>\n<p>print(arr)<\/p>\n<h2><strong>\u4f7f\u7528NumPy\u7684\u5e03\u5c14\u7d22\u5f15\u8d4b\u503c<\/strong><\/h2>\n<p>arr[arr &gt; 20] = 100<\/p>\n<h2><strong>\u8f93\u51fa\uff1a[  1  20 100 100   5]<\/strong><\/h2>\n<p>print(arr)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528NumPy\u521b\u5efa\u4e86\u4e00\u4e2a\u6570\u7ec4\uff0c\u5e76\u901a\u8fc7\u7d22\u5f15\u3001\u5207\u7247\u3001\u5e03\u5c14\u7d22\u5f15\u7b49\u65b9\u6cd5\u5bf9\u5176\u8fdb\u884c\u8d4b\u503c\u3002NumPy\u7684\u5f3a\u5927\u4e4b\u5904\u5728\u4e8e\u5b83\u652f\u6301\u8bb8\u591a\u9ad8\u7ea7\u529f\u80fd\uff0c\u6bd4\u5982\u5e03\u5c14\u7d22\u5f15\u3001\u5e7f\u64ad\u3001\u77e2\u91cf\u5316\u64cd\u4f5c\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u8d4b\u503c\u7ed9\u6570\u7ec4\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u5305\u62ec\u76f4\u63a5\u6307\u5b9a\u7d22\u5f15\u3001\u4f7f\u7528\u5207\u7247\u3001\u5faa\u73af\u548c\u5217\u8868\u89e3\u6790\u3001\u4ee5\u53ca\u4f7f\u7528NumPy\u5e93\u7b49\u3002\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u9700\u6c42\u548c\u4f7f\u7528\u573a\u666f\u3002\u5982\u679c\u60a8\u9700\u8981\u5904\u7406\u5927\u91cf\u6570\u636e\u6216\u8fdb\u884c\u590d\u6742\u7684\u6570\u5b66\u8fd0\u7b97\uff0cNumPy\u662f\u4e00\u4e2a\u975e\u5e38\u597d\u7684\u9009\u62e9\u3002\u5b83\u4e0d\u4ec5\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u7ec4\u5bf9\u8c61\uff0c\u8fd8\u5305\u542b\u8bb8\u591a\u9ad8\u6548\u7684\u6570\u5b66\u51fd\u6570\u548c\u64cd\u4f5c\u7b26\u3002\u901a\u8fc7\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u5728Python\u4e2d\u8f7b\u677e\u5730\u64cd\u4f5c\u548c\u5904\u7406\u6570\u7ec4\u6570\u636e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u548c\u521d\u59cb\u5316\u6570\u7ec4\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6570\u7ec4\u901a\u5e38\u4f7f\u7528\u5217\u8868\u6216NumPy\u5e93\u6765\u5b9e\u73b0\u3002\u4f7f\u7528\u5217\u8868\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u76f4\u63a5\u8d4b\u503c\u7684\u65b9\u5f0f\u521b\u5efa\u548c\u521d\u59cb\u5316\u6570\u7ec4\uff0c\u4f8b\u5982\uff1a<code>my_array = [1, 2, 3, 4]<\/code>\u3002\u82e5\u4f7f\u7528NumPy\u5e93\uff0c\u9996\u5148\u9700\u8981\u5bfc\u5165\u8be5\u5e93\uff0c\u7136\u540e\u53ef\u4ee5\u7528<code>np.array()<\/code>\u51fd\u6570\u521b\u5efa\u6570\u7ec4\uff0c\u793a\u4f8b\uff1a<code>import numpy as np; my_array = np.array([1, 2, 3, 4])<\/code>\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u66f4\u65b0\u6570\u7ec4\u7684\u5143\u7d20\uff1f<\/strong><br \/>\u66f4\u65b0\u6570\u7ec4\u5143\u7d20\u7684\u65b9\u5f0f\u53d6\u51b3\u4e8e\u60a8\u4f7f\u7528\u7684\u662f\u5217\u8868\u8fd8\u662fNumPy\u6570\u7ec4\u3002\u5bf9\u4e8e\u5217\u8868\uff0c\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u5e76\u8d4b\u503c\uff0c\u4f8b\u5982\uff1a<code>my_list[0] = 10<\/code>\u3002\u5bf9\u4e8eNumPy\u6570\u7ec4\uff0c\u540c\u6837\u53ef\u4ee5\u4f7f\u7528\u7d22\u5f15\u8fdb\u884c\u66f4\u65b0\uff1a<code>my_array[0] = 10<\/code>\u3002\u8fd9\u6837\u53ef\u4ee5\u8f7b\u677e\u4fee\u6539\u6570\u7ec4\u4e2d\u7684\u7279\u5b9a\u5143\u7d20\u3002<\/p>\n<p><strong>Python\u4e2d\u5982\u4f55\u4ece\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u8d4b\u503c\u5230\u6570\u7ec4\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5c06\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u7684\u503c\u8d4b\u7ed9\u6570\u7ec4\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4ece\u5143\u7ec4\u3001\u96c6\u5408\u6216\u5b57\u5178\u4e2d\u63d0\u53d6\u503c\u5e76\u8d4b\u7ed9\u6570\u7ec4\u3002\u5bf9\u4e8e\u5217\u8868\uff0c\u53ef\u4ee5\u4f7f\u7528<code>list()<\/code>\u51fd\u6570\u5c06\u5143\u7ec4\u8f6c\u6362\u4e3a\u5217\u8868\uff1a<code>my_array = list((1, 2, 3))<\/code>\u3002\u5bf9\u4e8e\u5b57\u5178\uff0c\u53ef\u4ee5\u63d0\u53d6\u952e\u6216\u503c\u5e76\u8d4b\u503c\uff0c\u4f8b\u5982\uff1a<code>my_array = list(my_dict.values())<\/code>\uff0c\u8fd9\u6837\u53ef\u4ee5\u6839\u636e\u9700\u8981\u7075\u6d3b\u5730\u521b\u5efa\u548c\u521d\u59cb\u5316\u6570\u7ec4\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u8d4b\u503c\u7ed9\u6570\u7ec4\u53ef\u4ee5\u901a\u8fc7\u76f4\u63a5\u6307\u5b9a\u7d22\u5f15\u3001\u4f7f\u7528\u5207\u7247\u3001\u5229\u7528\u5faa\u73af\u6216\u5217\u8868\u89e3\u6790\u3001\u4ee5\u53ca\u4f7f\u7528NumPy\u5e93\u7b49\u65b9\u6cd5\u5b9e\u73b0 [&hellip;]","protected":false},"author":3,"featured_media":991396,"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\/991389"}],"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=991389"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/991389\/revisions"}],"predecessor-version":[{"id":991399,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/991389\/revisions\/991399"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/991396"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=991389"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=991389"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=991389"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}