{"id":1119901,"date":"2025-01-08T18:48:59","date_gmt":"2025-01-08T10:48:59","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1119901.html"},"modified":"2025-01-08T18:49:02","modified_gmt":"2025-01-08T10:49:02","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e6%af%8f%e4%b8%aa%e5%85%83%e7%b4%a0%e9%83%bd%e5%8a%a0%e4%b8%80","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1119901.html","title":{"rendered":"python\u5982\u4f55\u5c06\u6bcf\u4e2a\u5143\u7d20\u90fd\u52a0\u4e00"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25082802\/cba83b33-cb5c-447d-9551-f7d2319c4088.webp\" alt=\"python\u5982\u4f55\u5c06\u6bcf\u4e2a\u5143\u7d20\u90fd\u52a0\u4e00\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5c06\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u52a0\u4e00\uff0c<strong>\u4f7f\u7528\u5217\u8868\u89e3\u6790\u3001\u4f7f\u7528\u5faa\u73af\u3001\u4f7f\u7528NumPy\u5e93<\/strong>\u3002\u5176\u4e2d\uff0c\u5217\u8868\u89e3\u6790\u662f\u4e00\u79cd\u7b80\u6d01\u9ad8\u6548\u7684\u65b9\u6cd5\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u7ed3\u5408\u793a\u4f8b\u4ee3\u7801\u8fdb\u884c\u8bf4\u660e\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u662fPython\u7684\u4e00\u79cd\u72ec\u7279\u800c\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u7528\u4e8e\u4ee5\u7b80\u6d01\u7684\u65b9\u5f0f\u521b\u5efa\u5217\u8868\u3002\u5b83\u53ef\u4ee5\u7528\u6765\u5c06\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u52a0\u4e00\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u539f\u59cb\u5217\u8868<\/p>\n<p>original_list = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u4f7f\u7528\u5217\u8868\u89e3\u6790\u5c06\u6bcf\u4e2a\u5143\u7d20\u52a0\u4e00<\/strong><\/h2>\n<p>new_list = [x + 1 for x in original_list]<\/p>\n<p>print(new_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7\u5217\u8868\u89e3\u6790\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u5217\u8868<code>new_list<\/code>\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f<code>original_list<\/code>\u4e2d\u5bf9\u5e94\u5143\u7d20\u52a0\u4e00\u7684\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong>  <\/p>\n<\/p>\n<ul>\n<li><strong>\u7b80\u6d01\uff1a<\/strong> \u4e00\u884c\u4ee3\u7801\u5373\u53ef\u5b8c\u6210\u64cd\u4f5c\u3002<\/li>\n<li><strong>\u9ad8\u6548\uff1a<\/strong> \u5217\u8868\u89e3\u6790\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u6bd4\u4f7f\u7528\u663e\u5f0f\u5faa\u73af\u66f4\u5feb\u3002<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5faa\u73af<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u5faa\u73af\u662f\u53e6\u4e00\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u5404\u79cd\u573a\u666f\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u5c06\u5217\u8868\u4e2d\u6bcf\u4e2a\u5143\u7d20\u90fd\u52a0\u4e00\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u539f\u59cb\u5217\u8868<\/p>\n<p>original_list = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7a7a\u5217\u8868\u5b58\u50a8\u7ed3\u679c<\/strong><\/h2>\n<p>new_list = []<\/p>\n<h2><strong>\u4f7f\u7528for\u5faa\u73af\u5c06\u6bcf\u4e2a\u5143\u7d20\u52a0\u4e00<\/strong><\/h2>\n<p>for x in original_list:<\/p>\n<p>    new_list.append(x + 1)<\/p>\n<p>print(new_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7<code>for<\/code>\u5faa\u73af\u904d\u5386<code>original_list<\/code>\uff0c\u5e76\u5c06\u6bcf\u4e2a\u5143\u7d20\u52a0\u4e00\u540e\u7684\u7ed3\u679c\u6dfb\u52a0\u5230<code>new_list<\/code>\u4e2d\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong>  <\/p>\n<\/p>\n<ul>\n<li><strong>\u53ef\u8bfb\u6027\uff1a<\/strong> \u4ee3\u7801\u903b\u8f91\u6e05\u6670\uff0c\u9002\u5408\u521d\u5b66\u8005\u3002<\/li>\n<li><strong>\u7075\u6d3b\u6027\uff1a<\/strong> \u53ef\u4ee5\u5728\u5faa\u73af\u4e2d\u6dfb\u52a0\u66f4\u591a\u7684\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u4f7f\u7528NumPy\u5e93<\/h3>\n<\/p>\n<p><p>NumPy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u7279\u522b\u9002\u7528\u4e8e\u5904\u7406\u5927\u578b\u6570\u7ec4\u548c\u77e9\u9635\u3002\u4f7f\u7528NumPy\u53ef\u4ee5\u5927\u5927\u7b80\u5316\u548c\u52a0\u901f\u5217\u8868\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u539f\u59cb\u5217\u8868<\/strong><\/h2>\n<p>original_list = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u5c06\u5217\u8868\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4<\/strong><\/h2>\n<p>original_array = np.array(original_list)<\/p>\n<h2><strong>\u5c06\u6bcf\u4e2a\u5143\u7d20\u52a0\u4e00<\/strong><\/h2>\n<p>new_array = original_array + 1<\/p>\n<p>print(new_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u5c06<code>original_list<\/code>\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4<code>original_array<\/code>\uff0c\u7136\u540e\u76f4\u63a5\u5bf9\u6570\u7ec4\u8fdb\u884c\u52a0\u4e00\u64cd\u4f5c\uff0c\u5f97\u5230<code>new_array<\/code>\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong>  <\/p>\n<\/p>\n<ul>\n<li><strong>\u9ad8\u6548\uff1a<\/strong> NumPy\u5bf9\u6570\u7ec4\u64cd\u4f5c\u8fdb\u884c\u4e86\u9ad8\u5ea6\u4f18\u5316\uff0c\u901f\u5ea6\u5feb\u3002<\/li>\n<li><strong>\u7b80\u6d01\uff1a<\/strong> \u4ee3\u7801\u7b80\u6d01\uff0c\u4fbf\u4e8e\u7406\u89e3\u3002<\/li>\n<\/ul>\n<p><h3>\u56db\u3001\u4f7f\u7528Map\u51fd\u6570<\/h3>\n<\/p>\n<p><p>Python\u7684\u5185\u7f6e<code>map<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06\u6307\u5b9a\u7684\u51fd\u6570\u5e94\u7528\u4e8e\u5217\u8868\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528<code>map<\/code>\u51fd\u6570\u5c06\u6bcf\u4e2a\u5143\u7d20\u52a0\u4e00\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u539f\u59cb\u5217\u8868<\/p>\n<p>original_list = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u4f7f\u7528map\u51fd\u6570\u5c06\u6bcf\u4e2a\u5143\u7d20\u52a0\u4e00<\/strong><\/h2>\n<p>new_list = list(map(lambda x: x + 1, original_list))<\/p>\n<p>print(new_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>map<\/code>\u51fd\u6570\u5c06\u533f\u540d\u51fd\u6570<code>lambda x: x + 1<\/code>\u5e94\u7528\u4e8e<code>original_list<\/code>\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5e76\u5c06\u7ed3\u679c\u8f6c\u6362\u4e3a\u5217\u8868<code>new_list<\/code>\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong>  <\/p>\n<\/p>\n<ul>\n<li><strong>\u7b80\u6d01\uff1a<\/strong> \u4f7f\u7528\u51fd\u6570\u5f0f\u7f16\u7a0b\u98ce\u683c\uff0c\u4ee3\u7801\u7b80\u77ed\u3002<\/li>\n<li><strong>\u53ef\u8bfb\u6027\uff1a<\/strong> \u5bf9\u4e8e\u719f\u6089<code>map<\/code>\u51fd\u6570\u7684\u8bfb\u8005\uff0c\u4ee3\u7801\u6613\u4e8e\u7406\u89e3\u3002<\/li>\n<\/ul>\n<p><h3>\u4e94\u3001\u4f7f\u7528Pandas\u5e93<\/h3>\n<\/p>\n<p><p>Pandas\u662f\u4e00\u4e2a\u7528\u4e8e\u6570\u636e\u64cd\u4f5c\u548c\u5206\u6790\u7684\u5f3a\u5927\u5e93\uff0c\u7279\u522b\u9002\u7528\u4e8e\u5904\u7406\u8868\u683c\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Pandas\u5c06\u6bcf\u4e2a\u5143\u7d20\u52a0\u4e00\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u539f\u59cb\u5217\u8868<\/strong><\/h2>\n<p>original_list = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u5c06\u5217\u8868\u8f6c\u6362\u4e3aPandas Series<\/strong><\/h2>\n<p>original_series = pd.Series(original_list)<\/p>\n<h2><strong>\u5c06\u6bcf\u4e2a\u5143\u7d20\u52a0\u4e00<\/strong><\/h2>\n<p>new_series = original_series + 1<\/p>\n<p>print(new_series)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u5c06<code>original_list<\/code>\u8f6c\u6362\u4e3aPandas\u7684<code>Series<\/code>\u5bf9\u8c61<code>original_series<\/code>\uff0c\u7136\u540e\u76f4\u63a5\u5bf9<code>Series<\/code>\u8fdb\u884c\u52a0\u4e00\u64cd\u4f5c\uff0c\u5f97\u5230<code>new_series<\/code>\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong>  <\/p>\n<\/p>\n<ul>\n<li><strong>\u7b80\u6d01\uff1a<\/strong> Pandas\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u64cd\u4f5c\u51fd\u6570\uff0c\u4ee3\u7801\u7b80\u77ed\u3002<\/li>\n<li><strong>\u529f\u80fd\u5f3a\u5927\uff1a<\/strong> Pandas\u9002\u7528\u4e8e\u66f4\u590d\u6742\u7684\u6570\u636e\u64cd\u4f5c\u548c\u5206\u6790\u4efb\u52a1\u3002<\/li>\n<\/ul>\n<p><h3>\u516d\u3001\u4f7f\u7528\u5217\u8868\u7684\u751f\u6210\u5668\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u7684\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u4e0e\u5217\u8868\u89e3\u6790\u7c7b\u4f3c\uff0c\u4f46\u5b83\u4e0d\u4f1a\u7acb\u5373\u751f\u6210\u5b8c\u6574\u7684\u5217\u8868\uff0c\u800c\u662f\u751f\u6210\u4e00\u4e2a\u751f\u6210\u5668\u5bf9\u8c61\u3002\u8fd9\u5728\u5904\u7406\u5927\u578b\u5217\u8868\u65f6\u975e\u5e38\u6709\u7528\uff0c\u56e0\u4e3a\u5b83\u8282\u7701\u4e86\u5185\u5b58\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u539f\u59cb\u5217\u8868<\/p>\n<p>original_list = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u5c06\u6bcf\u4e2a\u5143\u7d20\u52a0\u4e00<\/strong><\/h2>\n<p>new_generator = (x + 1 for x in original_list)<\/p>\n<h2><strong>\u5c06\u751f\u6210\u5668\u8f6c\u6362\u4e3a\u5217\u8868<\/strong><\/h2>\n<p>new_list = list(new_generator)<\/p>\n<p>print(new_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u521b\u5efa\u4e86\u4e00\u4e2a\u751f\u6210\u5668\u5bf9\u8c61<code>new_generator<\/code>\uff0c\u7136\u540e\u5c06\u5176\u8f6c\u6362\u4e3a\u5217\u8868<code>new_list<\/code>\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong>  <\/p>\n<\/p>\n<ul>\n<li><strong>\u8282\u7701\u5185\u5b58\uff1a<\/strong> \u5bf9\u4e8e\u5927\u578b\u5217\u8868\uff0c\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u53ef\u4ee5\u8282\u7701\u5927\u91cf\u5185\u5b58\u3002<\/li>\n<li><strong>\u9ad8\u6548\uff1a<\/strong> \u751f\u6210\u5668\u6309\u9700\u751f\u6210\u5143\u7d20\uff0c\u6548\u7387\u9ad8\u3002<\/li>\n<\/ul>\n<p><h3>\u4e03\u3001\u4f7f\u7528\u9012\u5f52\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u9012\u5f52\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u5c06\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u52a0\u4e00\uff0c\u5c3d\u7ba1\u8fd9\u4e0d\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u9012\u5f52\u51fd\u6570\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u9012\u5f52\u51fd\u6570\u5c06\u6bcf\u4e2a\u5143\u7d20\u52a0\u4e00<\/p>\n<p>def add_one_recursive(lst):<\/p>\n<p>    if not lst:<\/p>\n<p>        return []<\/p>\n<p>    else:<\/p>\n<p>        return [lst[0] + 1] + add_one_recursive(lst[1:])<\/p>\n<h2><strong>\u539f\u59cb\u5217\u8868<\/strong><\/h2>\n<p>original_list = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u8c03\u7528\u9012\u5f52\u51fd\u6570<\/strong><\/h2>\n<p>new_list = add_one_recursive(original_list)<\/p>\n<p>print(new_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u9012\u5f52\u51fd\u6570<code>add_one_recursive<\/code>\u5c06\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u52a0\u4e00\uff0c\u5e76\u5c06\u7ed3\u679c\u8fd4\u56de\u7ed9<code>new_list<\/code>\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong>  <\/p>\n<\/p>\n<ul>\n<li><strong>\u9012\u5f52\u601d\u7ef4\uff1a<\/strong> \u9002\u5408\u719f\u6089\u9012\u5f52\u7684\u8bfb\u8005\u3002<\/li>\n<li><strong>\u4ee3\u7801\u7b80\u6d01\uff1a<\/strong> \u9012\u5f52\u51fd\u6570\u7684\u4ee3\u7801\u901a\u5e38\u8f83\u4e3a\u7b80\u6d01\u3002<\/li>\n<\/ul>\n<p><h3>\u516b\u3001\u4f7f\u7528\u96c6\u5408\u63a8\u5bfc\u5f0f<\/h3>\n<\/p>\n<p><p>\u96c6\u5408\u63a8\u5bfc\u5f0f\u4e0e\u5217\u8868\u89e3\u6790\u7c7b\u4f3c\uff0c\u4f46\u5b83\u7528\u4e8e\u521b\u5efa\u96c6\u5408\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528\u96c6\u5408\u63a8\u5bfc\u5f0f\u5c06\u6bcf\u4e2a\u5143\u7d20\u52a0\u4e00\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u539f\u59cb\u5217\u8868<\/p>\n<p>original_list = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u4f7f\u7528\u96c6\u5408\u63a8\u5bfc\u5f0f\u5c06\u6bcf\u4e2a\u5143\u7d20\u52a0\u4e00<\/strong><\/h2>\n<p>new_set = {x + 1 for x in original_list}<\/p>\n<p>print(new_set)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7\u96c6\u5408\u63a8\u5bfc\u5f0f\u521b\u5efa\u4e86\u4e00\u4e2a\u96c6\u5408<code>new_set<\/code>\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f<code>original_list<\/code>\u4e2d\u5bf9\u5e94\u5143\u7d20\u52a0\u4e00\u7684\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong>  <\/p>\n<\/p>\n<ul>\n<li><strong>\u53bb\u91cd\uff1a<\/strong> \u96c6\u5408\u63a8\u5bfc\u5f0f\u81ea\u52a8\u53bb\u9664\u91cd\u590d\u5143\u7d20\u3002<\/li>\n<li><strong>\u7b80\u6d01\uff1a<\/strong> \u4ee3\u7801\u7b80\u77ed\uff0c\u6613\u4e8e\u7406\u89e3\u3002<\/li>\n<\/ul>\n<p><h3>\u4e5d\u3001\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f<\/h3>\n<\/p>\n<p><p>\u5b57\u5178\u63a8\u5bfc\u5f0f\u4e0e\u5217\u8868\u89e3\u6790\u7c7b\u4f3c\uff0c\u4f46\u5b83\u7528\u4e8e\u521b\u5efa\u5b57\u5178\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u5c06\u6bcf\u4e2a\u5143\u7d20\u52a0\u4e00\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u539f\u59cb\u5217\u8868<\/p>\n<p>original_list = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u5c06\u6bcf\u4e2a\u5143\u7d20\u52a0\u4e00<\/strong><\/h2>\n<p>new_dict = {x: x + 1 for x in original_list}<\/p>\n<p>print(new_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7\u5b57\u5178\u63a8\u5bfc\u5f0f\u521b\u5efa\u4e86\u4e00\u4e2a\u5b57\u5178<code>new_dict<\/code>\uff0c\u5176\u4e2d\u6bcf\u4e2a\u952e\u503c\u5bf9\u7684\u503c\u90fd\u662f<code>original_list<\/code>\u4e2d\u5bf9\u5e94\u5143\u7d20\u52a0\u4e00\u7684\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong>  <\/p>\n<\/p>\n<ul>\n<li><strong>\u952e\u503c\u5bf9\uff1a<\/strong> \u5b57\u5178\u63a8\u5bfc\u5f0f\u9002\u7528\u4e8e\u9700\u8981\u952e\u503c\u5bf9\u7684\u573a\u666f\u3002<\/li>\n<li><strong>\u7b80\u6d01\uff1a<\/strong> \u4ee3\u7801\u7b80\u77ed\uff0c\u6613\u4e8e\u7406\u89e3\u3002<\/li>\n<\/ul>\n<p><h3>\u5341\u3001\u4f7f\u7528itertools\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>itertools<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u8bb8\u591a\u7528\u4e8e\u8fed\u4ee3\u64cd\u4f5c\u7684\u51fd\u6570\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528<code>itertools.starmap<\/code>\u5c06\u6bcf\u4e2a\u5143\u7d20\u52a0\u4e00\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import itertools<\/p>\n<h2><strong>\u539f\u59cb\u5217\u8868<\/strong><\/h2>\n<p>original_list = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u4f7f\u7528itertools.starmap\u5c06\u6bcf\u4e2a\u5143\u7d20\u52a0\u4e00<\/strong><\/h2>\n<p>new_list = list(itertools.starmap(lambda x: x + 1, zip(original_list)))<\/p>\n<p>print(new_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7<code>itertools.starmap<\/code>\u51fd\u6570\u548c<code>zip<\/code>\u51fd\u6570\u5c06\u6bcf\u4e2a\u5143\u7d20\u52a0\u4e00\uff0c\u5e76\u5c06\u7ed3\u679c\u8f6c\u6362\u4e3a\u5217\u8868<code>new_list<\/code>\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong>  <\/p>\n<\/p>\n<ul>\n<li><strong>\u9ad8\u6548\uff1a<\/strong> <code>itertools<\/code>\u6a21\u5757\u5bf9\u8fed\u4ee3\u64cd\u4f5c\u8fdb\u884c\u4e86\u4f18\u5316\uff0c\u6548\u7387\u9ad8\u3002<\/li>\n<li><strong>\u7075\u6d3b\u6027\uff1a<\/strong> \u9002\u7528\u4e8e\u66f4\u590d\u6742\u7684\u8fed\u4ee3\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5c06\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u52a0\u4e00\uff0c<strong>\u4f7f\u7528\u5217\u8868\u89e3\u6790\u3001\u4f7f\u7528\u5faa\u73af\u3001\u4f7f\u7528NumPy\u5e93\u3001\u4f7f\u7528Map\u51fd\u6570\u3001\u4f7f\u7528Pandas\u5e93\u3001\u4f7f\u7528\u5217\u8868\u7684\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u3001\u4f7f\u7528\u9012\u5f52\u51fd\u6570\u3001\u4f7f\u7528\u96c6\u5408\u63a8\u5bfc\u5f0f\u3001\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u3001\u4f7f\u7528itertools\u6a21\u5757<\/strong>\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u7b80\u6d01\u6027\u548c\u6548\u7387\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u573a\u666f\u9009\u62e9\u6700\u9002\u5408\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5bf9\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u52a0\u4e00\u64cd\u4f5c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u5b9e\u73b0\u5bf9\u6bcf\u4e2a\u5143\u7d20\u52a0\u4e00\u7684\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u5217\u8868<code>numbers = [1, 2, 3, 4]<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<code>new_numbers = [x + 1 for x in numbers]<\/code>\u3002\u8fd9\u6837\uff0c<code>new_numbers<\/code>\u5c06\u53d8\u6210<code>[2, 3, 4, 5]<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u6d01\u4e14\u9ad8\u6548\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u5bf9\u6570\u7ec4\u4e2d\u6bcf\u4e2a\u5143\u7d20\u52a0\u4e00\uff1f<\/strong><br \/>\u9664\u4e86\u5217\u8868\u63a8\u5bfc\u5f0f\uff0cPython\u7684NumPy\u5e93\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e2a\u529f\u80fd\u3002\u4f7f\u7528NumPy\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u3002\u5982\u679c\u4f60\u5b89\u88c5\u4e86NumPy\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<code>import numpy as np<\/code>\uff0c\u7136\u540e\u521b\u5efa\u4e00\u4e2a\u6570\u7ec4<code>arr = np.array([1, 2, 3, 4])<\/code>\uff0c\u63a5\u7740\u901a\u8fc7<code>arr + 1<\/code>\u6765\u5b9e\u73b0\u52a0\u4e00\u64cd\u4f5c\uff0c\u7ed3\u679c\u5c06\u662f<code>array([2, 3, 4, 5])<\/code>\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u5305\u542b\u4e0d\u540c\u7c7b\u578b\u5143\u7d20\u7684\u5217\u8868\uff1f<\/strong><br \/>\u5982\u679c\u5217\u8868\u4e2d\u5305\u542b\u4e0d\u540c\u7c7b\u578b\u7684\u5143\u7d20\uff0c\u6bd4\u5982\u6574\u6570\u548c\u5b57\u7b26\u4e32\uff0c\u76f4\u63a5\u52a0\u4e00\u4f1a\u5bfc\u81f4\u9519\u8bef\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u8fdb\u884c\u5224\u65ad\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">mixed_list = [1, &#39;two&#39;, 3]\nresult = [x + 1 if isinstance(x, int) else x for x in mixed_list]\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u8ba9\u6574\u6570\u52a0\u4e00\uff0c\u800c\u4fdd\u7559\u5b57\u7b26\u4e32\u4e0d\u53d8\uff0c\u5f97\u5230\u7684\u7ed3\u679c\u4e3a<code>[2, &#39;two&#39;, 4]<\/code>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5c06\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u52a0\u4e00\uff0c\u4f7f\u7528\u5217\u8868\u89e3\u6790\u3001\u4f7f\u7528\u5faa\u73af\u3001\u4f7f\u7528NumPy\u5e93\u3002\u5176\u4e2d\uff0c\u5217\u8868 [&hellip;]","protected":false},"author":3,"featured_media":1119909,"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\/1119901"}],"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=1119901"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1119901\/revisions"}],"predecessor-version":[{"id":1119910,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1119901\/revisions\/1119910"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1119909"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1119901"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1119901"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1119901"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}