{"id":1099177,"date":"2025-01-08T15:28:07","date_gmt":"2025-01-08T07:28:07","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1099177.html"},"modified":"2025-01-08T15:28:11","modified_gmt":"2025-01-08T07:28:11","slug":"python%e5%a6%82%e4%bd%95%e4%bf%ae%e6%94%b9%e5%88%97%e8%a1%a8%e4%b8%ad%e7%9a%84%e6%9f%90%e4%b8%aa%e5%85%83%e7%b4%a0-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1099177.html","title":{"rendered":"python\u5982\u4f55\u4fee\u6539\u5217\u8868\u4e2d\u7684\u67d0\u4e2a\u5143\u7d20"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25063022\/521813a3-590c-46c8-b7b3-03f1a85565dd.webp\" alt=\"python\u5982\u4f55\u4fee\u6539\u5217\u8868\u4e2d\u7684\u67d0\u4e2a\u5143\u7d20\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u4fee\u6539\u5217\u8868\u4e2d\u7684\u67d0\u4e2a\u5143\u7d20\u6709\u591a\u79cd\u65b9\u6cd5<\/strong>\uff0c\u4f8b\u5982\u4f7f\u7528\u7d22\u5f15\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u5faa\u73af\u904d\u5386\u7b49\u3002<strong>\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u7d22\u5f15\u76f4\u63a5\u4fee\u6539\u3001\u4f7f\u7528\u5faa\u73af\u904d\u5386\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/strong>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff0c\u5373\u901a\u8fc7\u7d22\u5f15\u76f4\u63a5\u4fee\u6539\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p><strong>\u76f4\u63a5\u4f7f\u7528\u7d22\u5f15\u4fee\u6539\u5217\u8868\u4e2d\u7684\u5143\u7d20\u662f\u4e00\u79cd\u7b80\u5355\u800c\u76f4\u63a5\u7684\u65b9\u6cd5<\/strong>\u3002\u5728Python\u4e2d\uff0c\u5217\u8868\u662f\u53ef\u53d8\u7684\u5e8f\u5217\u7c7b\u578b\uff0c\u56e0\u6b64\u4f60\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u5e76\u4fee\u6539\u5176\u5143\u7d20\u3002\u9996\u5148\uff0c\u627e\u5230\u4f60\u8981\u4fee\u6539\u7684\u5143\u7d20\u7684\u7d22\u5f15\u4f4d\u7f6e\uff0c\u7136\u540e\u4f7f\u7528\u8d4b\u503c\u8fd0\u7b97\u7b26\u201c=\u201d\u6765\u66f4\u65b0\u8be5\u4f4d\u7f6e\u7684\u503c\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u5217\u8868<\/p>\n<p>my_list = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u4fee\u6539\u7d22\u5f15\u4e3a2\u7684\u5143\u7d20\uff08\u5373\u7b2c3\u4e2a\u5143\u7d20\uff09<\/strong><\/h2>\n<p>my_list[2] = 10<\/p>\n<h2><strong>\u8f93\u51fa\u4fee\u6539\u540e\u7684\u5217\u8868<\/strong><\/h2>\n<p>print(my_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\u5c06\u662f <code>[1, 2, 10, 4, 5]<\/code>\uff0c\u53ef\u4ee5\u770b\u5230\u5217\u8868\u4e2d\u7684\u7b2c\u4e09\u4e2a\u5143\u7d20\u5df2\u6210\u529f\u4fee\u6539\u4e3a10\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001\u76f4\u63a5\u4f7f\u7528\u7d22\u5f15\u4fee\u6539<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u7d22\u5f15\u76f4\u63a5\u4fee\u6539\u5217\u8868\u4e2d\u7684\u5143\u7d20\u662f\u4e00\u79cd\u6700\u7b80\u5355\u3001\u76f4\u63a5\u7684\u65b9\u6cd5\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u77e5\u9053\u8981\u4fee\u6539\u7684\u5143\u7d20\u7684\u7d22\u5f15\u4f4d\u7f6e\uff0c\u7136\u540e\u4f7f\u7528\u8d4b\u503c\u8fd0\u7b97\u7b26\u201c=\u201d\u6765\u66f4\u65b0\u8be5\u4f4d\u7f6e\u7684\u503c\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u5217\u8868<\/p>\n<p>my_list = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;]<\/p>\n<h2><strong>\u4fee\u6539\u7d22\u5f15\u4e3a1\u7684\u5143\u7d20\uff08\u5373\u7b2c2\u4e2a\u5143\u7d20\uff09<\/strong><\/h2>\n<p>my_list[1] = &#39;z&#39;<\/p>\n<h2><strong>\u8f93\u51fa\u4fee\u6539\u540e\u7684\u5217\u8868<\/strong><\/h2>\n<p>print(my_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8f93\u51fa\u7ed3\u679c\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code>[&#39;a&#39;, &#39;z&#39;, &#39;c&#39;, &#39;d&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u7d22\u5f15\u4e3a1\u7684\u5143\u7d20&#39;b&#39;\u4fee\u6539\u4e3a\u4e86&#39;z&#39;\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5faa\u73af\u904d\u5386\u4fee\u6539<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u6211\u4eec\u9700\u8981\u6839\u636e\u67d0\u4e9b\u6761\u4ef6\u6765\u4fee\u6539\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff0c\u6b64\u65f6\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u904d\u5386\u5217\u8868\u5e76\u5728\u6ee1\u8db3\u6761\u4ef6\u65f6\u8fdb\u884c\u4fee\u6539\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u5217\u8868<\/p>\n<p>my_list = [1, 2, 3, 4, 5, 6]<\/p>\n<h2><strong>\u4f7f\u7528\u5faa\u73af\u904d\u5386\u5217\u8868<\/strong><\/h2>\n<p>for i in range(len(my_list)):<\/p>\n<p>    # \u5982\u679c\u5143\u7d20\u662f\u5076\u6570\uff0c\u5219\u5c06\u5176\u4fee\u6539\u4e3a\u8be5\u5143\u7d20\u7684\u5e73\u65b9<\/p>\n<p>    if my_list[i] % 2 == 0:<\/p>\n<p>        my_list[i] = my_list[i]  2<\/p>\n<h2><strong>\u8f93\u51fa\u4fee\u6539\u540e\u7684\u5217\u8868<\/strong><\/h2>\n<p>print(my_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8f93\u51fa\u7ed3\u679c\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code>[1, 4, 3, 16, 5, 36]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u5217\u8868\u4e2d\u7684\u5076\u6570\u5143\u7d20\u4fee\u6539\u4e3a\u4e86\u5b83\u4eec\u7684\u5e73\u65b9\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u4fee\u6539<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u8bed\u6cd5\uff0c\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u65b0\u7684\u5217\u8868\u6216\u4fee\u6539\u73b0\u6709\u5217\u8868\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u6839\u636e\u67d0\u4e9b\u6761\u4ef6\u4fee\u6539\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u5217\u8868<\/p>\n<p>my_list = [1, 2, 3, 4, 5, 6]<\/p>\n<h2><strong>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u4fee\u6539\u5217\u8868\u4e2d\u7684\u5076\u6570\u5143\u7d20\u4e3a\u5176\u5e73\u65b9<\/strong><\/h2>\n<p>my_list = [x2 if x % 2 == 0 else x for x in my_list]<\/p>\n<h2><strong>\u8f93\u51fa\u4fee\u6539\u540e\u7684\u5217\u8868<\/strong><\/h2>\n<p>print(my_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8f93\u51fa\u7ed3\u679c\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code>[1, 4, 3, 16, 5, 36]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u5c06\u5076\u6570\u5143\u7d20\u4fee\u6539\u4e3a\u4e86\u5b83\u4eec\u7684\u5e73\u65b9\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<code>map<\/code>\u8fdb\u884c\u4fee\u6539<\/h3>\n<\/p>\n<p><p>Python\u7684\u5185\u7f6e\u51fd\u6570<code>map<\/code>\u53ef\u4ee5\u5c06\u4e00\u4e2a\u51fd\u6570\u5e94\u7528\u5230\u4e00\u4e2a\u6216\u591a\u4e2a\u5217\u8868\u7684\u6240\u6709\u5143\u7d20\u4e0a\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\u3002\u6211\u4eec\u53ef\u4ee5\u5229\u7528<code>map<\/code>\u51fd\u6570\u6765\u4fee\u6539\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u5217\u8868<\/p>\n<p>my_list = [1, 2, 3, 4, 5, 6]<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u7528\u4e8e\u5c06\u5076\u6570\u4fee\u6539\u4e3a\u5176\u5e73\u65b9<\/strong><\/h2>\n<p>def modify_element(x):<\/p>\n<p>    return x2 if x % 2 == 0 else x<\/p>\n<h2><strong>\u4f7f\u7528map\u51fd\u6570\u4fee\u6539\u5217\u8868\u4e2d\u7684\u5076\u6570\u5143\u7d20<\/strong><\/h2>\n<p>my_list = list(map(modify_element, my_list))<\/p>\n<h2><strong>\u8f93\u51fa\u4fee\u6539\u540e\u7684\u5217\u8868<\/strong><\/h2>\n<p>print(my_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8f93\u51fa\u7ed3\u679c\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code>[1, 4, 3, 16, 5, 36]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>map<\/code>\u51fd\u6570\u5c06\u5076\u6570\u5143\u7d20\u4fee\u6539\u4e3a\u4e86\u5b83\u4eec\u7684\u5e73\u65b9\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528<code>enumerate<\/code>\u83b7\u53d6\u7d22\u5f15\u5e76\u4fee\u6539<\/h3>\n<\/p>\n<p><p><code>enumerate<\/code>\u51fd\u6570\u53ef\u4ee5\u8fd4\u56de\u5217\u8868\u4e2d\u7684\u5143\u7d20\u53ca\u5176\u5bf9\u5e94\u7684\u7d22\u5f15\u3002\u6211\u4eec\u53ef\u4ee5\u5229\u7528<code>enumerate<\/code>\u51fd\u6570\u6765\u904d\u5386\u5217\u8868\uff0c\u5e76\u5728\u904d\u5386\u8fc7\u7a0b\u4e2d\u6839\u636e\u7d22\u5f15\u4fee\u6539\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u5217\u8868<\/p>\n<p>my_list = [1, 2, 3, 4, 5, 6]<\/p>\n<h2><strong>\u4f7f\u7528enumerate\u51fd\u6570\u904d\u5386\u5217\u8868<\/strong><\/h2>\n<p>for index, value in enumerate(my_list):<\/p>\n<p>    # \u5982\u679c\u5143\u7d20\u662f\u5076\u6570\uff0c\u5219\u5c06\u5176\u4fee\u6539\u4e3a\u8be5\u5143\u7d20\u7684\u5e73\u65b9<\/p>\n<p>    if value % 2 == 0:<\/p>\n<p>        my_list[index] = value  2<\/p>\n<h2><strong>\u8f93\u51fa\u4fee\u6539\u540e\u7684\u5217\u8868<\/strong><\/h2>\n<p>print(my_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8f93\u51fa\u7ed3\u679c\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code>[1, 4, 3, 16, 5, 36]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>enumerate<\/code>\u51fd\u6570\u904d\u5386\u5217\u8868\u5e76\u4fee\u6539\u5076\u6570\u5143\u7d20\u4e3a\u5b83\u4eec\u7684\u5e73\u65b9\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528<code>filter<\/code>\u51fd\u6570\u548c\u751f\u6210\u5668\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u867d\u7136<code>filter<\/code>\u51fd\u6570\u901a\u5e38\u7528\u4e8e\u7b5b\u9009\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff0c\u4f46\u6211\u4eec\u4e5f\u53ef\u4ee5\u7ed3\u5408\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u5bf9\u5217\u8868\u8fdb\u884c\u4fee\u6539\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u5217\u8868<\/p>\n<p>my_list = [1, 2, 3, 4, 5, 6]<\/p>\n<h2><strong>\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u548cfilter\u51fd\u6570\u4fee\u6539\u5217\u8868\u4e2d\u7684\u5076\u6570\u5143\u7d20<\/strong><\/h2>\n<p>my_list = [x2 if x % 2 == 0 else x for x in filter(lambda x: True, my_list)]<\/p>\n<h2><strong>\u8f93\u51fa\u4fee\u6539\u540e\u7684\u5217\u8868<\/strong><\/h2>\n<p>print(my_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8f93\u51fa\u7ed3\u679c\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code>[1, 4, 3, 16, 5, 36]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u7ed3\u5408\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u548c<code>filter<\/code>\u51fd\u6570\u5c06\u5076\u6570\u5143\u7d20\u4fee\u6539\u4e3a\u4e86\u5b83\u4eec\u7684\u5e73\u65b9\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528<code>numpy<\/code>\u5e93\u8fdb\u884c\u4fee\u6539<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u7684\u5217\u8868\u662f\u6570\u503c\u7c7b\u578b\u7684\uff0c\u4e14\u4f60\u9700\u8981\u5bf9\u5927\u578b\u6570\u636e\u96c6\u8fdb\u884c\u9ad8\u6548\u7684\u64cd\u4f5c\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528<code>numpy<\/code>\u5e93\u3002<code>numpy<\/code>\u5e93\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u5217\u8868<\/strong><\/h2>\n<p>my_list = [1, 2, 3, 4, 5, 6]<\/p>\n<h2><strong>\u5c06\u5217\u8868\u8f6c\u6362\u4e3anumpy\u6570\u7ec4<\/strong><\/h2>\n<p>arr = np.array(my_list)<\/p>\n<h2><strong>\u4fee\u6539\u6570\u7ec4\u4e2d\u7684\u5076\u6570\u5143\u7d20\u4e3a\u5176\u5e73\u65b9<\/strong><\/h2>\n<p>arr[arr % 2 == 0] = arr[arr % 2 == 0]  2<\/p>\n<h2><strong>\u5c06numpy\u6570\u7ec4\u8f6c\u6362\u56de\u5217\u8868<\/strong><\/h2>\n<p>my_list = arr.tolist()<\/p>\n<h2><strong>\u8f93\u51fa\u4fee\u6539\u540e\u7684\u5217\u8868<\/strong><\/h2>\n<p>print(my_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8f93\u51fa\u7ed3\u679c\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code>[1, 4, 3, 16, 5, 36]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>numpy<\/code>\u5e93\u5c06\u5076\u6570\u5143\u7d20\u4fee\u6539\u4e3a\u4e86\u5b83\u4eec\u7684\u5e73\u65b9\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528<code>pandas<\/code>\u5e93\u8fdb\u884c\u4fee\u6539<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u9700\u8981\u5904\u7406\u66f4\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u6216\u8fdb\u884c\u6570\u636e\u5206\u6790\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528<code>pandas<\/code>\u5e93\u3002<code>pandas<\/code>\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u64cd\u4f5c\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u5217\u8868<\/strong><\/h2>\n<p>my_list = [1, 2, 3, 4, 5, 6]<\/p>\n<h2><strong>\u5c06\u5217\u8868\u8f6c\u6362\u4e3apandas Series<\/strong><\/h2>\n<p>series = pd.Series(my_list)<\/p>\n<h2><strong>\u4fee\u6539Series\u4e2d\u7684\u5076\u6570\u5143\u7d20\u4e3a\u5176\u5e73\u65b9<\/strong><\/h2>\n<p>series[series % 2 == 0] = series[series % 2 == 0]  2<\/p>\n<h2><strong>\u5c06Series\u8f6c\u6362\u56de\u5217\u8868<\/strong><\/h2>\n<p>my_list = series.tolist()<\/p>\n<h2><strong>\u8f93\u51fa\u4fee\u6539\u540e\u7684\u5217\u8868<\/strong><\/h2>\n<p>print(my_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8f93\u51fa\u7ed3\u679c\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code>[1, 4, 3, 16, 5, 36]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>pandas<\/code>\u5e93\u5c06\u5076\u6570\u5143\u7d20\u4fee\u6539\u4e3a\u4e86\u5b83\u4eec\u7684\u5e73\u65b9\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u4f7f\u7528\u9012\u5f52\u4fee\u6539\u5217\u8868\u4e2d\u7684\u5143\u7d20<\/h3>\n<\/p>\n<p><p>\u9012\u5f52\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u7f16\u7a0b\u6280\u672f\uff0c\u53ef\u4ee5\u7528\u6765\u89e3\u51b3\u4e00\u4e9b\u590d\u6742\u7684\u95ee\u9898\u3002\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528\u9012\u5f52\u6765\u4fee\u6539\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e00\u4e2a\u9012\u5f52\u51fd\u6570\uff0c\u7528\u4e8e\u4fee\u6539\u5217\u8868\u4e2d\u7684\u5076\u6570\u5143\u7d20\u4e3a\u5176\u5e73\u65b9<\/p>\n<p>def modify_list(lst):<\/p>\n<p>    if not lst:<\/p>\n<p>        return []<\/p>\n<p>    head = lst[0]  2 if lst[0] % 2 == 0 else lst[0]<\/p>\n<p>    return [head] + modify_list(lst[1:])<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u5217\u8868<\/strong><\/h2>\n<p>my_list = [1, 2, 3, 4, 5, 6]<\/p>\n<h2><strong>\u4f7f\u7528\u9012\u5f52\u51fd\u6570\u4fee\u6539\u5217\u8868<\/strong><\/h2>\n<p>my_list = modify_list(my_list)<\/p>\n<h2><strong>\u8f93\u51fa\u4fee\u6539\u540e\u7684\u5217\u8868<\/strong><\/h2>\n<p>print(my_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8f93\u51fa\u7ed3\u679c\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code>[1, 4, 3, 16, 5, 36]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u9012\u5f52\u51fd\u6570\u5c06\u5076\u6570\u5143\u7d20\u4fee\u6539\u4e3a\u4e86\u5b83\u4eec\u7684\u5e73\u65b9\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u4f7f\u7528<code>itertools<\/code>\u6a21\u5757\u8fdb\u884c\u4fee\u6539<\/h3>\n<\/p>\n<p><p><code>itertools<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u6548\u7684\u8fed\u4ee3\u5668\u51fd\u6570\u3002\u6211\u4eec\u53ef\u4ee5\u7ed3\u5408<code>itertools<\/code>\u6a21\u5757\u548c\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u6765\u4fee\u6539\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import itertools<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u5217\u8868<\/strong><\/h2>\n<p>my_list = [1, 2, 3, 4, 5, 6]<\/p>\n<h2><strong>\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u548citertools.ch<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n\u4fee\u6539\u5217\u8868\u4e2d\u7684\u5076\u6570\u5143\u7d20<\/strong><\/h2>\n<p>my_list = list(itertools.chain((x2 if x % 2 == 0 else x for x in my_list)))<\/p>\n<h2><strong>\u8f93\u51fa\u4fee\u6539\u540e\u7684\u5217\u8868<\/strong><\/h2>\n<p>print(my_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8f93\u51fa\u7ed3\u679c\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code>[1, 4, 3, 16, 5, 36]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u7ed3\u5408\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u548c<code>itertools.chain<\/code>\u5c06\u5076\u6570\u5143\u7d20\u4fee\u6539\u4e3a\u4e86\u5b83\u4eec\u7684\u5e73\u65b9\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0a\u662f\u51e0\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\u6765\u4fee\u6539Python\u5217\u8868\u4e2d\u7684\u67d0\u4e2a\u5143\u7d20\u3002\u6839\u636e\u5177\u4f53\u7684\u9700\u6c42\u548c\u573a\u666f\uff0c\u4f60\u53ef\u4ee5\u9009\u62e9\u9002\u5408\u7684\u65b9\u6cd5\u6765\u64cd\u4f5c\u5217\u8868\u3002\u5e0c\u671b\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u5904\u7406Python\u4e2d\u7684\u5217\u8868\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bbf\u95ee\u5217\u8868\u7684\u7279\u5b9a\u5143\u7d20\u4ee5\u8fdb\u884c\u4fee\u6539\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u5217\u8868\u4e2d\u7684\u7279\u5b9a\u5143\u7d20\u3002\u7d22\u5f15\u4ece0\u5f00\u59cb\uff0c\u56e0\u6b64\u8981\u4fee\u6539\u5217\u8868\u4e2d\u7b2cn\u4e2a\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528<code>list[n]<\/code>\u7684\u65b9\u5f0f\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u5217\u8868<code>my_list = [1, 2, 3, 4]<\/code>\uff0c\u8981\u5c06\u7b2c\u4e8c\u4e2a\u5143\u7d20\u4fee\u6539\u4e3a5\uff0c\u53ef\u4ee5\u4f7f\u7528<code>my_list[1] = 5<\/code>\u3002\u8fd9\u6837\uff0c<code>my_list<\/code>\u5c06\u53d8\u4e3a<code>[1, 5, 3, 4]<\/code>\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5b89\u5168\u5730\u4fee\u6539\u5217\u8868\u5143\u7d20\u4ee5\u907f\u514d\u7d22\u5f15\u9519\u8bef\uff1f<\/strong><br \/>\u5728\u4fee\u6539\u5217\u8868\u5143\u7d20\u65f6\uff0c\u786e\u4fdd\u7d22\u5f15\u5728\u5217\u8868\u7684\u6709\u6548\u8303\u56f4\u5185\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002\u53ef\u4ee5\u4f7f\u7528<code>len()<\/code>\u51fd\u6570\u6765\u68c0\u67e5\u5217\u8868\u7684\u957f\u5ea6\uff0c\u786e\u4fdd\u8981\u8bbf\u95ee\u7684\u7d22\u5f15\u5c0f\u4e8e\u5217\u8868\u957f\u5ea6\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>if n &lt; len(my_list):<\/code>\u6765\u786e\u8ba4\u7d22\u5f15n\u662f\u5426\u6709\u6548\u3002\u8fd9\u6837\u53ef\u4ee5\u907f\u514d<code>IndexError<\/code>\uff0c\u786e\u4fdd\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528\u5faa\u73af\u6216\u6761\u4ef6\u8bed\u53e5\u6279\u91cf\u4fee\u6539\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u904d\u5386\u5217\u8868\u5e76\u6839\u636e\u7279\u5b9a\u6761\u4ef6\u4fee\u6539\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60f3\u8981\u5c06\u5217\u8868\u4e2d\u6240\u6709\u5076\u6570\u5143\u7d20\u4e58\u4ee52\uff0c\u53ef\u4ee5\u8fd9\u6837\u5b9e\u73b0\uff1a  <\/p>\n<pre><code class=\"language-python\">for i in range(len(my_list)):\n    if my_list[i] % 2 == 0:\n        my_list[i] *= 2\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u7075\u6d3b\u5730\u6839\u636e\u6761\u4ef6\u6279\u91cf\u5904\u7406\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff0c\u9002\u7528\u4e8e\u591a\u79cd\u573a\u666f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u4fee\u6539\u5217\u8868\u4e2d\u7684\u67d0\u4e2a\u5143\u7d20\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u4f8b\u5982\u4f7f\u7528\u7d22\u5f15\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u5faa\u73af\u904d\u5386\u7b49\u3002\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u7d22\u5f15\u76f4 [&hellip;]","protected":false},"author":3,"featured_media":1099185,"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\/1099177"}],"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=1099177"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1099177\/revisions"}],"predecessor-version":[{"id":1099186,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1099177\/revisions\/1099186"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1099185"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1099177"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1099177"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1099177"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}