{"id":1159985,"date":"2025-01-13T18:59:35","date_gmt":"2025-01-13T10:59:35","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1159985.html"},"modified":"2025-01-13T18:59:37","modified_gmt":"2025-01-13T10:59:37","slug":"%e5%a6%82%e4%bd%95%e4%bf%ae%e6%94%b9%e5%88%97%e8%a1%a8%e5%a4%9a%e8%a1%8c%e5%80%bcpython","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1159985.html","title":{"rendered":"\u5982\u4f55\u4fee\u6539\u5217\u8868\u591a\u884c\u503cpython"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25201546\/13170eee-f92a-469f-ac3d-04aa6e5f1e91.webp\" alt=\"\u5982\u4f55\u4fee\u6539\u5217\u8868\u591a\u884c\u503cpython\" \/><\/p>\n<p><p> <strong>\u5982\u4f55\u4fee\u6539\u5217\u8868\u591a\u884c\u503cpython<\/strong>\uff1a<strong>\u4f7f\u7528\u7d22\u5f15\u76f4\u63a5\u4fee\u6539\u3001\u901a\u8fc7\u5faa\u73af\u4fee\u6539\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u4f7f\u7528map\u51fd\u6570\u3001\u901a\u8fc7\u6761\u4ef6\u5224\u65ad\u4fee\u6539<\/strong>\u3002\u5176\u4e2d\uff0c\u901a\u8fc7\u5faa\u73af\u4fee\u6539\u662f\u4e00\u79cd\u5e38\u89c1\u4e14\u7075\u6d3b\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u5faa\u73af\u4fee\u6539\u5217\u8868\u7684\u591a\u884c\u503c\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528for\u5faa\u73af\u904d\u5386\u5217\u8868\u5e76\u5bf9\u6bcf\u4e00\u4e2a\u5143\u7d20\u8fdb\u884c\u5904\u7406\u3002\u5177\u4f53\u64cd\u4f5c\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868<\/p>\n<p>my_list = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u4f7f\u7528for\u5faa\u73af\u4fee\u6539\u6bcf\u4e2a\u5143\u7d20<\/strong><\/h2>\n<p>for i in range(len(my_list)):<\/p>\n<p>    my_list[i] = my_list[i] * 2  # \u5c06\u6bcf\u4e2a\u5143\u7d20\u90fd\u4e58\u4ee52<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [2, 4, 6, 8, 10]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u64cd\u4f5c\u7075\u6d3b\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u5bf9\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u4e0d\u540c\u7684\u5904\u7406\uff0c\u4e5f\u53ef\u4ee5\u6839\u636e\u67d0\u4e9b\u6761\u4ef6\u6709\u9009\u62e9\u5730\u4fee\u6539\u5143\u7d20\u503c\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u7d22\u5f15\u76f4\u63a5\u4fee\u6539<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u7d22\u5f15\u76f4\u63a5\u4fee\u6539\u662f\u6700\u7b80\u5355\u7684\u65b9\u5f0f\u3002\u901a\u8fc7\u7d22\u5f15\u76f4\u63a5\u8bbf\u95ee\u5e76\u4fee\u6539\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002\u8be5\u65b9\u6cd5\u9002\u5408\u5728\u77e5\u9053\u7279\u5b9a\u7d22\u5f15\u4f4d\u7f6e\u65f6\u8fdb\u884c\u5355\u4e2a\u6216\u5c11\u91cf\u5143\u7d20\u4fee\u6539\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868<\/p>\n<p>my_list = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u4fee\u6539\u7279\u5b9a\u7d22\u5f15\u4f4d\u7f6e\u7684\u503c<\/strong><\/h2>\n<p>my_list[2] = 10  # \u5c06\u7d22\u5f152\u7684\u503c\u6539\u4e3a10<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 2, 10, 4, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5728\u77e5\u9053\u7279\u5b9a\u7d22\u5f15\u4f4d\u7f6e\u65f6\u8fdb\u884c\u5355\u4e2a\u6216\u5c11\u91cf\u5143\u7d20\u4fee\u6539\u7684\u60c5\u51b5\uff0c\u4f46\u4e0d\u9002\u5408\u5927\u89c4\u6a21\u6216\u590d\u6742\u7684\u4fee\u6539\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u901a\u8fc7\u5faa\u73af\u4fee\u6539<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7for\u5faa\u73af\u904d\u5386\u5217\u8868\u5e76\u5bf9\u6bcf\u4e00\u4e2a\u5143\u7d20\u8fdb\u884c\u5904\u7406\uff0c\u662f\u4e00\u79cd\u5e38\u89c1\u4e14\u7075\u6d3b\u7684\u65b9\u6cd5\u3002\u53ef\u4ee5\u6839\u636e\u9700\u8981\u5bf9\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u4e0d\u540c\u7684\u5904\u7406\uff0c\u4e5f\u53ef\u4ee5\u6839\u636e\u67d0\u4e9b\u6761\u4ef6\u6709\u9009\u62e9\u5730\u4fee\u6539\u5143\u7d20\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868<\/p>\n<p>my_list = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u4f7f\u7528for\u5faa\u73af\u4fee\u6539\u6bcf\u4e2a\u5143\u7d20<\/strong><\/h2>\n<p>for i in range(len(my_list)):<\/p>\n<p>    my_list[i] = my_list[i] * 2  # \u5c06\u6bcf\u4e2a\u5143\u7d20\u90fd\u4e58\u4ee52<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [2, 4, 6, 8, 10]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5faa\u73af\u4fee\u6539\u7684\u597d\u5904\u662f\u53ef\u4ee5\u6839\u636e\u6761\u4ef6\u5224\u65ad\u8fdb\u884c\u9009\u62e9\u6027\u4fee\u6539\u3002\u4f8b\u5982\uff0c\u53ea\u4fee\u6539\u5076\u6570\u4f4d\u7f6e\u7684\u5143\u7d20\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868<\/p>\n<p>my_list = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u4f7f\u7528for\u5faa\u73af\u4fee\u6539\u6bcf\u4e2a\u5143\u7d20<\/strong><\/h2>\n<p>for i in range(len(my_list)):<\/p>\n<p>    if i % 2 == 0:  # \u53ea\u4fee\u6539\u5076\u6570\u4f4d\u7f6e\u7684\u5143\u7d20<\/p>\n<p>        my_list[i] = my_list[i] * 2<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [2, 2, 6, 4, 10]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u4fee\u6539\u5217\u8868\u7684\u65b9\u6cd5\uff0c\u9002\u5408\u5bf9\u5217\u8868\u8fdb\u884c\u6279\u91cf\u4fee\u6539\u3002\u53ef\u4ee5\u5728\u4e00\u884c\u4ee3\u7801\u4e2d\u5b8c\u6210\u590d\u6742\u7684\u4fee\u6539\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868<\/p>\n<p>my_list = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u4fee\u6539\u6bcf\u4e2a\u5143\u7d20<\/strong><\/h2>\n<p>my_list = [x * 2 for x in my_list]  # \u5c06\u6bcf\u4e2a\u5143\u7d20\u90fd\u4e58\u4ee52<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [2, 4, 6, 8, 10]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u4e0d\u4ec5\u7b80\u6d01\u660e\u4e86\uff0c\u800c\u4e14\u5728\u5904\u7406\u8f83\u5927\u6570\u636e\u96c6\u65f6\u6027\u80fd\u8f83\u597d\u3002\u53ef\u4ee5\u7ed3\u5408\u6761\u4ef6\u5224\u65ad\u8fdb\u884c\u9009\u62e9\u6027\u4fee\u6539\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868<\/p>\n<p>my_list = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u4fee\u6539\u6bcf\u4e2a\u5143\u7d20<\/strong><\/h2>\n<p>my_list = [x * 2 if x % 2 == 0 else x for x in my_list]  # \u53ea\u4fee\u6539\u5076\u6570\u5143\u7d20<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 4, 3, 8, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528map\u51fd\u6570<\/h3>\n<\/p>\n<p><p>map\u51fd\u6570\u53ef\u4ee5\u5c06\u4e00\u4e2a\u51fd\u6570\u5e94\u7528\u5230\u4e00\u4e2a\u5217\u8868\u7684\u6bcf\u4e2a\u5143\u7d20\u4e0a\u3002\u9002\u5408\u9700\u8981\u5bf9\u5217\u8868\u5143\u7d20\u8fdb\u884c\u7edf\u4e00\u5904\u7406\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868<\/p>\n<p>my_list = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u4f7f\u7528map\u51fd\u6570\u4fee\u6539\u6bcf\u4e2a\u5143\u7d20<\/strong><\/h2>\n<p>my_list = list(map(lambda x: x * 2, my_list))  # \u5c06\u6bcf\u4e2a\u5143\u7d20\u90fd\u4e58\u4ee52<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [2, 4, 6, 8, 10]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>map\u51fd\u6570\u7684\u4f18\u70b9\u662f\u7b80\u6d01\u6613\u8bfb\uff0c\u4f46\u5728\u9700\u8981\u590d\u6742\u6761\u4ef6\u5224\u65ad\u65f6\u4e0d\u5982for\u5faa\u73af\u548c\u5217\u8868\u63a8\u5bfc\u5f0f\u7075\u6d3b\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u901a\u8fc7\u6761\u4ef6\u5224\u65ad\u4fee\u6539<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\u9700\u8981\u6839\u636e\u7279\u5b9a\u6761\u4ef6\u6765\u4fee\u6539\u5217\u8868\u7684\u5143\u7d20\u503c\uff0c\u53ef\u4ee5\u7ed3\u5408if\u6761\u4ef6\u5224\u65ad\u8fdb\u884c\u9009\u62e9\u6027\u4fee\u6539\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868<\/p>\n<p>my_list = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u4f7f\u7528for\u5faa\u73af\u548c\u6761\u4ef6\u5224\u65ad\u4fee\u6539\u5143\u7d20<\/strong><\/h2>\n<p>for i in range(len(my_list)):<\/p>\n<p>    if my_list[i] % 2 == 0:  # \u53ea\u4fee\u6539\u5076\u6570\u5143\u7d20<\/p>\n<p>        my_list[i] = my_list[i] * 2<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 4, 3, 8, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6761\u4ef6\u5224\u65ad\u53ef\u4ee5\u7ed3\u5408\u524d\u9762\u63d0\u5230\u7684for\u5faa\u73af\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u7b49\u65b9\u6cd5\u4f7f\u7528\uff0c\u4f7f\u5f97\u4fee\u6539\u64cd\u4f5c\u66f4\u52a0\u7075\u6d3b\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u6279\u91cf\u66ff\u6362<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\u9700\u8981\u5c06\u5217\u8868\u4e2d\u7684\u67d0\u4e9b\u503c\u6279\u91cf\u66ff\u6362\u4e3a\u65b0\u7684\u503c\uff0c\u53ef\u4ee5\u901a\u8fc7\u904d\u5386\u5217\u8868\u5e76\u4f7f\u7528\u6761\u4ef6\u5224\u65ad\u8fdb\u884c\u6279\u91cf\u66ff\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868<\/p>\n<p>my_list = [1, 2, 3, 4, 5, 2, 2]<\/p>\n<h2><strong>\u6279\u91cf\u66ff\u6362\u5217\u8868\u4e2d\u7684\u67d0\u4e9b\u503c<\/strong><\/h2>\n<p>value_to_replace = 2<\/p>\n<p>new_value = 10<\/p>\n<p>for i in range(len(my_list)):<\/p>\n<p>    if my_list[i] == value_to_replace:<\/p>\n<p>        my_list[i] = new_value<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 10, 3, 4, 5, 10, 10]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6279\u91cf\u66ff\u6362\u9002\u7528\u4e8e\u9700\u8981\u5c06\u5217\u8868\u4e2d\u7684\u67d0\u4e9b\u7279\u5b9a\u503c\u5168\u90e8\u66ff\u6362\u4e3a\u65b0\u503c\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528enumerate<\/h3>\n<\/p>\n<p><p>\u5728\u904d\u5386\u5217\u8868\u65f6\uff0c\u4f7f\u7528enumerate\u53ef\u4ee5\u540c\u65f6\u83b7\u53d6\u5143\u7d20\u7684\u7d22\u5f15\u548c\u503c\uff0c\u65b9\u4fbf\u8fdb\u884c\u590d\u6742\u7684\u4fee\u6539\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868<\/p>\n<p>my_list = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u4f7f\u7528enumerate\u904d\u5386\u5217\u8868<\/strong><\/h2>\n<p>for index, value in enumerate(my_list):<\/p>\n<p>    if value % 2 == 0:  # \u53ea\u4fee\u6539\u5076\u6570\u5143\u7d20<\/p>\n<p>        my_list[index] = value * 2<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 4, 3, 8, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>enumerate\u53ef\u4ee5\u8ba9\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\uff0c\u540c\u65f6\u907f\u514d\u624b\u52a8\u7ba1\u7406\u7d22\u5f15\uff0c\u6709\u52a9\u4e8e\u51cf\u5c11\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u5d4c\u5957\u5217\u8868\u7684\u4fee\u6539<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u5217\u8868\u4e2d\u5305\u542b\u5d4c\u5957\u5217\u8868\uff0c\u53ef\u4ee5\u901a\u8fc7\u9012\u5f52\u6216\u5d4c\u5957\u5faa\u73af\u8fdb\u884c\u4fee\u6539\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521d\u59cb\u5316\u4e00\u4e2a\u5d4c\u5957\u5217\u8868<\/p>\n<p>nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]<\/p>\n<h2><strong>\u9012\u5f52\u4fee\u6539\u5d4c\u5957\u5217\u8868<\/strong><\/h2>\n<p>def modify_nested_list(lst):<\/p>\n<p>    for i in range(len(lst)):<\/p>\n<p>        if isinstance(lst[i], list):<\/p>\n<p>            modify_nested_list(lst[i])<\/p>\n<p>        else:<\/p>\n<p>            lst[i] = lst[i] * 2<\/p>\n<p>modify_nested_list(nested_list)<\/p>\n<p>print(nested_list)  # \u8f93\u51fa: [[2, 4, 6], [8, 10, 12], [14, 16, 18]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u9012\u5f52\u65b9\u6cd5\u9002\u7528\u4e8e\u4efb\u610f\u6df1\u5ea6\u7684\u5d4c\u5957\u5217\u8868\uff0c\u786e\u4fdd\u6240\u6709\u5c42\u7ea7\u7684\u5143\u7d20\u90fd\u80fd\u88ab\u4fee\u6539\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u4f7f\u7528numpy\u6570\u7ec4\u8fdb\u884c\u6279\u91cf\u4fee\u6539<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u6570\u503c\u578b\u5217\u8868\uff0c\u53ef\u4ee5\u4f7f\u7528numpy\u5e93\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4\uff0c\u8fdb\u884c\u9ad8\u6548\u7684\u6279\u91cf\u4fee\u6539\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868<\/strong><\/h2>\n<p>my_list = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u5c06\u5217\u8868\u8f6c\u6362\u4e3anumpy\u6570\u7ec4<\/strong><\/h2>\n<p>np_array = np.array(my_list)<\/p>\n<h2><strong>\u8fdb\u884c\u6279\u91cf\u4fee\u6539<\/strong><\/h2>\n<p>np_array = np_array * 2<\/p>\n<h2><strong>\u5c06numpy\u6570\u7ec4\u8f6c\u6362\u56de\u5217\u8868<\/strong><\/h2>\n<p>my_list = np_array.tolist()<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [2, 4, 6, 8, 10]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>numpy\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u7ec4\u64cd\u4f5c\u51fd\u6570\uff0c\u9002\u5408\u9700\u8981\u8fdb\u884c\u5927\u91cf\u6570\u503c\u8ba1\u7b97\u548c\u6279\u91cf\u4fee\u6539\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u4f7f\u7528pandas\u8fdb\u884c\u6279\u91cf\u4fee\u6539<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u6570\u636e\u5206\u6790\u4efb\u52a1\uff0c\u53ef\u4ee5\u4f7f\u7528pandas\u5e93\u5c06\u5217\u8868\u8f6c\u6362\u4e3aDataFrame\uff0c\u8fdb\u884c\u590d\u6742\u7684\u6279\u91cf\u4fee\u6539\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868<\/strong><\/h2>\n<p>my_list = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u5c06\u5217\u8868\u8f6c\u6362\u4e3apandas DataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame(my_list, columns=[&#39;values&#39;])<\/p>\n<h2><strong>\u8fdb\u884c\u6279\u91cf\u4fee\u6539<\/strong><\/h2>\n<p>df[&#39;values&#39;] = df[&#39;values&#39;] * 2<\/p>\n<h2><strong>\u5c06DataFrame\u8f6c\u6362\u56de\u5217\u8868<\/strong><\/h2>\n<p>my_list = df[&#39;values&#39;].tolist()<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [2, 4, 6, 8, 10]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>pandas\u5e93\u9002\u7528\u4e8e\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u590d\u6742\u5904\u7406\u548c\u5206\u6790\u7684\u573a\u666f\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u636e\u64cd\u4f5c\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u901a\u8fc7\u51fd\u6570\u5c01\u88c5\u4fee\u6539\u903b\u8f91<\/h3>\n<\/p>\n<p><p>\u5c06\u4fee\u6539\u903b\u8f91\u5c01\u88c5\u5728\u51fd\u6570\u4e2d\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u91cd\u7528\u6027\u548c\u53ef\u8bfb\u6027\u3002\u7279\u522b\u662f\u5f53\u4fee\u6539\u903b\u8f91\u8f83\u4e3a\u590d\u6742\u65f6\uff0c\u51fd\u6570\u5c01\u88c5\u53ef\u4ee5\u4f7f\u4ee3\u7801\u7ed3\u6784\u66f4\u52a0\u6e05\u6670\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u5c01\u88c5\u4fee\u6539\u903b\u8f91<\/p>\n<p>def modify_list(lst):<\/p>\n<p>    for i in range(len(lst)):<\/p>\n<p>        lst[i] = lst[i] * 2<\/p>\n<p>    return lst<\/p>\n<h2><strong>\u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868<\/strong><\/h2>\n<p>my_list = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u8c03\u7528\u51fd\u6570\u8fdb\u884c\u4fee\u6539<\/strong><\/h2>\n<p>my_list = modify_list(my_list)<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [2, 4, 6, 8, 10]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u51fd\u6570\u5c01\u88c5\u6709\u52a9\u4e8e\u4ee3\u7801\u7684\u7ef4\u62a4\u548c\u6269\u5c55\uff0c\u540c\u65f6\u4f7f\u4e3b\u7a0b\u5e8f\u903b\u8f91\u66f4\u52a0\u7b80\u6d01\u660e\u4e86\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u7684\u5185\u7f6e\u65b9\u6cd5\u8fdb\u884c\u4fee\u6539<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u5bf9\u8c61\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5185\u7f6e\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5e2e\u52a9\u8fdb\u884c\u5143\u7d20\u7684\u6279\u91cf\u4fee\u6539\u548c\u5904\u7406\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u5217\u8868\u7684<code>extend<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5c06\u591a\u4e2a\u5217\u8868\u5143\u7d20\u5408\u5e76\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868<\/p>\n<p>my_list = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u4f7f\u7528\u5217\u8868\u7684\u5185\u7f6e\u65b9\u6cd5\u8fdb\u884c\u4fee\u6539<\/strong><\/h2>\n<p>my_list.extend([6, 7, 8])  # \u5408\u5e76\u65b0\u5143\u7d20<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 2, 3, 4, 5, 6, 7, 8]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5217\u8868\u7684\u5185\u7f6e\u65b9\u6cd5\u63d0\u4f9b\u4e86\u76f4\u63a5\u4fee\u6539\u5217\u8868\u7ed3\u6784\u7684\u529f\u80fd\uff0c\u9002\u7528\u4e8e\u9700\u8981\u6dfb\u52a0\u3001\u5220\u9664\u6216\u66ff\u6362\u591a\u4e2a\u5143\u7d20\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e09\u3001\u4f7f\u7528\u751f\u6210\u5668\u4fee\u6539\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u5668\u53ef\u4ee5\u5728\u751f\u6210\u5143\u7d20\u7684\u8fc7\u7a0b\u4e2d\u8fdb\u884c\u4fee\u6539\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5bf9\u5927\u91cf\u6570\u636e\u8fdb\u884c\u5904\u7406\u4e14\u4e0d\u5e0c\u671b\u4e00\u6b21\u6027\u52a0\u8f7d\u6240\u6709\u6570\u636e\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e00\u4e2a\u751f\u6210\u5668\u51fd\u6570<\/p>\n<p>def modify_generator(lst):<\/p>\n<p>    for x in lst:<\/p>\n<p>        yield x * 2<\/p>\n<h2><strong>\u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868<\/strong><\/h2>\n<p>my_list = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u4f7f\u7528\u751f\u6210\u5668\u4fee\u6539\u5217\u8868<\/strong><\/h2>\n<p>my_list = list(modify_generator(my_list))<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [2, 4, 6, 8, 10]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u751f\u6210\u5668\u53ef\u4ee5\u63d0\u9ad8\u5185\u5b58\u5229\u7528\u7387\uff0c\u9002\u7528\u4e8e\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u6216\u6d41\u5f0f\u6570\u636e\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u56db\u3001\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u8fdb\u884c\u590d\u6742\u4fee\u6539<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u6765\u5b8c\u6210\u590d\u6742\u7684\u4fee\u6539\u4efb\u52a1\u3002\u4f8b\u5982\uff0c\u5148\u4f7f\u7528\u6761\u4ef6\u5224\u65ad\u7b5b\u9009\u5143\u7d20\uff0c\u518d\u4f7f\u7528map\u51fd\u6570\u8fdb\u884c\u7edf\u4e00\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868<\/p>\n<p>my_list = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u7ed3\u5408\u6761\u4ef6\u5224\u65ad\u548cmap\u51fd\u6570\u8fdb\u884c\u590d\u6742\u4fee\u6539<\/strong><\/h2>\n<p>my_list = list(map(lambda x: x * 2 if x % 2 == 0 else x, my_list))<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [1, 4, 3, 8, 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u7075\u6d3b\u5e94\u5bf9\u5404\u79cd\u590d\u6742\u9700\u6c42\uff0c\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e94\u3001\u5904\u7406\u5217\u8868\u4e2d\u7684\u5b57\u5178\u5143\u7d20<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u5217\u8868\u4e2d\u7684\u5143\u7d20\u662f\u5b57\u5178\uff0c\u53ef\u4ee5\u901a\u8fc7\u904d\u5386\u5217\u8868\u5e76\u4fee\u6539\u5b57\u5178\u4e2d\u7684\u7279\u5b9a\u952e\u503c\u5bf9\u6765\u8fdb\u884c\u4fee\u6539\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521d\u59cb\u5316\u4e00\u4e2a\u5305\u542b\u5b57\u5178\u5143\u7d20\u7684\u5217\u8868<\/p>\n<p>my_list = [{&#39;a&#39;: 1}, {&#39;a&#39;: 2}, {&#39;a&#39;: 3}]<\/p>\n<h2><strong>\u4fee\u6539\u5b57\u5178\u4e2d\u7684\u7279\u5b9a\u952e\u503c\u5bf9<\/strong><\/h2>\n<p>for item in my_list:<\/p>\n<p>    item[&#39;a&#39;] = item[&#39;a&#39;] * 2<\/p>\n<p>print(my_list)  # \u8f93\u51fa: [{&#39;a&#39;: 2}, {&#39;a&#39;: 4}, {&#39;a&#39;: 6}]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5904\u7406\u5305\u542b\u5b57\u5178\u7684\u5217\u8868\u65f6\uff0c\u9700\u8981\u786e\u4fdd\u904d\u5386\u8fc7\u7a0b\u4e2d\u6b63\u786e\u8bbf\u95ee\u548c\u4fee\u6539\u5b57\u5178\u4e2d\u7684\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u516d\u3001\u5904\u7406\u591a\u7ef4\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u5904\u7406\u591a\u7ef4\u5217\u8868\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u6216\u9012\u5f52\u65b9\u6cd5\u8fdb\u884c\u4fee\u6539\uff0c\u786e\u4fdd\u6240\u6709\u7ef4\u5ea6\u7684\u5143\u7d20\u90fd\u80fd\u88ab\u6b63\u786e\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521d\u59cb\u5316\u4e00\u4e2a\u591a\u7ef4\u5217\u8868<\/p>\n<p>multi_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]<\/p>\n<h2><strong>\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u4fee\u6539\u591a\u7ef4\u5217\u8868<\/strong><\/h2>\n<p>for i in range(len(multi_list)):<\/p>\n<p>    for j in range(len(multi_list[i])):<\/p>\n<p>        multi_list[i][j] = multi_list[i][j] * 2<\/p>\n<p>print(multi_list)  # \u8f93\u51fa: [[2, 4, 6], [8, 10, 12], [14, 16, 18]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5d4c\u5957\u5faa\u73af\u9002\u7528\u4e8e\u5904\u7406\u56fa\u5b9a\u7ef4\u5ea6\u7684\u591a\u7ef4\u5217\u8868\uff0c\u9012\u5f52\u65b9\u6cd5\u5219\u9002\u7528\u4e8e\u5904\u7406\u4efb\u610f\u6df1\u5ea6\u7684\u591a\u7ef4\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e03\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u8fdb\u884c\u9ad8\u7ea7\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u590d\u6742\u573a\u666f\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\uff08\u5982<code>numpy<\/code>\u3001<code>pandas<\/code>\u7b49\uff09\u8fdb\u884c\u9ad8\u7ea7\u64cd\u4f5c\uff0c\u5145\u5206\u5229\u7528\u5e93\u63d0\u4f9b\u7684\u529f\u80fd\u548c\u6027\u80fd\u4f18\u52bf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521d\u59cb\u5316\u4e00\u4e2a\u591a\u7ef4\u5217\u8868<\/strong><\/h2>\n<p>multi_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]<\/p>\n<h2><strong>\u5c06\u591a\u7ef4\u5217\u8868\u8f6c\u6362\u4e3anumpy\u6570\u7ec4<\/strong><\/h2>\n<p>np_array = np.array(multi_list)<\/p>\n<h2><strong>\u8fdb\u884c\u6279\u91cf\u4fee\u6539<\/strong><\/h2>\n<p>np_array = np_array * 2<\/p>\n<h2><strong>\u5c06numpy\u6570\u7ec4\u8f6c\u6362\u56de\u591a\u7ef4\u5217\u8868<\/strong><\/h2>\n<p>multi_list = np_array.tolist()<\/p>\n<p>print(multi_list)  # \u8f93\u51fa: [[2, 4, 6], [8, 10, 12], [14, 16, 18]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7b2c\u4e09\u65b9\u5e93\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u636e\u64cd\u4f5c\u51fd\u6570\uff0c\u9002\u7528\u4e8e\u9700\u8981\u8fdb\u884c\u5927\u91cf\u6570\u636e\u5904\u7406\u548c\u9ad8\u7ea7\u5206\u6790\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u4fee\u6539\u5217\u8868\u7684\u591a\u884c\u503c\u5728Python\u4e2d\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u7d22\u5f15\u76f4\u63a5\u4fee\u6539\u3001\u901a\u8fc7\u5faa\u73af\u4fee\u6539\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u4f7f\u7528map\u51fd\u6570\u3001\u901a\u8fc7\u6761\u4ef6\u5224\u65ad\u4fee\u6539\u7b49\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u52bf\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u751a\u81f3\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u8fdb\u884c\u590d\u6742\u7684\u4fee\u6539\u64cd\u4f5c\u3002\u901a\u8fc7\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u5b8c\u6210\u5404\u79cd\u5217\u8868\u4fee\u6539\u4efb\u52a1\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6709\u6548\u5730\u4fee\u6539\u5217\u8868\u4e2d\u7684\u591a\u884c\u503c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u6216\u5185\u7f6e\u51fd\u6570\u6765\u4fee\u6539\u5217\u8868\u4e2d\u7684\u591a\u884c\u503c\u3002\u5faa\u73af\u9002\u5408\u5bf9\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u590d\u6742\u7684\u4fee\u6539\uff0c\u800c\u5217\u8868\u63a8\u5bfc\u5f0f\u5219\u63d0\u4f9b\u4e86\u66f4\u7b80\u6d01\u7684\u5199\u6cd5\u3002\u6bd4\u5982\uff0c\u5982\u679c\u8981\u5c06\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u503c\u589e\u52a01\uff0c\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u4ee3\u7801\uff1a<code>my_list = [x + 1 for x in my_list]<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u6d01\uff0c\u8fd8\u80fd\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002<\/p>\n<p><strong>\u662f\u5426\u6709\u65b9\u6cd5\u53ef\u4ee5\u6279\u91cf\u66ff\u6362\u5217\u8868\u4e2d\u7684\u7279\u5b9a\u503c\uff1f<\/strong><br \/>\u5f53\u7136\uff0c\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6216<code>map()<\/code>\u51fd\u6570\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5b9e\u73b0\u6279\u91cf\u66ff\u6362\u3002\u4f8b\u5982\uff0c\u82e5\u60f3\u5c06\u5217\u8868\u4e2d\u6240\u6709\u7684\u201capple\u201d\u66ff\u6362\u4e3a\u201corange\u201d\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<code>my_list = [&#39;orange&#39; if x == &#39;apple&#39; else x for x in my_list]<\/code>\u3002\u8fd9\u79cd\u65b9\u5f0f\u5141\u8bb8\u4f60\u5728\u4e00\u4e2a\u64cd\u4f5c\u4e2d\u5b8c\u6210\u591a\u4e2a\u66ff\u6362\uff0c\u975e\u5e38\u9ad8\u6548\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u5305\u542b\u591a\u79cd\u6570\u636e\u7c7b\u578b\u7684\u5217\u8868\u5e76\u4fee\u6539\u5176\u4e2d\u7684\u7279\u5b9a\u884c\uff1f<\/strong><br \/>\u5904\u7406\u5305\u542b\u4e0d\u540c\u6570\u636e\u7c7b\u578b\u7684\u5217\u8868\u65f6\uff0c\u9996\u5148\u9700\u8981\u786e\u4fdd\u80fd\u591f\u8bc6\u522b\u5e76\u9009\u62e9\u8981\u4fee\u6539\u7684\u5143\u7d20\u3002\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u68c0\u67e5\u5143\u7d20\u7684\u6570\u636e\u7c7b\u578b\u3002\u4f8b\u5982\uff0c\u82e5\u8981\u5c06\u5217\u8868\u4e2d\u6240\u6709\u7684\u6570\u5b57\u589e\u52a01\uff0c\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u4ee3\u7801\uff1a<code>my_list = [x + 1 if isinstance(x, int) else x for x in my_list]<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u6709\u6548\u5730\u9488\u5bf9\u7279\u5b9a\u7c7b\u578b\u7684\u5143\u7d20\u8fdb\u884c\u4fee\u6539\uff0c\u540c\u65f6\u4fdd\u6301\u5176\u4ed6\u6570\u636e\u7684\u5b8c\u6574\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5982\u4f55\u4fee\u6539\u5217\u8868\u591a\u884c\u503cpython\uff1a\u4f7f\u7528\u7d22\u5f15\u76f4\u63a5\u4fee\u6539\u3001\u901a\u8fc7\u5faa\u73af\u4fee\u6539\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u4f7f\u7528map\u51fd\u6570\u3001\u901a\u8fc7\u6761\u4ef6\u5224\u65ad\u4fee [&hellip;]","protected":false},"author":3,"featured_media":1159991,"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\/1159985"}],"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=1159985"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1159985\/revisions"}],"predecessor-version":[{"id":1159992,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1159985\/revisions\/1159992"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1159991"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1159985"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1159985"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1159985"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}