{"id":1139509,"date":"2025-01-08T22:10:52","date_gmt":"2025-01-08T14:10:52","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1139509.html"},"modified":"2025-01-08T22:10:55","modified_gmt":"2025-01-08T14:10:55","slug":"python%e5%a6%82%e4%bd%95%e9%9a%8f%e6%9c%ba%e7%94%9f%e6%88%90%e4%b8%80%e7%bb%84%e4%b8%8d%e9%87%8d%e5%a4%8d%e7%9a%84%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1139509.html","title":{"rendered":"python\u5982\u4f55\u968f\u673a\u751f\u6210\u4e00\u7ec4\u4e0d\u91cd\u590d\u7684\u6570\u636e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25102802\/4a0a1975-e565-4baf-83a2-f20a894f8f9c.webp\" alt=\"python\u5982\u4f55\u968f\u673a\u751f\u6210\u4e00\u7ec4\u4e0d\u91cd\u590d\u7684\u6570\u636e\" \/><\/p>\n<p><p> <strong>Python\u968f\u673a\u751f\u6210\u4e00\u7ec4\u4e0d\u91cd\u590d\u7684\u6570\u636e\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528random.sample()\u3001\u5229\u7528\u96c6\u5408(set)\u53bb\u91cd\u3001\u4f7f\u7528numpy\u5e93\u7684\u51fd\u6570\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u5177\u4f53\u9009\u62e9\u53d6\u51b3\u4e8e\u6570\u636e\u91cf\u548c\u4f7f\u7528\u573a\u666f\u3002<\/strong>\u5176\u4e2d\uff0c<strong>random.sample()<\/strong> \u65b9\u6cd5\u662f\u6700\u5e38\u89c1\u4e14\u7b80\u5355\u7684\u9009\u62e9\uff0c\u5b83\u80fd\u6709\u6548\u907f\u514d\u91cd\u590d\u5e76\u80fd\u63a7\u5236\u751f\u6210\u6570\u636e\u7684\u8303\u56f4\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u53ca\u5176\u4f7f\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528random.sample()<\/h3>\n<\/p>\n<p><p><code>random.sample()<\/code> \u662fPython\u6807\u51c6\u5e93<code>random<\/code>\u6a21\u5757\u4e2d\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u7528\u4e8e\u4ece\u4e00\u4e2a\u5e8f\u5217\u4e2d\u968f\u673a\u9009\u62e9\u6307\u5b9a\u6570\u91cf\u7684\u5143\u7d20\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u4e0d\u91cd\u590d\u7684\u5217\u8868\u3002\u8fd9\u4e2a\u65b9\u6cd5\u9002\u7528\u4e8e\u751f\u6210\u6570\u91cf\u8f83\u5c11\u4e14\u8303\u56f4\u8f83\u5927\u7684\u6570\u636e\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 random<\/p>\n<p>data = random.sample(range(1000), 10)<\/p>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<p><code>random.sample()<\/code> \u51fd\u6570\u63a5\u6536\u4e24\u4e2a\u53c2\u6570\uff1a\u4e00\u4e2a\u662f\u9700\u8981\u62bd\u53d6\u6570\u636e\u7684\u5e8f\u5217\uff08\u53ef\u4ee5\u662f\u5217\u8868\u3001\u5143\u7ec4\u6216\u8303\u56f4\u5bf9\u8c61\u7b49\uff09\uff0c\u53e6\u4e00\u4e2a\u662f\u62bd\u53d6\u7684\u6570\u91cf\u3002\u8be5\u51fd\u6570\u4fdd\u8bc1\u8fd4\u56de\u7684\u6570\u636e\u6ca1\u6709\u91cd\u590d\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5229\u7528\u96c6\u5408(set)\u53bb\u91cd<\/h3>\n<\/p>\n<p><p>\u96c6\u5408\uff08set\uff09\u662f\u4e00\u79cd\u65e0\u5e8f\u4e14\u4e0d\u91cd\u590d\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u5229\u7528\u5b83\u7684\u7279\u6027\u6765\u751f\u6210\u4e0d\u91cd\u590d\u7684\u6570\u636e\u3002\u53ef\u4ee5\u5148\u751f\u6210\u4e00\u4e2a\u5305\u542b\u91cd\u590d\u6570\u636e\u7684\u5217\u8868\uff0c\u7136\u540e\u5c06\u5176\u8f6c\u6362\u4e3a\u96c6\u5408\u4ee5\u53bb\u91cd\uff0c\u518d\u6839\u636e\u9700\u8981\u8c03\u6574\u96c6\u5408\u7684\u5927\u5c0f\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 random<\/p>\n<p>data_set = set()<\/p>\n<p>while len(data_set) &lt; 10:<\/p>\n<p>    data_set.add(random.randint(0, 999))<\/p>\n<p>data = list(data_set)<\/p>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<p>\u5728\u8fd9\u4e2a\u65b9\u6cd5\u4e2d\uff0c\u901a\u8fc7\u4e0d\u65ad\u5411\u96c6\u5408\u4e2d\u6dfb\u52a0\u968f\u673a\u751f\u6210\u7684\u6574\u6570\uff0c\u76f4\u5230\u96c6\u5408\u7684\u5927\u5c0f\u8fbe\u5230\u6240\u9700\u7684\u6570\u91cf\u3002\u7531\u4e8e\u96c6\u5408\u7684\u7279\u6027\uff0c\u6dfb\u52a0\u8fc7\u7a0b\u4e2d\u81ea\u52a8\u53bb\u91cd\uff0c\u4fdd\u8bc1\u6700\u7ec8\u6570\u636e\u6ca1\u6709\u91cd\u590d\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528numpy\u5e93\u7684\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>numpy<\/code>\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u5176<code>numpy.random<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u968f\u673a\u6570\u751f\u6210\u51fd\u6570\u3002\u4f7f\u7528<code>numpy<\/code>\u53ef\u4ee5\u65b9\u4fbf\u5730\u751f\u6210\u5927\u91cf\u968f\u673a\u6570\u636e\uff0c\u5e76\u4e14\u5177\u6709\u8f83\u9ad8\u7684\u6027\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 numpy as np<\/p>\n<p>data = np.random.choice(range(1000), 10, replace=False)<\/p>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<p><code>numpy.random.choice()<\/code> \u51fd\u6570\u7528\u4e8e\u4ece\u7ed9\u5b9a\u7684\u4e00\u7ef4\u6570\u7ec4\u4e2d\u968f\u673a\u62bd\u53d6\u6307\u5b9a\u6570\u91cf\u7684\u5143\u7d20\u3002\u901a\u8fc7\u8bbe\u7f6e<code>replace=False<\/code>\u53c2\u6570\uff0c\u53ef\u4ee5\u786e\u4fdd\u62bd\u53d6\u7684\u6570\u636e\u6ca1\u6709\u91cd\u590d\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u7efc\u5408\u5bf9\u6bd4\u4e0e\u9009\u62e9<\/h3>\n<\/p>\n<p><h4>1\u3001\u6027\u80fd\u4e0e\u6548\u7387<\/h4>\n<\/p>\n<p><p>\u5728\u751f\u6210\u4e0d\u91cd\u590d\u7684\u6570\u636e\u65f6\uff0c<code>random.sample()<\/code> \u548c <code>numpy.random.choice()<\/code> \u90fd\u5177\u6709\u8f83\u9ad8\u7684\u6548\u7387\uff0c\u9002\u5408\u5927\u591a\u6570\u573a\u666f\u3002\u96c6\u5408\u53bb\u91cd\u6cd5\u9002\u7528\u4e8e\u751f\u6210\u6570\u91cf\u8f83\u5c11\u7684\u6570\u636e\uff0c\u4e14\u4ee3\u7801\u8f83\u4e3a\u76f4\u89c2\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u6570\u636e\u8303\u56f4\u4e0e\u63a7\u5236<\/h4>\n<\/p>\n<p><p><code>random.sample()<\/code> \u548c <code>numpy.random.choice()<\/code> \u90fd\u53ef\u4ee5\u7075\u6d3b\u63a7\u5236\u6570\u636e\u8303\u56f4\u548c\u6570\u91cf\uff0c\u9002\u7528\u4e8e\u751f\u6210\u5927\u8303\u56f4\u5185\u7684\u968f\u673a\u6570\u636e\u3002\u800c\u96c6\u5408\u53bb\u91cd\u6cd5\u4e3b\u8981\u4f9d\u8d56\u968f\u673a\u6570\u751f\u6210\u51fd\u6570\uff0c\u9002\u7528\u4e8e\u7279\u5b9a\u8303\u56f4\u5185\u7684\u6570\u636e\u751f\u6210\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u4f7f\u7528\u573a\u666f<\/h4>\n<\/p>\n<ul>\n<li><strong><code>random.sample()<\/code><\/strong>\uff1a\u9002\u7528\u4e8e\u4e00\u822c\u5e94\u7528\u573a\u666f\uff0c\u4ee3\u7801\u7b80\u6d01\u660e\u4e86\u3002<\/li>\n<li><strong>\u96c6\u5408\u53bb\u91cd\u6cd5<\/strong>\uff1a\u9002\u7528\u4e8e\u9700\u8981\u624b\u52a8\u63a7\u5236\u6570\u636e\u751f\u6210\u8fc7\u7a0b\u7684\u573a\u666f\u3002<\/li>\n<li><strong><code>numpy.random.choice()<\/code><\/strong>\uff1a\u9002\u7528\u4e8e\u9700\u8981\u9ad8\u6027\u80fd\u3001\u5927\u89c4\u6a21\u6570\u636e\u751f\u6210\u7684\u573a\u666f\uff0c\u5c24\u5176\u662f\u5728\u79d1\u5b66\u8ba1\u7b97\u6216\u6570\u636e\u5206\u6790\u4e2d\u3002<\/li>\n<\/ul>\n<p><h3>\u4e94\u3001\u7efc\u5408\u5e94\u7528\u5b9e\u4f8b<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7efc\u5408\u5e94\u7528\u5b9e\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528\u4e0a\u8ff0\u65b9\u6cd5\u751f\u6210\u4e00\u7ec4\u4e0d\u91cd\u590d\u7684\u6570\u636e\uff0c\u5e76\u8fdb\u884c\u4e00\u4e9b\u57fa\u672c\u7684\u7edf\u8ba1\u5206\u6790\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 random<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u4f7f\u7528random.sample()\u751f\u6210\u4e0d\u91cd\u590d\u6570\u636e<\/strong><\/h2>\n<p>data_sample = random.sample(range(1000), 10)<\/p>\n<p>print(&quot;Data generated using random.sample():&quot;, data_sample)<\/p>\n<h2><strong>\u4f7f\u7528\u96c6\u5408\u53bb\u91cd\u6cd5\u751f\u6210\u4e0d\u91cd\u590d\u6570\u636e<\/strong><\/h2>\n<p>data_set = set()<\/p>\n<p>while len(data_set) &lt; 10:<\/p>\n<p>    data_set.add(random.randint(0, 999))<\/p>\n<p>data_list = list(data_set)<\/p>\n<p>print(&quot;Data generated using set:&quot;, data_list)<\/p>\n<h2><strong>\u4f7f\u7528numpy\u751f\u6210\u4e0d\u91cd\u590d\u6570\u636e<\/strong><\/h2>\n<p>data_numpy = np.random.choice(range(1000), 10, replace=False)<\/p>\n<p>print(&quot;Data generated using numpy:&quot;, data_numpy)<\/p>\n<h2><strong>\u7edf\u8ba1\u5206\u6790<\/strong><\/h2>\n<p>mean_sample = np.mean(data_sample)<\/p>\n<p>mean_list = np.mean(data_list)<\/p>\n<p>mean_numpy = np.mean(data_numpy)<\/p>\n<p>print(f&quot;Mean of random.sample() data: {mean_sample}&quot;)<\/p>\n<p>print(f&quot;Mean of set data: {mean_list}&quot;)<\/p>\n<p>print(f&quot;Mean of numpy data: {mean_numpy}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<p>\u8fd9\u4e2a\u5b9e\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u4e09\u79cd\u65b9\u6cd5\u751f\u6210\u4e0d\u91cd\u590d\u7684\u6570\u636e\uff0c\u5e76\u8fdb\u884c\u5e73\u5747\u503c\u8ba1\u7b97\u3002\u901a\u8fc7\u5bf9\u6bd4\u4e0d\u540c\u65b9\u6cd5\u751f\u6210\u7684\u6570\u636e\uff0c\u53ef\u4ee5\u53d1\u73b0\u5404\u65b9\u6cd5\u7684\u4f18\u52bf\u548c\u9002\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u751f\u6210\u4e00\u7ec4\u4e0d\u91cd\u590d\u7684\u6570\u636e\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528<code>random.sample()<\/code>\u3001\u96c6\u5408\u53bb\u91cd\u6cd5\u548c<code>numpy<\/code>\u5e93\u3002<strong><code>random.sample()<\/code>\u65b9\u6cd5\u7b80\u6d01\u9ad8\u6548<\/strong>\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u573a\u666f\uff1b<strong>\u96c6\u5408\u53bb\u91cd\u6cd5\u9002\u5408\u624b\u52a8\u63a7\u5236\u6570\u636e\u751f\u6210<\/strong>\uff0c\u4ee3\u7801\u76f4\u89c2\uff1b<strong><code>numpy<\/code>\u5e93\u9002\u7528\u4e8e\u9ad8\u6027\u80fd\u3001\u5927\u89c4\u6a21\u6570\u636e\u751f\u6210<\/strong>\uff0c\u7279\u522b\u9002\u5408\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u751f\u6210\u6240\u9700\u7684\u4e0d\u91cd\u590d\u6570\u636e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u786e\u4fdd\u751f\u6210\u7684\u6570\u636e\u4e0d\u91cd\u590d\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>set<\/code>\u6570\u636e\u7ed3\u6784\u6765\u5b58\u50a8\u968f\u673a\u751f\u6210\u7684\u6570\u636e\uff0c\u8fd9\u6837\u53ef\u4ee5\u81ea\u52a8\u8fc7\u6ee4\u6389\u91cd\u590d\u7684\u5143\u7d20\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>random.sample()<\/code>\u51fd\u6570\u53ef\u4ee5\u4e00\u6b21\u6027\u751f\u6210\u6307\u5b9a\u6570\u91cf\u7684\u4e0d\u91cd\u590d\u968f\u673a\u6570\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u60f3\u751f\u621010\u4e2a\u4e0d\u91cd\u590d\u7684\u968f\u673a\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528<code>random.sample(range(1, 100), 10)<\/code>\uff0c\u5b83\u5c06\u4ece1\u523099\u4e4b\u95f4\u968f\u673a\u9009\u53d610\u4e2a\u6570\u5b57\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u5e93\u6765\u751f\u6210\u4e0d\u91cd\u590d\u7684\u968f\u673a\u6570\u636e\uff1f<\/strong><br \/>Python\u7684\u6807\u51c6\u5e93<code>random<\/code>\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u968f\u673a\u6570\u751f\u6210\u51fd\u6570\uff0c\u7279\u522b\u662f<code>random.sample()<\/code>\u548c<code>random.choice()<\/code>\u3002\u6b64\u5916\uff0c<code>numpy<\/code>\u5e93\u4e5f\u975e\u5e38\u9002\u5408\u5904\u7406\u968f\u673a\u6570\u636e\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u751f\u6210\u5927\u89c4\u6a21\u6570\u636e\u65f6\u3002\u4f7f\u7528<code>numpy.random.choice()<\/code>\u53ef\u4ee5\u65b9\u4fbf\u5730\u751f\u6210\u4e0d\u91cd\u590d\u7684\u968f\u673a\u6837\u672c\u3002<\/p>\n<p><strong>\u751f\u6210\u968f\u673a\u6570\u636e\u65f6\u5982\u4f55\u63a7\u5236\u6570\u636e\u7684\u8303\u56f4\u548c\u6570\u91cf\uff1f<\/strong><br \/>\u5728\u4f7f\u7528<code>random.sample()<\/code>\u65f6\uff0c\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u8303\u56f4\u6765\u63a7\u5236\u751f\u6210\u6570\u636e\u7684\u4e0a\u4e0b\u9650\u3002\u4f8b\u5982\uff0c<code>random.sample(range(1, 51), 5)<\/code>\u5c06\u4ece1\u523050\u4e2d\u968f\u673a\u9009\u62e95\u4e2a\u4e0d\u91cd\u590d\u7684\u6570\u5b57\u3002\u6b64\u5916\uff0c\u7528\u6237\u53ef\u4ee5\u8c03\u6574\u53c2\u6570\u4ee5\u751f\u6210\u6240\u9700\u6570\u91cf\u7684\u968f\u673a\u6570\uff0c\u53ea\u9700\u66f4\u6539\u51fd\u6570\u4e2d\u7684\u6570\u91cf\u53c2\u6570\u5373\u53ef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u968f\u673a\u751f\u6210\u4e00\u7ec4\u4e0d\u91cd\u590d\u7684\u6570\u636e\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528random.sample()\u3001\u5229\u7528\u96c6\u5408(set)\u53bb\u91cd\u3001\u4f7f\u7528 [&hellip;]","protected":false},"author":3,"featured_media":1139512,"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\/1139509"}],"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=1139509"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1139509\/revisions"}],"predecessor-version":[{"id":1139513,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1139509\/revisions\/1139513"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1139512"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1139509"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1139509"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1139509"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}