{"id":1134807,"date":"2025-01-08T21:19:07","date_gmt":"2025-01-08T13:19:07","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1134807.html"},"modified":"2025-01-08T21:19:10","modified_gmt":"2025-01-08T13:19:10","slug":"python%e5%a6%82%e4%bd%95%e6%9f%a5%e6%89%be%e5%88%97%e8%a1%a8%e9%87%8c%e6%af%94%e8%87%aa%e5%b7%b1%e5%b0%8f%e7%9a%84%e6%95%b0%e5%ad%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1134807.html","title":{"rendered":"python\u5982\u4f55\u67e5\u627e\u5217\u8868\u91cc\u6bd4\u81ea\u5df1\u5c0f\u7684\u6570\u5b57"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25103634\/d48b7888-c658-4f74-b4d8-8a38b3f69797.webp\" alt=\"python\u5982\u4f55\u67e5\u627e\u5217\u8868\u91cc\u6bd4\u81ea\u5df1\u5c0f\u7684\u6570\u5b57\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u67e5\u627e\u5217\u8868\u91cc\u6bd4\u81ea\u5df1\u5c0f\u7684\u6570\u5b57\uff0c\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001filter\u51fd\u6570\u7b49\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002<\/strong> \u5176\u4e2d\uff0c\u6700\u5e38\u89c1\u548c\u9ad8\u6548\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u5b9e\u73b0\uff0c\u56e0\u4e3a\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u6d01\uff0c\u800c\u4e14\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u6027\u80fd\u8f83\u4f18\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u67e5\u627e\u5217\u8868\u4e2d\u6bd4\u81ea\u5df1\u5c0f\u7684\u6570\u5b57\uff0c\u5e76\u4ecb\u7ecd\u4e00\u4e9b\u5b9e\u9645\u5e94\u7528\u548c\u4f18\u5316\u6280\u5de7\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u67e5\u627e\u6bd4\u81ea\u5df1\u5c0f\u7684\u6570\u5b57<\/h3>\n<\/p>\n<p><p>\u5d4c\u5957\u5faa\u73af\u662f\u4e00\u79cd\u76f4\u63a5\u4e14\u5bb9\u6613\u7406\u89e3\u7684\u65b9\u6cd5\uff0c\u901a\u8fc7\u904d\u5386\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5e76\u5c06\u5176\u4e0e\u5217\u8868\u4e2d\u7684\u5176\u4ed6\u5143\u7d20\u8fdb\u884c\u6bd4\u8f83\uff0c\u627e\u51fa\u6bd4\u5176\u5c0f\u7684\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def find_smaller_numbers_nested_loop(lst):<\/p>\n<p>    result = []<\/p>\n<p>    for i in range(len(lst)):<\/p>\n<p>        smaller_numbers = []<\/p>\n<p>        for j in range(len(lst)):<\/p>\n<p>            if lst[j] &lt; lst[i]:<\/p>\n<p>                smaller_numbers.append(lst[j])<\/p>\n<p>        result.append(smaller_numbers)<\/p>\n<p>    return result<\/p>\n<p>lst = [3, 1, 4, 1, 5]<\/p>\n<p>print(find_smaller_numbers_nested_loop(lst))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e24\u4e2a\u5d4c\u5957\u7684<code>for<\/code>\u5faa\u73af\u904d\u5386\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5e76\u5c06\u6bd4\u5f53\u524d\u5143\u7d20\u5c0f\u7684\u6240\u6709\u6570\u5b57\u5b58\u50a8\u5728\u4e00\u4e2a\u65b0\u7684\u5217\u8868\u4e2d\u3002\u6700\u7ec8\u7ed3\u679c\u662f\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u5b50\u5217\u8868\u7684\u5217\u8868\uff0c\u6bcf\u4e2a\u5b50\u5217\u8868\u4e2d\u7684\u5143\u7d20\u90fd\u662f\u6bd4\u5bf9\u5e94\u4f4d\u7f6e\u5143\u7d20\u5c0f\u7684\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u67e5\u627e\u6bd4\u81ea\u5df1\u5c0f\u7684\u6570\u5b57<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u66f4\u7b80\u6d01\u548cPythonic\u7684\u65b9\u5f0f\uff0c\u5b83\u53ef\u4ee5\u5728\u4e00\u884c\u4ee3\u7801\u4e2d\u5b8c\u6210\u5d4c\u5957\u5faa\u73af\u7684\u529f\u80fd\u3002\u5217\u8868\u63a8\u5bfc\u5f0f\u4e0d\u4ec5\u4f7f\u4ee3\u7801\u66f4\u7b80\u6d01\uff0c\u800c\u4e14\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u6027\u80fd\u4e5f\u66f4\u4f18\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def find_smaller_numbers_list_comprehension(lst):<\/p>\n<p>    return [[y for y in lst if y &lt; x] for x in lst]<\/p>\n<p>lst = [3, 1, 4, 1, 5]<\/p>\n<p>print(find_smaller_numbers_list_comprehension(lst))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u4e24\u4e2a\u5d4c\u5957\u7684\u5217\u8868\u63a8\u5bfc\u5f0f\u3002\u5916\u5c42\u5217\u8868\u63a8\u5bfc\u5f0f\u904d\u5386\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5185\u5c42\u5217\u8868\u63a8\u5bfc\u5f0f\u5219\u627e\u51fa\u6240\u6709\u6bd4\u5f53\u524d\u5143\u7d20\u5c0f\u7684\u6570\u5b57\u3002\u6700\u7ec8\u7ed3\u679c\u4e0e\u5d4c\u5957\u5faa\u73af\u7684\u7ed3\u679c\u76f8\u540c\uff0c\u4f46\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528filter\u51fd\u6570\u67e5\u627e\u6bd4\u81ea\u5df1\u5c0f\u7684\u6570\u5b57<\/h3>\n<\/p>\n<p><p><code>filter<\/code>\u51fd\u6570\u662f\u4e00\u79cd\u51fd\u6570\u5f0f\u7f16\u7a0b\u98ce\u683c\u7684\u65b9\u6cd5\uff0c\u5b83\u53ef\u4ee5\u7528\u4e8e\u7b5b\u9009\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002<code>filter<\/code>\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u4e00\u4e2a\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u7528\u4e8e\u5b9a\u4e49\u7b5b\u9009\u6761\u4ef6\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def find_smaller_numbers_filter(lst):<\/p>\n<p>    return [list(filter(lambda y: y &lt; x, lst)) for x in lst]<\/p>\n<p>lst = [3, 1, 4, 1, 5]<\/p>\n<p>print(find_smaller_numbers_filter(lst))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86<code>filter<\/code>\u51fd\u6570\u6765\u7b5b\u9009\u6bd4\u5f53\u524d\u5143\u7d20\u5c0f\u7684\u6570\u5b57\u3002<code>filter<\/code>\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u6211\u4eec\u5c06\u5176\u8f6c\u6362\u4e3a\u5217\u8868\u5e76\u5b58\u50a8\u5728\u6700\u7ec8\u7ed3\u679c\u4e2d\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0e\u5217\u8868\u63a8\u5bfc\u5f0f\u975e\u5e38\u76f8\u4f3c\uff0c\u4f46\u4f7f\u7528\u4e86\u51fd\u6570\u5f0f\u7f16\u7a0b\u7684\u98ce\u683c\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f18\u5316\u548c\u6027\u80fd\u8003\u8651<\/h3>\n<\/p>\n<p><p>\u867d\u7136\u4e0a\u8ff0\u65b9\u6cd5\u90fd\u53ef\u4ee5\u5b9e\u73b0\u67e5\u627e\u5217\u8868\u4e2d\u6bd4\u81ea\u5df1\u5c0f\u7684\u6570\u5b57\u7684\u529f\u80fd\uff0c\u4f46\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0c\u6027\u80fd\u53ef\u80fd\u4f1a\u6210\u4e3a\u4e00\u4e2a\u95ee\u9898\u3002\u53ef\u4ee5\u8003\u8651\u4ee5\u4e0b\u51e0\u79cd\u4f18\u5316\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u6bd4\u8f83<\/h4>\n<\/p>\n<p><p>\u5728\u4e00\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u901a\u8fc7\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u6bd4\u8f83\u6765\u63d0\u9ad8\u6027\u80fd\u3002\u4f8b\u5982\uff0c\u5982\u679c\u5217\u8868\u662f\u6709\u5e8f\u7684\uff0c\u53ef\u4ee5\u5229\u7528\u4e8c\u5206\u67e5\u627e\u6765\u5feb\u901f\u627e\u5230\u6bd4\u5f53\u524d\u5143\u7d20\u5c0f\u7684\u6240\u6709\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import bisect<\/p>\n<p>def find_smaller_numbers_sorted(lst):<\/p>\n<p>    sorted_lst = sorted(lst)<\/p>\n<p>    result = []<\/p>\n<p>    for x in lst:<\/p>\n<p>        index = bisect.bisect_left(sorted_lst, x)<\/p>\n<p>        result.append(sorted_lst[:index])<\/p>\n<p>    return result<\/p>\n<p>lst = [3, 1, 4, 1, 5]<\/p>\n<p>print(find_smaller_numbers_sorted(lst))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bf9\u5217\u8868\u8fdb\u884c\u6392\u5e8f\uff0c\u7136\u540e\u4f7f\u7528<code>bisect<\/code>\u6a21\u5757\u7684<code>bisect_left<\/code>\u51fd\u6570\u627e\u5230\u6bd4\u5f53\u524d\u5143\u7d20\u5c0f\u7684\u6240\u6709\u6570\u5b57\u7684\u4f4d\u7f6e\u3002\u7531\u4e8e\u6392\u5e8f\u540e\u7684\u5217\u8868\u662f\u6709\u5e8f\u7684\uff0c\u53ef\u4ee5\u76f4\u63a5\u5207\u7247\u5f97\u5230\u6bd4\u5f53\u524d\u5143\u7d20\u5c0f\u7684\u6240\u6709\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5e76\u884c\u8ba1\u7b97<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u975e\u5e38\u5927\u7684\u5217\u8868\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u5e76\u884c\u8ba1\u7b97\u6765\u63d0\u9ad8\u6027\u80fd\u3002Python\u7684<code>multiprocessing<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u5e76\u884c\u8ba1\u7b97\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5229\u7528\u591a\u6838\u5904\u7406\u5668\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from multiprocessing import Pool<\/p>\n<p>def find_smaller_numbers_parallel(lst):<\/p>\n<p>    def smaller_numbers(x):<\/p>\n<p>        return [y for y in lst if y &lt; x]<\/p>\n<p>    with Pool() as pool:<\/p>\n<p>        result = pool.map(smaller_numbers, lst)<\/p>\n<p>    return result<\/p>\n<p>lst = [3, 1, 4, 1, 5]<\/p>\n<p>print(find_smaller_numbers_parallel(lst))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86<code>multiprocessing<\/code>\u6a21\u5757\u4e2d\u7684<code>Pool<\/code>\u7c7b\u6765\u5b9e\u73b0\u5e76\u884c\u8ba1\u7b97\u3002<code>Pool<\/code>\u7c7b\u7684<code>map<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5e76\u884c\u5730\u5e94\u7528\u4e00\u4e2a\u51fd\u6570\u5230\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u7684\u6bcf\u4e2a\u5143\u7d20\u4e0a\uff0c\u4ece\u800c\u63d0\u9ad8\u8ba1\u7b97\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5b9e\u9645\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u67e5\u627e\u5217\u8868\u4e2d\u6bd4\u81ea\u5df1\u5c0f\u7684\u6570\u5b57\u5728\u8bb8\u591a\u5b9e\u9645\u5e94\u7528\u4e2d\u90fd\u6709\u91cd\u8981\u7684\u4f5c\u7528\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u6570\u636e\u5206\u6790<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u67e5\u627e\u6bd4\u67d0\u4e2a\u503c\u5c0f\u7684\u6240\u6709\u6570\u636e\u70b9\u6709\u52a9\u4e8e\u7406\u89e3\u6570\u636e\u7684\u5206\u5e03\u548c\u8d8b\u52bf\u3002\u4f8b\u5982\uff0c\u5728\u80a1\u7968\u5e02\u573a\u5206\u6790\u4e2d\uff0c\u53ef\u4ee5\u67e5\u627e\u6bd4\u67d0\u4e2a\u4ef7\u683c\u4f4e\u7684\u6240\u6709\u4ea4\u6613\u8bb0\u5f55\uff0c\u4ece\u800c\u4e86\u89e3\u4ef7\u683c\u6ce2\u52a8\u7684\u8303\u56f4\u548c\u9891\u7387\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u6392\u5e8f\u548c\u6392\u540d<\/h4>\n<\/p>\n<p><p>\u5728\u6392\u5e8f\u548c\u6392\u540d\u7b97\u6cd5\u4e2d\uff0c\u67e5\u627e\u6bd4\u67d0\u4e2a\u5143\u7d20\u5c0f\u7684\u6240\u6709\u5143\u7d20\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u5728\u5feb\u901f\u6392\u5e8f\u7b97\u6cd5\u4e2d\uff0c\u9700\u8981\u4e0d\u65ad\u5730\u5c06\u5217\u8868\u5212\u5206\u4e3a\u6bd4\u67d0\u4e2a\u57fa\u51c6\u503c\u5c0f\u7684\u90e8\u5206\u548c\u6bd4\u57fa\u51c6\u503c\u5927\u7684\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u52a8\u6001\u89c4\u5212<\/h4>\n<\/p>\n<p><p>\u5728\u52a8\u6001\u89c4\u5212\u7b97\u6cd5\u4e2d\uff0c\u67e5\u627e\u6bd4\u67d0\u4e2a\u72b6\u6001\u5c0f\u7684\u6240\u6709\u72b6\u6001\u6709\u52a9\u4e8e\u6784\u5efa\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u3002\u4f8b\u5982\uff0c\u5728\u6700\u957f\u9012\u589e\u5b50\u5e8f\u5217\u95ee\u9898\u4e2d\uff0c\u9700\u8981\u67e5\u627e\u6bd4\u5f53\u524d\u5143\u7d20\u5c0f\u7684\u6240\u6709\u5143\u7d20\uff0c\u4ee5\u786e\u5b9a\u6700\u957f\u7684\u9012\u589e\u5b50\u5e8f\u5217\u957f\u5ea6\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u8be6\u7ec6\u63a2\u8ba8\u4e86\u5728Python\u4e2d\u67e5\u627e\u5217\u8868\u91cc\u6bd4\u81ea\u5df1\u5c0f\u7684\u6570\u5b57\u7684\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u5d4c\u5957\u5faa\u73af\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u548cfilter\u51fd\u6570\u7b49\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u6cd5\u3002\u6b64\u5916\uff0c\u6211\u4eec\u8fd8\u4ecb\u7ecd\u4e86\u4e00\u4e9b\u4f18\u5316\u6280\u5de7\u548c\u5b9e\u9645\u5e94\u7528\u573a\u666f\uff0c\u4ee5\u5e2e\u52a9\u8bfb\u8005\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u662f\u5728\u6570\u636e\u5206\u6790\u3001\u6392\u5e8f\u548c\u6392\u540d\u8fd8\u662f\u52a8\u6001\u89c4\u5212\u4e2d\uff0c\u67e5\u627e\u6bd4\u81ea\u5df1\u5c0f\u7684\u6570\u5b57\u90fd\u662f\u4e00\u4e2a\u57fa\u672c\u800c\u91cd\u8981\u7684\u64cd\u4f5c\u3002\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\u548c\u4f18\u5316\u6280\u5de7\uff0c\u60a8\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u5904\u7406\u5404\u79cd\u590d\u6742\u7684\u6570\u636e\u5904\u7406\u4efb\u52a1\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u67e5\u627e\u5217\u8868\u4e2d\u6bd4\u7ed9\u5b9a\u6570\u5b57\u5c0f\u7684\u6240\u6709\u6570\u5b57\uff1f<\/strong><br \/>\u8981\u67e5\u627e\u5217\u8868\u4e2d\u6bd4\u7279\u5b9a\u6570\u5b57\u5c0f\u7684\u6570\u5b57\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = [5, 3, 8, 1, 2]\nnum = 4\nsmaller_numbers = [x for x in my_list if x &lt; num]\nprint(smaller_numbers)  # \u8f93\u51fa: [3, 1, 2]\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u80fd\u5feb\u901f\u8fc7\u6ee4\u51fa\u6240\u6709\u5c0f\u4e8e\u7ed9\u5b9a\u6570\u5b57\u7684\u5143\u7d20\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u6765\u5b9e\u73b0\u76f8\u540c\u7684\u529f\u80fd\uff1f<\/strong><br \/>\u662f\u7684\uff0cPython\u63d0\u4f9b\u4e86\u5185\u7f6e\u7684<code>filter<\/code>\u51fd\u6570\uff0c\u8fd9\u4e2a\u51fd\u6570\u540c\u6837\u53ef\u4ee5\u7528\u6765\u627e\u51fa\u5c0f\u4e8e\u6307\u5b9a\u6570\u5b57\u7684\u5143\u7d20\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = [5, 3, 8, 1, 2]\nnum = 4\nsmaller_numbers = list(filter(lambda x: x &lt; num, my_list))\nprint(smaller_numbers)  # \u8f93\u51fa: [3, 1, 2]\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u80fd\u8ba9\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\uff0c\u5c24\u5176\u9002\u5408\u5904\u7406\u8f83\u5927\u7684\u6570\u636e\u96c6\u3002<\/p>\n<p><strong>\u5982\u679c\u6211\u60f3\u8981\u627e\u51fa\u5217\u8868\u4e2d\u5c0f\u4e8e\u591a\u4e2a\u4e0d\u540c\u6570\u5b57\u7684\u5143\u7d20\uff0c\u8be5\u5982\u4f55\u505a\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u7ed3\u5408\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5bf9\u591a\u4e2a\u6570\u5b57\u8fdb\u884c\u7b5b\u9009\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = [5, 3, 8, 1, 2]\nnumbers = [4, 2]\nsmaller_numbers = {num: [x for x in my_list if x &lt; num] for num in numbers}\nprint(smaller_numbers)  # \u8f93\u51fa: {4: [3, 1, 2], 2: [1]}\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u8ba9\u4f60\u53ef\u4ee5\u540c\u65f6\u5904\u7406\u591a\u4e2a\u6570\u5b57\uff0c\u5e76\u4ee5\u5b57\u5178\u7684\u5f62\u5f0f\u8fd4\u56de\u7ed3\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u67e5\u627e\u5217\u8868\u91cc\u6bd4\u81ea\u5df1\u5c0f\u7684\u6570\u5b57\uff0c\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001filter\u51fd\u6570\u7b49\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002 \u5176\u4e2d\uff0c [&hellip;]","protected":false},"author":3,"featured_media":1134819,"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\/1134807"}],"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=1134807"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1134807\/revisions"}],"predecessor-version":[{"id":1134823,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1134807\/revisions\/1134823"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1134819"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1134807"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1134807"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1134807"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}