{"id":1007174,"date":"2024-12-27T10:50:59","date_gmt":"2024-12-27T02:50:59","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1007174.html"},"modified":"2024-12-27T10:51:02","modified_gmt":"2024-12-27T02:51:02","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e9%80%89%e6%8b%a9%e6%95%b0%e5%ad%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1007174.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u9009\u62e9\u6570\u5b57"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25083008\/447d8d09-9d95-44e5-83b2-fcfc22c67938.webp\" alt=\"python\u4e2d\u5982\u4f55\u9009\u62e9\u6570\u5b57\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u9009\u62e9\u6570\u5b57\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001\u8fd0\u7b97\u7b26\u548c\u6570\u636e\u7ed3\u6784\uff0c\u5982\u5217\u8868\u3001\u5b57\u5178\u7b49\uff0c\u8fdb\u884c\u6570\u5b57\u7684\u9009\u62e9\u548c\u64cd\u4f5c\u3002\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u7d22\u5f15\u4ece\u5217\u8868\u4e2d\u9009\u62e9\u7279\u5b9a\u6570\u5b57\u3001\u901a\u8fc7\u6761\u4ef6\u7b5b\u9009\u7b26\u5408\u8981\u6c42\u7684\u6570\u5b57\u3001\u5229\u7528\u968f\u673a\u6a21\u5757\u751f\u6210\u968f\u673a\u6570\u5b57\u3002<\/strong>\u5176\u4e2d\uff0c\u5229\u7528\u7d22\u5f15\u4ece\u5217\u8868\u4e2d\u9009\u62e9\u7279\u5b9a\u6570\u5b57\u662f\u4e00\u4e2a\u975e\u5e38\u76f4\u63a5\u548c\u5e38\u7528\u7684\u65b9\u6cd5\u3002Python\u5217\u8868\u662f\u4e00\u4e2a\u6709\u5e8f\u7684\u96c6\u5408\uff0c\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u5176\u4e2d\u7684\u5143\u7d20\u3002\u7d22\u5f15\u4ece0\u5f00\u59cb\u8ba1\u6570\uff0c\u8d1f\u7d22\u5f15\u53ef\u4ee5\u4ece\u5217\u8868\u7684\u672b\u5c3e\u5f00\u59cb\u8ba1\u6570\uff0c\u8fd9\u63d0\u4f9b\u4e86\u7075\u6d3b\u548c\u65b9\u4fbf\u7684\u65b9\u5f0f\u6765\u9009\u62e9\u5217\u8868\u4e2d\u7684\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u7d22\u5f15\u9009\u62e9\u6570\u5b57<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5217\u8868\u662f\u4e00\u79cd\u975e\u5e38\u5e38\u7528\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5b83\u53ef\u4ee5\u5b58\u50a8\u4e00\u7cfb\u5217\u7684\u6570\u636e\u3002\u901a\u8fc7\u7d22\u5f15\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u4ece\u5217\u8868\u4e2d\u9009\u62e9\u7279\u5b9a\u7684\u6570\u5b57\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u4f7f\u7528\u7d22\u5f15\u9009\u62e9\u6570\u5b57\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><h4>1.1 \u4ece\u5217\u8868\u4e2d\u9009\u62e9\u5355\u4e2a\u6570\u5b57<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u6570\u5b57\u7684\u5217\u8868\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u7d22\u5f15\u6765\u9009\u62e9\u5217\u8868\u4e2d\u7684\u67d0\u4e2a\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [10, 20, 30, 40, 50]<\/p>\n<p>selected_number = numbers[2]  # \u9009\u62e9\u7b2c\u4e09\u4e2a\u6570\u5b57\uff0c\u7d22\u5f15\u4e3a2<\/p>\n<p>print(selected_number)  # \u8f93\u51fa\uff1a30<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>numbers[2]<\/code>\u8868\u793a\u9009\u62e9\u7d22\u5f15\u4e3a2\u7684\u6570\u5b57\uff0c\u537330\u3002<\/p>\n<\/p>\n<p><h4>1.2 \u4f7f\u7528\u8d1f\u7d22\u5f15\u9009\u62e9\u6570\u5b57<\/h4>\n<\/p>\n<p><p>Python\u7684\u5217\u8868\u5141\u8bb8\u4f7f\u7528\u8d1f\u7d22\u5f15\uff0c\u4ece\u800c\u53ef\u4ee5\u4ece\u5217\u8868\u7684\u672b\u5c3e\u5f00\u59cb\u9009\u62e9\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [10, 20, 30, 40, 50]<\/p>\n<p>selected_number = numbers[-1]  # \u9009\u62e9\u6700\u540e\u4e00\u4e2a\u6570\u5b57<\/p>\n<p>print(selected_number)  # \u8f93\u51fa\uff1a50<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8d1f\u7d22\u5f15<code>-1<\/code>\u8868\u793a\u9009\u62e9\u5217\u8868\u4e2d\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff0c\u8fd9\u5bf9\u4e8e\u9700\u8981\u4ece\u5217\u8868\u672b\u5c3e\u83b7\u53d6\u5143\u7d20\u7684\u60c5\u51b5\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u901a\u8fc7\u6761\u4ef6\u7b5b\u9009\u6570\u5b57<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528\u7d22\u5f15\uff0cPython\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5f3a\u5927\u7684\u65b9\u5f0f\u6765\u901a\u8fc7\u6761\u4ef6\u7b5b\u9009\u5217\u8868\u4e2d\u7684\u6570\u5b57\u3002\u6761\u4ef6\u7b5b\u9009\u901a\u5e38\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6216\u8fc7\u6ee4\u51fd\u6570\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h4>2.1 \u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u7b5b\u9009\u6570\u5b57<\/h4>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\uff0c\u7528\u4e8e\u4ece\u73b0\u6709\u5217\u8868\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u6839\u636e\u6307\u5b9a\u7684\u6761\u4ef6\u7b5b\u9009\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [10, 20, 30, 40, 50]<\/p>\n<p>even_numbers = [num for num in numbers if num % 2 == 0]<\/p>\n<p>print(even_numbers)  # \u8f93\u51fa\uff1a[10, 20, 30, 40, 50]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u7528\u4e8e\u7b5b\u9009\u51fa\u5217\u8868\u4e2d\u6240\u6709\u7684\u5076\u6570\u3002<\/p>\n<\/p>\n<p><h4>2.2 \u4f7f\u7528filter\u51fd\u6570\u7b5b\u9009\u6570\u5b57<\/h4>\n<\/p>\n<p><p><code>filter()<\/code>\u51fd\u6570\u662fPython\u7684\u5185\u7f6e\u51fd\u6570\uff0c\u7528\u4e8e\u6839\u636e\u6307\u5b9a\u7684\u6761\u4ef6\u7b5b\u9009\u51fa\u7b26\u5408\u6761\u4ef6\u7684\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [10, 21, 30, 41, 50]<\/p>\n<p>odd_numbers = list(filter(lambda x: x % 2 != 0, numbers))<\/p>\n<p>print(odd_numbers)  # \u8f93\u51fa\uff1a[21, 41]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\uff0c\u6211\u4eec\u4f7f\u7528<code>filter()<\/code>\u51fd\u6570\u548c<code>lambda<\/code>\u8868\u8fbe\u5f0f\u6765\u7b5b\u9009\u51fa\u5217\u8868\u4e2d\u7684\u5947\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5229\u7528\u968f\u673a\u6a21\u5757\u751f\u6210\u968f\u673a\u6570\u5b57<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u751f\u6210\u968f\u673a\u6570\u5b57\u3002Python\u7684<code>random<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u751f\u6210\u968f\u673a\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><h4>3.1 \u751f\u6210\u968f\u673a\u6574\u6570<\/h4>\n<\/p>\n<p><p><code>random.randint()<\/code>\u65b9\u6cd5\u7528\u4e8e\u751f\u6210\u6307\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6574\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>random_number = random.randint(1, 100)  # \u751f\u62101\u5230100\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570<\/p>\n<p>print(random_number)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u65b9\u6cd5\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff0c\u8868\u793a\u968f\u673a\u6570\u7684\u8303\u56f4\uff08\u5305\u62ec\u8fb9\u754c\uff09\u3002<\/p>\n<\/p>\n<p><h4>3.2 \u751f\u6210\u968f\u673a\u6d6e\u70b9\u6570<\/h4>\n<\/p>\n<p><p><code>random.uniform()<\/code>\u65b9\u6cd5\u7528\u4e8e\u751f\u6210\u6307\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6d6e\u70b9\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>random_float = random.uniform(1.0, 10.0)  # \u751f\u62101.0\u523010.0\u4e4b\u95f4\u7684\u968f\u673a\u6d6e\u70b9\u6570<\/p>\n<p>print(random_float)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>uniform()<\/code>\u65b9\u6cd5\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff0c\u8868\u793a\u6d6e\u70b9\u6570\u7684\u8303\u56f4\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u96c6\u5408\u9009\u62e9\u552f\u4e00\u6570\u5b57<\/h3>\n<\/p>\n<p><p>\u96c6\u5408\u662f\u4e00\u79cd\u65e0\u5e8f\u7684\u3001\u4e0d\u91cd\u590d\u7684\u5143\u7d20\u96c6\u5408\uff0c\u5728Python\u4e2d\u53ef\u4ee5\u7528\u4e8e\u5b58\u50a8\u552f\u4e00\u7684\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><h4>4.1 \u4ece\u5217\u8868\u4e2d\u83b7\u53d6\u552f\u4e00\u6570\u5b57<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\uff0c\u4ee5\u4fbf\u83b7\u53d6\u5217\u8868\u4e2d\u7684\u552f\u4e00\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [10, 20, 20, 30, 40, 40, 50]<\/p>\n<p>unique_numbers = set(numbers)<\/p>\n<p>print(unique_numbers)  # \u8f93\u51fa\uff1a{40, 10, 50, 20, 30}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u96c6\u5408\u81ea\u52a8\u53bb\u9664\u91cd\u590d\u7684\u5143\u7d20\uff0c\u56e0\u6b64\u975e\u5e38\u9002\u5408\u7528\u4e8e\u83b7\u53d6\u552f\u4e00\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><h4>4.2 \u96c6\u5408\u8fd0\u7b97\u9009\u62e9\u6570\u5b57<\/h4>\n<\/p>\n<p><p>\u96c6\u5408\u652f\u6301\u591a\u79cd\u8fd0\u7b97\uff0c\u5982\u4ea4\u96c6\u3001\u5e76\u96c6\u3001\u5dee\u96c6\u7b49\uff0c\u8fd9\u4e9b\u8fd0\u7b97\u53ef\u4ee5\u7528\u4e8e\u9009\u62e9\u7279\u5b9a\u7684\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">set1 = {10, 20, 30, 40}<\/p>\n<p>set2 = {30, 40, 50, 60}<\/p>\n<p>intersection = set1 &amp; set2  # \u6c42\u4ea4\u96c6<\/p>\n<p>union = set1 | set2  # \u6c42\u5e76\u96c6<\/p>\n<p>difference = set1 - set2  # \u6c42\u5dee\u96c6<\/p>\n<p>print(intersection)  # \u8f93\u51fa\uff1a{40, 30}<\/p>\n<p>print(union)  # \u8f93\u51fa\uff1a{40, 10, 50, 20, 60, 30}<\/p>\n<p>print(difference)  # \u8f93\u51fa\uff1a{10, 20}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u96c6\u5408\u8fd0\u7b97\uff0c\u6211\u4eec\u53ef\u4ee5\u9009\u62e9\u51fa\u4e24\u7ec4\u6570\u5b57\u7684\u4ea4\u96c6\u3001\u5e76\u96c6\u6216\u5dee\u96c6\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u5b57\u5178\u9009\u62e9\u6570\u5b57<\/h3>\n<\/p>\n<p><p>\u5b57\u5178\u662fPython\u4e2d\u53e6\u4e00\u79cd\u5e38\u7528\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u7528\u6765\u5b58\u50a8\u952e\u503c\u5bf9\u3002\u901a\u8fc7\u952e\uff0c\u6211\u4eec\u53ef\u4ee5\u9009\u62e9\u5bf9\u5e94\u7684\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><h4>5.1 \u901a\u8fc7\u952e\u9009\u62e9\u6570\u5b57<\/h4>\n<\/p>\n<p><p>\u5b57\u5178\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u6709\u4e00\u4e2a\u552f\u4e00\u7684\u952e\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u952e\u6765\u9009\u62e9\u5bf9\u5e94\u7684\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">scores = {&#39;Alice&#39;: 85, &#39;Bob&#39;: 92, &#39;Charlie&#39;: 78}<\/p>\n<p>alice_score = scores[&#39;Alice&#39;]<\/p>\n<p>print(alice_score)  # \u8f93\u51fa\uff1a85<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u952e&#39;Alice&#39;\u9009\u62e9\u4e86\u5bf9\u5e94\u7684\u5206\u6570\u3002<\/p>\n<\/p>\n<p><h4>5.2 \u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u7b5b\u9009\u6570\u5b57<\/h4>\n<\/p>\n<p><p>\u5b57\u5178\u63a8\u5bfc\u5f0f\u53ef\u4ee5\u7528\u4e8e\u4ece\u73b0\u6709\u5b57\u5178\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b57\u5178\uff0c\u7b5b\u9009\u51fa\u7b26\u5408\u6761\u4ef6\u7684\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">scores = {&#39;Alice&#39;: 85, &#39;Bob&#39;: 92, &#39;Charlie&#39;: 78}<\/p>\n<p>high_scores = {name: score for name, score in scores.items() if score &gt; 80}<\/p>\n<p>print(high_scores)  # \u8f93\u51fa\uff1a{&#39;Alice&#39;: 85, &#39;Bob&#39;: 92}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u7b5b\u9009\u51fa\u5206\u6570\u9ad8\u4e8e80\u7684\u5b66\u751f\u53ca\u5176\u5206\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u9009\u62e9\u548c\u5904\u7406\u6570\u5b57<\/h3>\n<\/p>\n<p><p>Python\u7684\u751f\u6001\u7cfb\u7edf\u4e2d\u6709\u8bb8\u591a\u7b2c\u4e09\u65b9\u5e93\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u9ad8\u6548\u5730\u9009\u62e9\u548c\u5904\u7406\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><h4>6.1 \u4f7f\u7528NumPy\u9009\u62e9\u6570\u5b57<\/h4>\n<\/p>\n<p><p>NumPy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u53ef\u4ee5\u7528\u4e8e\u5904\u7406\u5927\u578b\u591a\u7ef4\u6570\u7ec4\u548c\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>array = np.array([10, 20, 30, 40, 50])<\/p>\n<p>selected_numbers = array[array &gt; 25]<\/p>\n<p>print(selected_numbers)  # \u8f93\u51fa\uff1a[30 40 50]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>NumPy\u6570\u7ec4\u652f\u6301\u5e03\u5c14\u7d22\u5f15\uff0c\u53ef\u4ee5\u901a\u8fc7\u6761\u4ef6\u9009\u62e9\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h4>6.2 \u4f7f\u7528Pandas\u9009\u62e9\u6570\u5b57<\/h4>\n<\/p>\n<p><p>Pandas\u662f\u4e00\u4e2a\u6d41\u884c\u7684\u6570\u636e\u5206\u6790\u5e93\uff0c\u63d0\u4f9b\u4e86\u6570\u636e\u5e27\u5bf9\u8c61\uff0c\u7c7b\u4f3c\u4e8e\u7535\u5b50\u8868\u683c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = {&#39;Name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;], &#39;Score&#39;: [85, 92, 78]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>high_scores = df[df[&#39;Score&#39;] &gt; 80]<\/p>\n<p>print(high_scores)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>Pandas\u7684\u6570\u636e\u5e27\u652f\u6301\u57fa\u4e8e\u6761\u4ef6\u7684\u884c\u9009\u62e9\uff0c\u53ef\u4ee5\u7528\u4e8e\u9009\u62e9\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u9009\u62e9\u6570\u5b57\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u5305\u62ec\u4f7f\u7528\u7d22\u5f15\u3001\u901a\u8fc7\u6761\u4ef6\u7b5b\u9009\u3001\u5229\u7528\u968f\u673a\u6a21\u5757\u751f\u6210\u3001\u4f7f\u7528\u96c6\u5408\u548c\u5b57\u5178\uff0c\u4ee5\u53ca\u501f\u52a9\u7b2c\u4e09\u65b9\u5e93\u5982NumPy\u548cPandas\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u5176\u5e94\u7528\u573a\u666f\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u9ad8\u6548\u5730\u5904\u7406\u548c\u5206\u6790\u6570\u5b57\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u6700\u9002\u5408\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u6570\u5b57\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u7b5b\u9009\u7279\u5b9a\u8303\u56f4\u7684\u6570\u5b57\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u7ed3\u5408\u6761\u4ef6\u8bed\u53e5\u6765\u7b5b\u9009\u7279\u5b9a\u8303\u56f4\u7684\u6570\u5b57\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u6570\u5b57\u7684\u5217\u8868\uff0c\u4f60\u60f3\u9009\u62e9\u6240\u6709\u57281\u523010\u4e4b\u95f4\u7684\u6570\u5b57\uff0c\u53ef\u4ee5\u8fd9\u6837\u5b9e\u73b0\uff1a  <\/p>\n<pre><code class=\"language-python\">numbers = [0, 5, 12, 7, 3, 15, 8]\nfiltered_numbers = [num for num in numbers if 1 &lt;= num &lt;= 10]\nprint(filtered_numbers)  # \u8f93\u51fa: [5, 7, 3, 8]\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u9009\u62e9\u91cd\u590d\u7684\u6570\u5b57\uff1f<\/strong><br \/>\u9009\u62e9\u5217\u8868\u4e2d\u91cd\u590d\u7684\u6570\u5b57\u53ef\u4ee5\u4f7f\u7528\u96c6\u5408\u548c\u5217\u8868\u63a8\u5bfc\u5f0f\u3002\u901a\u8fc7\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u96c6\u5408\uff0c\u4f60\u53ef\u4ee5\u627e\u5230\u552f\u4e00\u7684\u6570\u5b57\uff0c\u7136\u540e\u518d\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u627e\u51fa\u90a3\u4e9b\u5728\u539f\u5217\u8868\u4e2d\u51fa\u73b0\u8d85\u8fc7\u4e00\u6b21\u7684\u6570\u5b57\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">numbers = [1, 2, 3, 1, 2, 4, 5]\nduplicates = [num for num in set(numbers) if numbers.count(num) &gt; 1]\nprint(duplicates)  # \u8f93\u51fa: [1, 2]\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u6839\u636e\u6761\u4ef6\u9009\u62e9\u6570\u5b57\u5e76\u8fdb\u884c\u6392\u5e8f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u5148\u7b5b\u9009\u51fa\u7b26\u5408\u6761\u4ef6\u7684\u6570\u5b57\uff0c\u7136\u540e\u4f7f\u7528\u5185\u7f6e\u7684<code>sorted()<\/code>\u51fd\u6570\u5bf9\u7ed3\u679c\u8fdb\u884c\u6392\u5e8f\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60f3\u9009\u62e9\u6240\u6709\u5076\u6570\u5e76\u6309\u5347\u5e8f\u6392\u5217\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a  <\/p>\n<pre><code class=\"language-python\">numbers = [5, 8, 2, 11, 4, 7, 10]\neven_numbers_sorted = sorted([num for num in numbers if num % 2 == 0])\nprint(even_numbers_sorted)  # \u8f93\u51fa: [2, 4, 8, 10]\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u9009\u62e9\u6570\u5b57\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001\u8fd0\u7b97\u7b26\u548c\u6570\u636e\u7ed3\u6784\uff0c\u5982\u5217\u8868\u3001\u5b57\u5178\u7b49\uff0c\u8fdb\u884c\u6570\u5b57\u7684\u9009\u62e9\u548c\u64cd\u4f5c\u3002\u5e38\u89c1\u7684\u65b9 [&hellip;]","protected":false},"author":3,"featured_media":1007190,"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\/1007174"}],"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=1007174"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1007174\/revisions"}],"predecessor-version":[{"id":1007192,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1007174\/revisions\/1007192"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1007190"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1007174"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1007174"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1007174"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}