{"id":1098647,"date":"2025-01-08T15:23:06","date_gmt":"2025-01-08T07:23:06","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1098647.html"},"modified":"2025-01-08T15:23:09","modified_gmt":"2025-01-08T07:23:09","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e6%9b%bf%e6%8d%a2%e5%88%97%e8%a1%a8%e4%b8%ad%e7%9a%84%e5%85%83%e7%b4%a0-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1098647.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u66ff\u6362\u5217\u8868\u4e2d\u7684\u5143\u7d20"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25062727\/e4872e33-fe48-46e2-a005-9b3f66c51c85.webp\" alt=\"python\u4e2d\u5982\u4f55\u66ff\u6362\u5217\u8868\u4e2d\u7684\u5143\u7d20\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u66ff\u6362\u5217\u8868\u4e2d\u7684\u5143\u7d20\u53ef\u4ee5\u4f7f\u7528\u7d22\u5f15\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001map\u51fd\u6570\u7b49\u65b9\u6cd5\u3002<\/strong><br \/>\u5176\u4e2d\uff0c\u7d22\u5f15\u65b9\u6cd5\u6700\u4e3a\u76f4\u63a5\uff0c\u901a\u8fc7\u6307\u5b9a\u5143\u7d20\u7684\u4f4d\u7f6e\u8fdb\u884c\u66ff\u6362\uff0c\u9002\u7528\u4e8e\u5df2\u77e5\u5143\u7d20\u4f4d\u7f6e\u7684\u60c5\u51b5\u3002\u63a5\u4e0b\u6765\u5c06\u8be6\u7ec6\u63cf\u8ff0\u7d22\u5f15\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u4f7f\u7528\u7d22\u5f15\u66ff\u6362\u5217\u8868\u4e2d\u7684\u5143\u7d20\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u76f4\u63a5\u6307\u5b9a\u5217\u8868\u4e2d\u5143\u7d20\u7684\u4f4d\u7f6e\u6765\u8fdb\u884c\u66ff\u6362\u3002\u4f8b\u5982\uff0c\u5982\u679c\u8981\u5c06\u5217\u8868\u4e2d\u7b2c\u4e8c\u4e2a\u4f4d\u7f6e\u7684\u5143\u7d20\u66ff\u6362\u4e3a\u65b0\u7684\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>my_list[1] = 10<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 10, 3, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u9ad8\u6548\uff0c\u5f53\u6211\u4eec\u77e5\u9053\u5177\u4f53\u8981\u66ff\u6362\u5143\u7d20\u7684\u4f4d\u7f6e\u65f6\uff0c\u975e\u5e38\u65b9\u4fbf\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u7d22\u5f15\u66ff\u6362\u5143\u7d20<\/h3>\n<\/p>\n<p><p>\u5728Python\u5217\u8868\u4e2d\uff0c\u6bcf\u4e2a\u5143\u7d20\u90fd\u6709\u4e00\u4e2a\u7d22\u5f15\uff0c\u901a\u8fc7\u7d22\u5f15\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u548c\u4fee\u6539\u5143\u7d20\u3002\u7d22\u5f15\u4ece0\u5f00\u59cb\uff0c\u56e0\u6b64\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\u662f0\uff0c\u7b2c\u4e8c\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\u662f1\uff0c\u4f9d\u6b64\u7c7b\u63a8\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u5217\u8868<\/p>\n<p>fruits = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;, &#39;date&#39;, &#39;elderberry&#39;]<\/p>\n<h2><strong>\u4f7f\u7528\u7d22\u5f15\u66ff\u6362\u7b2c\u4e8c\u4e2a\u5143\u7d20<\/strong><\/h2>\n<p>fruits[1] = &#39;blueberry&#39;<\/p>\n<p>print(fruits)  # \u8f93\u51fa: [&#39;apple&#39;, &#39;blueberry&#39;, &#39;cherry&#39;, &#39;date&#39;, &#39;elderberry&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u9762\u7684\u4ee3\u7801\uff0c\u6211\u4eec\u5c06\u5217\u8868 <code>fruits<\/code> \u4e2d\u7b2c\u4e8c\u4e2a\u5143\u7d20 <code>banana<\/code> \u66ff\u6362\u4e3a <code>blueberry<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u6211\u4eec\u77e5\u9053\u5177\u4f53\u4f4d\u7f6e\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u65b0\u7684\u5217\u8868\u6216\u4fee\u6539\u73b0\u6709\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u66ff\u6362\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u5217\u8868<\/p>\n<p>numbers = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u66ff\u6362\u6240\u6709\u7684\u5076\u6570\u4e3a\u5b83\u4eec\u7684\u5e73\u65b9<\/strong><\/h2>\n<p>squared_evens = [x2 if x % 2 == 0 else x for x in numbers]<\/p>\n<p>print(squared_evens)  # \u8f93\u51fa: [1, 4, 3, 16, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u5c06 <code>numbers<\/code> \u5217\u8868\u4e2d\u7684\u6240\u6709\u5076\u6570\u66ff\u6362\u4e3a\u5b83\u4eec\u7684\u5e73\u65b9\uff0c\u800c\u5176\u4ed6\u5143\u7d20\u4fdd\u6301\u4e0d\u53d8\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528map\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>map<\/code> \u51fd\u6570\u662fPython\u5185\u7f6e\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u53ef\u4ee5\u5c06\u6307\u5b9a\u7684\u51fd\u6570\u5e94\u7528\u4e8e\u4e00\u4e2a\u6216\u591a\u4e2a\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>map<\/code> \u51fd\u6570\u6765\u66ff\u6362\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u5217\u8868<\/p>\n<p>numbers = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u4f7f\u7528 map \u51fd\u6570\u66ff\u6362\u6240\u6709\u7684\u5076\u6570\u4e3a\u5b83\u4eec\u7684\u5e73\u65b9<\/strong><\/h2>\n<p>def replace_even(x):<\/p>\n<p>    return x2 if x % 2 == 0 else x<\/p>\n<p>squared_evens = list(map(replace_even, numbers))<\/p>\n<p>print(squared_evens)  # \u8f93\u51fa: [1, 4, 3, 16, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u9762\u7684\u4ee3\u7801\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570 <code>replace_even<\/code>\uff0c\u5b83\u5c06\u5076\u6570\u66ff\u6362\u4e3a\u5b83\u4eec\u7684\u5e73\u65b9\uff0c\u7136\u540e\u4f7f\u7528 <code>map<\/code> \u51fd\u6570\u5c06\u8fd9\u4e2a\u51fd\u6570\u5e94\u7528\u5230 <code>numbers<\/code> \u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528for\u5faa\u73af<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528 <code>for<\/code> \u5faa\u73af\u6765\u904d\u5386\u5217\u8868\uff0c\u5e76\u6839\u636e\u6761\u4ef6\u66ff\u6362\u5143\u7d20\u3002\u867d\u7136\u8fd9\u79cd\u65b9\u6cd5\u53ef\u80fd\u4e0d\u5982\u5217\u8868\u63a8\u5bfc\u5f0f\u6216 <code>map<\/code> \u51fd\u6570\u7b80\u6d01\uff0c\u4f46\u5b83\u975e\u5e38\u76f4\u89c2\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u5217\u8868<\/p>\n<p>numbers = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u4f7f\u7528 for \u5faa\u73af\u66ff\u6362\u6240\u6709\u7684\u5076\u6570\u4e3a\u5b83\u4eec\u7684\u5e73\u65b9<\/strong><\/h2>\n<p>for i in range(len(numbers)):<\/p>\n<p>    if numbers[i] % 2 == 0:<\/p>\n<p>        numbers[i] = numbers[i]2<\/p>\n<p>print(numbers)  # \u8f93\u51fa: [1, 4, 3, 16, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>for<\/code> \u5faa\u73af\u904d\u5386 <code>numbers<\/code> \u5217\u8868\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5e76\u68c0\u67e5\u5b83\u662f\u5426\u662f\u5076\u6570\u3002\u5982\u679c\u662f\u5076\u6570\uff0c\u5219\u5c06\u5176\u66ff\u6362\u4e3a\u5e73\u65b9\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u66ff\u6362\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u5143\u7d20<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u6211\u4eec\u53ef\u80fd\u9700\u8981\u66ff\u6362\u5217\u8868\u4e2d\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u5143\u7d20\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u5224\u65ad\u7ed3\u5408\u4e0a\u8ff0\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u5217\u8868<\/p>\n<p>animals = [&#39;cat&#39;, &#39;dog&#39;, &#39;elephant&#39;, &#39;tiger&#39;, &#39;lion&#39;]<\/p>\n<h2><strong>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u66ff\u6362\u6240\u6709\u5305\u542b\u5b57\u6bcd &#39;e&#39; \u7684\u52a8\u7269\u540d\u79f0<\/strong><\/h2>\n<p>replaced_animals = [&#39;hippo&#39; if &#39;e&#39; in animal else animal for animal in animals]<\/p>\n<p>print(replaced_animals)  # \u8f93\u51fa: [&#39;cat&#39;, &#39;dog&#39;, &#39;hippo&#39;, &#39;tiger&#39;, &#39;lion&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u66ff\u6362\u4e86 <code>animals<\/code> \u5217\u8868\u4e2d\u6240\u6709\u5305\u542b\u5b57\u6bcd &#39;e&#39; \u7684\u52a8\u7269\u540d\u79f0\uff0c\u5c06\u5b83\u4eec\u66ff\u6362\u4e3a &#39;hippo&#39;\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5728\u51fd\u6570\u4e2d\u66ff\u6362\u5143\u7d20<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u5c06\u8fd9\u4e9b\u64cd\u4f5c\u5c01\u88c5\u5230\u51fd\u6570\u4e2d\uff0c\u4ee5\u4fbf\u5728\u591a\u4e2a\u5730\u65b9\u91cd\u590d\u4f7f\u7528\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\uff0c\u6f14\u793a\u5982\u4f55\u5728\u51fd\u6570\u4e2d\u66ff\u6362\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def replace_elements(lst, condition, new_value):<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    \u66ff\u6362\u5217\u8868\u4e2d\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u5143\u7d20\u3002<\/p>\n<p>    :param lst: \u8981\u64cd\u4f5c\u7684\u5217\u8868<\/p>\n<p>    :param condition: \u66ff\u6362\u6761\u4ef6\u7684\u51fd\u6570<\/p>\n<p>    :param new_value: \u65b0\u503c<\/p>\n<p>    :return: \u66ff\u6362\u540e\u7684\u5217\u8868<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    return [new_value if condition(x) else x for x in lst]<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u5217\u8868<\/strong><\/h2>\n<p>numbers = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u5b9a\u4e49\u66ff\u6362\u6761\u4ef6\u51fd\u6570<\/strong><\/h2>\n<p>def is_even(x):<\/p>\n<p>    return x % 2 == 0<\/p>\n<h2><strong>\u66ff\u6362\u5217\u8868\u4e2d\u6240\u6709\u7684\u5076\u6570\u4e3a 0<\/strong><\/h2>\n<p>new_numbers = replace_elements(numbers, is_even, 0)<\/p>\n<p>print(new_numbers)  # \u8f93\u51fa: [1, 0, 3, 0, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u901a\u7528\u51fd\u6570 <code>replace_elements<\/code>\uff0c\u5b83\u63a5\u53d7\u4e00\u4e2a\u5217\u8868\u3001\u4e00\u4e2a\u66ff\u6362\u6761\u4ef6\u51fd\u6570\u548c\u4e00\u4e2a\u65b0\u503c\u4f5c\u4e3a\u53c2\u6570\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528\u8fd9\u4e2a\u51fd\u6570\u5c06 <code>numbers<\/code> \u5217\u8868\u4e2d\u6240\u6709\u5076\u6570\u66ff\u6362\u4e3a 0\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u66ff\u6362\u591a\u4e2a\u5143\u7d20<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u6211\u4eec\u53ef\u80fd\u9700\u8981\u4e00\u6b21\u66ff\u6362\u591a\u4e2a\u5143\u7d20\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u6620\u5c04\u5b57\u5178\uff0c\u5c06\u65e7\u503c\u6620\u5c04\u5230\u65b0\u503c\uff0c\u7136\u540e\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6216 <code>map<\/code> \u51fd\u6570\u8fdb\u884c\u66ff\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u5217\u8868<\/p>\n<p>fruits = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;, &#39;date&#39;, &#39;elderberry&#39;]<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u6620\u5c04\u5b57\u5178<\/strong><\/h2>\n<p>replacement_dict = {&#39;banana&#39;: &#39;blueberry&#39;, &#39;date&#39;: &#39;dragonfruit&#39;}<\/p>\n<h2><strong>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u66ff\u6362\u591a\u4e2a\u5143\u7d20<\/strong><\/h2>\n<p>replaced_fruits = [replacement_dict.get(fruit, fruit) for fruit in fruits]<\/p>\n<p>print(replaced_fruits)  # \u8f93\u51fa: [&#39;apple&#39;, &#39;blueberry&#39;, &#39;cherry&#39;, &#39;dragonfruit&#39;, &#39;elderberry&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u6620\u5c04\u5b57\u5178 <code>replacement_dict<\/code>\uff0c\u7136\u540e\u4f7f\u7528 <code>replacement_dict.get(fruit, fruit)<\/code> \u6765\u66ff\u6362 <code>fruits<\/code> \u5217\u8868\u4e2d\u5b58\u5728\u4e8e\u5b57\u5178\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u66ff\u6362\u5d4c\u5957\u5217\u8868\u4e2d\u7684\u5143\u7d20<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5d4c\u5957\u5217\u8868\uff08\u5217\u8868\u4e2d\u7684\u5217\u8868\uff09\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u9012\u5f52\u51fd\u6570\u6765\u66ff\u6362\u5143\u7d20\u3002\u9012\u5f52\u51fd\u6570\u4f1a\u904d\u5386\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5982\u679c\u5143\u7d20\u662f\u5217\u8868\uff0c\u5219\u9012\u5f52\u8c03\u7528\u81ea\u8eab\u8fdb\u884c\u8fdb\u4e00\u6b65\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def replace_nested_elements(lst, old_value, new_value):<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    \u66ff\u6362\u5d4c\u5957\u5217\u8868\u4e2d\u6240\u6709\u5339\u914d\u7684\u5143\u7d20\u3002<\/p>\n<p>    :param lst: \u8981\u64cd\u4f5c\u7684\u5d4c\u5957\u5217\u8868<\/p>\n<p>    :param old_value: \u8981\u66ff\u6362\u7684\u65e7\u503c<\/p>\n<p>    :param new_value: \u65b0\u503c<\/p>\n<p>    :return: \u66ff\u6362\u540e\u7684\u5d4c\u5957\u5217\u8868<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    for i in range(len(lst)):<\/p>\n<p>        if isinstance(lst[i], list):<\/p>\n<p>            replace_nested_elements(lst[i], old_value, new_value)<\/p>\n<p>        elif lst[i] == old_value:<\/p>\n<p>            lst[i] = new_value<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5d4c\u5957\u5217\u8868<\/strong><\/h2>\n<p>nested_list = [1, [2, 3], [4, [5, 6]], 7]<\/p>\n<h2><strong>\u66ff\u6362\u5d4c\u5957\u5217\u8868\u4e2d\u6240\u6709\u7684 3 \u4e3a 0<\/strong><\/h2>\n<p>replace_nested_elements(nested_list, 3, 0)<\/p>\n<p>print(nested_list)  # \u8f93\u51fa: [1, [2, 0], [4, [5, 6]], 7]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u9012\u5f52\u51fd\u6570 <code>replace_nested_elements<\/code>\uff0c\u5b83\u4f1a\u904d\u5386\u5d4c\u5957\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5e76\u66ff\u6362\u6240\u6709\u5339\u914d <code>old_value<\/code> \u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u4f7f\u7528numpy\u6570\u7ec4\u66ff\u6362\u5143\u7d20<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0c<code>numpy<\/code> \u6570\u7ec4\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u66ff\u6362\u5143\u7d20\u7684\u65b9\u6cd5\u3002\u5982\u679c\u4f60\u6b63\u5728\u5904\u7406\u5927\u91cf\u6570\u503c\u6570\u636e\uff0c\u5efa\u8bae\u4f7f\u7528 <code>numpy<\/code> \u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a numpy \u6570\u7ec4<\/strong><\/h2>\n<p>arr = np.array([1, 2, 3, 4, 5])<\/p>\n<h2><strong>\u4f7f\u7528 numpy \u6570\u7ec4\u7684\u7d22\u5f15\u66ff\u6362\u5143\u7d20<\/strong><\/h2>\n<p>arr[arr % 2 == 0] = 0<\/p>\n<p>print(arr)  # \u8f93\u51fa: [1 0 3 0 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a <code>numpy<\/code> \u6570\u7ec4 <code>arr<\/code>\uff0c\u5e76\u4f7f\u7528\u5e03\u5c14\u7d22\u5f15\u5c06\u6240\u6709\u5076\u6570\u66ff\u6362\u4e3a 0\u3002<code>numpy<\/code> \u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u7d22\u5f15\u548c\u5207\u7247\u529f\u80fd\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u8fdb\u884c\u6570\u7ec4\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u66ff\u6362Python\u5217\u8868\u4e2d\u7684\u5143\u7d20\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u7d22\u5f15\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001<code>map<\/code> \u51fd\u6570\u3001<code>for<\/code> \u5faa\u73af\u3001\u6761\u4ef6\u5224\u65ad\u3001\u6620\u5c04\u5b57\u5178\u3001\u9012\u5f52\u51fd\u6570\u548c <code>numpy<\/code> \u6570\u7ec4\u3002\u6839\u636e\u5177\u4f53\u60c5\u51b5\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6267\u884c\u6548\u7387\u3002\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u7075\u6d3b\u5730\u64cd\u4f5c\u5217\u8868\uff0c\u6ee1\u8db3\u5404\u79cd\u66ff\u6362\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u66ff\u6362\u5217\u8868\u4e2d\u7684\u7279\u5b9a\u5143\u7d20\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u76f4\u63a5\u8bbf\u95ee\u5e76\u66ff\u6362\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002\u53ea\u9700\u6307\u5b9a\u8981\u66ff\u6362\u7684\u5143\u7d20\u4f4d\u7f6e\uff0c\u7136\u540e\u5c06\u5176\u8d4b\u503c\u4e3a\u65b0\u503c\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6709\u4e00\u4e2a\u5217\u8868<code>my_list = [1, 2, 3, 4]<\/code>\uff0c\u8981\u5c06\u7b2c\u4e8c\u4e2a\u5143\u7d20<code>2<\/code>\u66ff\u6362\u4e3a<code>5<\/code>\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>\u6211\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u65b9\u6cd5\u6765\u66ff\u6362\u5217\u8868\u4e2d\u6240\u6709\u7279\u5b9a\u7684\u5143\u7d20\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u6216\u5faa\u73af\u7ed3\u5408\u6761\u4ef6\u5224\u65ad\u6765\u66ff\u6362\u5217\u8868\u4e2d\u6240\u6709\u7279\u5b9a\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60f3\u5c06\u5217\u8868<code>my_list = [1, 2, 2, 3]<\/code>\u4e2d\u7684\u6240\u6709<code>2<\/code>\u66ff\u6362\u4e3a<code>5<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u5982\u4e0b\u6240\u793a\uff1a<code>my_list = [5 if x == 2 else x for x in my_list]<\/code>\u3002\u8fd9\u6837\uff0c<code>my_list<\/code>\u7684\u5185\u5bb9\u5c06\u66f4\u65b0\u4e3a<code>[1, 5, 5, 3]<\/code>\u3002<\/p>\n<p><strong>\u5728\u66ff\u6362\u5217\u8868\u5143\u7d20\u65f6\uff0c\u6709\u4ec0\u4e48\u9700\u8981\u6ce8\u610f\u7684\u5730\u65b9\u5417\uff1f<\/strong><br \/>\u5728\u66ff\u6362\u5217\u8868\u5143\u7d20\u65f6\uff0c\u786e\u4fdd\u8003\u8651\u5230\u7d22\u5f15\u8303\u56f4\u3002\u5982\u679c\u5c1d\u8bd5\u8bbf\u95ee\u4e0d\u5b58\u5728\u7684\u7d22\u5f15\uff0c\u5c06\u5f15\u53d1<code>IndexError<\/code>\u5f02\u5e38\u3002\u6b64\u5916\uff0c\u5982\u679c\u66ff\u6362\u7684\u5143\u7d20\u9700\u8981\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u8868\u8fbe\u5f0f\u6765\u786e\u4fdd\u66ff\u6362\u7684\u51c6\u786e\u6027\u3002\u6ce8\u610f\uff0c\u76f4\u63a5\u4fee\u6539\u5217\u8868\u4f1a\u6539\u53d8\u539f\u59cb\u6570\u636e\uff0c\u5982\u679c\u9700\u8981\u4fdd\u7559\u539f\u59cb\u5217\u8868\uff0c\u53ef\u4ee5\u5148\u590d\u5236\u5217\u8868\u518d\u8fdb\u884c\u66ff\u6362\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u66ff\u6362\u5217\u8868\u4e2d\u7684\u5143\u7d20\u53ef\u4ee5\u4f7f\u7528\u7d22\u5f15\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001map\u51fd\u6570\u7b49\u65b9\u6cd5\u3002\u5176\u4e2d\uff0c\u7d22\u5f15\u65b9\u6cd5\u6700\u4e3a\u76f4\u63a5\uff0c\u901a\u8fc7\u6307\u5b9a [&hellip;]","protected":false},"author":3,"featured_media":1098656,"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\/1098647"}],"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=1098647"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1098647\/revisions"}],"predecessor-version":[{"id":1098660,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1098647\/revisions\/1098660"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1098656"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1098647"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1098647"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1098647"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}