{"id":1002935,"date":"2024-12-27T10:13:03","date_gmt":"2024-12-27T02:13:03","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1002935.html"},"modified":"2024-12-27T10:13:05","modified_gmt":"2024-12-27T02:13:05","slug":"python%e5%a6%82%e4%bd%95%e5%af%b9%e6%95%b0%e6%8d%ae%e9%9a%8f%e6%9c%ba","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1002935.html","title":{"rendered":"python\u5982\u4f55\u5bf9\u6570\u636e\u968f\u673a"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25080507\/b1b94c16-4c1f-4a49-a213-7d4f395b9606.webp\" alt=\"python\u5982\u4f55\u5bf9\u6570\u636e\u968f\u673a\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u5bf9\u6570\u636e\u8fdb\u884c\u968f\u673a\u5316\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528random\u6a21\u5757\u3001shuffle\u65b9\u6cd5\u3001sample\u51fd\u6570\u7b49<\/strong>\u3002\u5176\u4e2d\uff0c<code>random\u6a21\u5757<\/code>\u662fPython\u5185\u7f6e\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u751f\u6210\u968f\u673a\u6570\u7684\u5404\u79cd\u65b9\u6cd5\uff1b<code>shuffle\u65b9\u6cd5<\/code>\u53ef\u4ee5\u7528\u6765\u968f\u673a\u6253\u4e71\u5217\u8868\u4e2d\u7684\u5143\u7d20\u987a\u5e8f\uff1b<code>sample\u51fd\u6570<\/code>\u5219\u53ef\u4ee5\u4ece\u5e8f\u5217\u4e2d\u968f\u673a\u9009\u62e9\u6307\u5b9a\u6570\u91cf\u7684\u5143\u7d20\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u5bf9\u6570\u636e\u8fdb\u884c\u968f\u673a\u5316\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001RANDOM\u6a21\u5757<\/p>\n<\/p>\n<p><p><code>random<\/code>\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u63d0\u4f9b\u4e86\u591a\u79cd\u751f\u6210\u968f\u673a\u6570\u7684\u65b9\u6cd5\u3002\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u5b83\u6765\u751f\u6210\u968f\u673a\u6574\u6570\u3001\u6d6e\u70b9\u6570\u4ee5\u53ca\u968f\u673a\u9009\u62e9\u5e8f\u5217\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u751f\u6210\u968f\u673a\u6574\u6570<\/strong><\/p>\n<\/p>\n<p><p><code>randint()<\/code>\u51fd\u6570\u53ef\u4ee5\u751f\u6210\u6307\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6574\u6570\u3002\u4f7f\u7528\u65b9\u5f0f\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>random_int = random.randint(1, 10)  # \u751f\u62101\u523010\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570<\/p>\n<p>print(random_int)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u548c\u79d1\u5b66\u8ba1\u7b97\u4e2d\uff0c\u751f\u6210\u968f\u673a\u6574\u6570\u53ef\u4ee5\u7528\u4e8e\u6a21\u62df\u5b9e\u9a8c\u3001\u751f\u6210\u6837\u672c\u6570\u636e\u7b49\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u751f\u6210\u968f\u673a\u6d6e\u70b9\u6570<\/strong><\/p>\n<\/p>\n<p><p><code>uniform()<\/code>\u51fd\u6570\u53ef\u4ee5\u751f\u6210\u6307\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6d6e\u70b9\u6570\u3002\u4f7f\u7528\u65b9\u5f0f\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">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>\u751f\u6210\u968f\u673a\u6d6e\u70b9\u6570\u901a\u5e38\u7528\u4e8e\u9700\u8981\u66f4\u9ad8\u7cbe\u5ea6\u7684\u6a21\u62df\u548c\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u968f\u673a\u9009\u62e9\u5e8f\u5217\u4e2d\u7684\u5143\u7d20<\/strong><\/p>\n<\/p>\n<p><p><code>choice()<\/code>\u51fd\u6570\u53ef\u4ee5\u4ece\u975e\u7a7a\u5e8f\u5217\u4e2d\u968f\u673a\u9009\u62e9\u4e00\u4e2a\u5143\u7d20\u3002\u4f7f\u7528\u65b9\u5f0f\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">items = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p>random_item = random.choice(items)<\/p>\n<p>print(random_item)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u5e38\u7528\u4e8e\u968f\u673a\u5316\u9009\u62e9\u5217\u8868\u3001\u5143\u7ec4\u6216\u5b57\u7b26\u4e32\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001SHUFFLE\u65b9\u6cd5<\/p>\n<\/p>\n<p><p><code>shuffle()<\/code>\u65b9\u6cd5\u7528\u4e8e\u5c06\u5e8f\u5217\u4e2d\u7684\u5143\u7d20\u968f\u673a\u6253\u4e71\u3002\u5b83\u76f4\u63a5\u4fee\u6539\u539f\u5e8f\u5217\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6253\u4e71\u5217\u8868<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">items = [1, 2, 3, 4, 5]<\/p>\n<p>random.shuffle(items)<\/p>\n<p>print(items)  # \u6253\u4e71\u540e\u7684\u5217\u8868<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8be5\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5c06\u6570\u636e\u987a\u5e8f\u968f\u673a\u5316\u7684\u60c5\u666f\uff0c\u5982\u6d17\u724c\u7b97\u6cd5\u3001\u968f\u673a\u6392\u5217\u95ee\u9898\u7b49\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5e94\u7528\u573a\u666f<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>shuffle()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u6e38\u620f\u5f00\u53d1\u4e2d\u5b9e\u73b0\u6d17\u724c\u529f\u80fd\uff0c\u6216\u8005\u5728\u6570\u636e\u5206\u6790\u4e2d\u6253\u4e71\u6570\u636e\u96c6\u4ee5\u8fdb\u884c\u4ea4\u53c9\u9a8c\u8bc1\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001SAMPLE\u51fd\u6570<\/p>\n<\/p>\n<p><p><code>sample()<\/code>\u51fd\u6570\u7528\u4e8e\u4ece\u5e8f\u5217\u4e2d\u968f\u673a\u9009\u62e9\u6307\u5b9a\u6570\u91cf\u7684\u5143\u7d20\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4ece\u5e8f\u5217\u4e2d\u9009\u62e9\u5143\u7d20<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">items = [1, 2, 3, 4, 5]<\/p>\n<p>random_items = random.sample(items, 3)  # \u4eceitems\u4e2d\u968f\u673a\u9009\u62e93\u4e2a\u5143\u7d20<\/p>\n<p>print(random_items)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8be5\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u4ece\u6570\u636e\u96c6\u4e2d\u62bd\u6837\u7684\u573a\u666f\uff0c\u7279\u522b\u662f\u5f53\u6211\u4eec\u9700\u8981\u786e\u4fdd\u6ca1\u6709\u91cd\u590d\u9009\u62e9\u65f6\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5e94\u7528\u573a\u666f<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u7edf\u8ba1\u5b66\u4e2d\uff0c<code>sample()<\/code>\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u6837\u672c\u6570\u636e\u96c6\uff0c\u4ece\u800c\u5728\u4e0d\u9700\u8981\u5168\u90e8\u6570\u636e\u7684\u60c5\u51b5\u4e0b\u8fdb\u884c\u5206\u6790\u3002\u8fd9\u5bf9\u4e8e\u5927\u6570\u636e\u96c6\u5c24\u4e3a\u91cd\u8981\uff0c\u56e0\u4e3a\u5904\u7406\u6574\u4e2a\u6570\u636e\u96c6\u53ef\u80fd\u4f1a\u8017\u8d39\u5927\u91cf\u7684\u65f6\u95f4\u548c\u8d44\u6e90\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001NUMPY\u5e93<\/p>\n<\/p>\n<p><p>\u9664\u4e86<code>random<\/code>\u6a21\u5757\uff0c<code>NumPy<\/code>\u5e93\u4e5f\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5f3a\u5927\u7684\u968f\u673a\u5316\u529f\u80fd\uff0c\u7279\u522b\u9002\u5408\u4e8e\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u751f\u6210\u968f\u673a\u6570\u7ec4<\/strong><\/p>\n<\/p>\n<p><p><code>numpy.random<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u751f\u6210\u968f\u673a\u6570\u7ec4\u7684\u65b9\u6cd5\uff0c\u6bd4\u5982<code>rand()<\/code>\u3001<code>randint()<\/code>\u3001<code>normal()<\/code>\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>random_array = np.random.rand(3, 3)  # \u751f\u62103x3\u7684\u968f\u673a\u6570\u7ec4<\/p>\n<p>print(random_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e9b\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u751f\u6210\u591a\u7ef4\u968f\u673a\u6570\u636e\u7684\u573a\u666f\uff0c\u5982<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u4e2d\u7684\u6743\u91cd\u521d\u59cb\u5316\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u968f\u673a\u6392\u5217\u6570\u7ec4<\/strong><\/p>\n<\/p>\n<p><p><code>numpy.random.shuffle()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u968f\u673a\u6392\u5217\u6570\u7ec4\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">arr = np.array([1, 2, 3, 4, 5])<\/p>\n<p>np.random.shuffle(arr)<\/p>\n<p>print(arr)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5728\u5904\u7406\u591a\u7ef4\u6570\u636e\u65f6\u5c24\u5176\u65b9\u4fbf\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u76f4\u63a5\u4f5c\u7528\u4e8e\u6570\u7ec4\uff0c\u800c\u4e0d\u9700\u8981\u5c06\u5176\u8f6c\u6362\u4e3a\u5217\u8868\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001PANDAS\u5e93<\/p>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c<code>pandas<\/code>\u5e93\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u5b83\u4e5f\u63d0\u4f9b\u4e86\u968f\u673a\u5316\u6570\u636e\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u968f\u673a\u6253\u4e71DataFrame<\/strong><\/p>\n<\/p>\n<p><p><code>pandas.DataFrame.sample()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u7528\u4e8e\u968f\u673a\u6253\u4e71DataFrame\u4e2d\u7684\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>df = pd.DataFrame({&#39;A&#39;: [1, 2, 3], &#39;B&#39;: [4, 5, 6]})<\/p>\n<p>shuffled_df = df.sample(frac=1).reset_index(drop=True)<\/p>\n<p>print(shuffled_df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u968f\u673a\u5316\u5904\u7406\u6570\u636e\u96c6\u7684\u573a\u666f\uff0c\u7279\u522b\u662f\u5728\u6570\u636e\u9884\u5904\u7406\u4e2d\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u968f\u673a\u62bd\u6837<\/strong><\/p>\n<\/p>\n<p><p><code>sample()<\/code>\u65b9\u6cd5\u8fd8\u53ef\u4ee5\u7528\u4e8e\u4eceDataFrame\u4e2d\u62bd\u6837\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">sampled_df = df.sample(n=2)  # \u968f\u673a\u62bd\u53d62\u884c<\/p>\n<p>print(sampled_df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u8bad\u7ec3\u96c6\u548c\u6d4b\u8bd5\u96c6\uff0c\u786e\u4fdd\u6a21\u578b\u7684\u6cdb\u5316\u80fd\u529b\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u516d\u3001\u5e94\u7528\u5b9e\u4f8b<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u591a\u79cd\u573a\u666f\u4e2d\u5e94\u7528\u968f\u673a\u5316\u6280\u672f\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5b9e\u9645\u5e94\u7528\u5b9e\u4f8b\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u968f\u673a\u62bd\u53d6\u5ba2\u6237<\/strong><\/p>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5ba2\u6237\u5217\u8868\uff0c\u9700\u8981\u968f\u673a\u62bd\u53d6\u4e00\u90e8\u5206\u8fdb\u884c\u8c03\u67e5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">customers = [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;, &#39;David&#39;, &#39;Eve&#39;]<\/p>\n<p>selected_customers = random.sample(customers, 3)<\/p>\n<p>print(selected_customers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u4f01\u4e1a\u5728\u4e0d\u63a5\u89e6\u6240\u6709\u5ba2\u6237\u7684\u60c5\u51b5\u4e0b\uff0c\u4e86\u89e3\u5ba2\u6237\u9700\u6c42\u548c\u6ee1\u610f\u5ea6\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u968f\u673a\u5316\u5b9e\u9a8c<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u79d1\u5b66\u7814\u7a76\u4e2d\uff0c\u968f\u673a\u5316\u5b9e\u9a8c\u662f\u8bc4\u4f30\u56e0\u679c\u5173\u7cfb\u7684\u91cd\u8981\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">groups = [&#39;Control&#39;, &#39;Treatment&#39;]<\/p>\n<p>random.shuffle(groups)<\/p>\n<p>print(groups)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u968f\u673a\u5316\u5206\u914d\u5b9e\u9a8c\u7ec4\u548c\u5bf9\u7167\u7ec4\uff0c\u53ef\u4ee5\u6709\u6548\u63a7\u5236\u5916\u90e8\u53d8\u91cf\u7684\u5f71\u54cd\uff0c\u63d0\u9ad8\u5b9e\u9a8c\u7684\u53ef\u4fe1\u5ea6\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u751f\u6210\u968f\u673a\u6d4b\u8bd5\u6570\u636e<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u8f6f\u4ef6\u5f00\u53d1\u4e2d\uff0c\u751f\u6210\u968f\u673a\u6d4b\u8bd5\u6570\u636e\u662f\u786e\u4fdd\u7a0b\u5e8f\u5065\u58ee\u6027\u7684\u91cd\u8981\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">test_data = [random.randint(0, 100) for _ in range(10)]<\/p>\n<p>print(test_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u968f\u673a\u6d4b\u8bd5\u6570\u636e\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u4eba\u5458\u8bc6\u522b\u7a0b\u5e8f\u4e2d\u7684\u6f5c\u5728\u95ee\u9898\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u8fb9\u754c\u6761\u4ef6\u65f6\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u603b\u4e4b\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u5f3a\u5927\u7684\u5de5\u5177\u548c\u5e93\u6765\u5b9e\u73b0\u6570\u636e\u7684\u968f\u673a\u5316\u3002\u8fd9\u4e9b\u5de5\u5177\u4e0d\u4ec5\u9002\u7528\u4e8e\u7b80\u5355\u7684\u968f\u673a\u6570\u751f\u6210\uff0c\u8fd8\u80fd\u5904\u7406\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u548c\u6570\u636e\u5206\u6790\u4efb\u52a1\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u548c\u5de5\u5177\uff0c\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u548c\u7ed3\u679c\u7684\u53ef\u9760\u6027\u3002\u65e0\u8bba\u662f\u5728\u79d1\u5b66\u7814\u7a76\u3001\u6570\u636e\u5206\u6790\u8fd8\u662f\u8f6f\u4ef6\u5f00\u53d1\u4e2d\uff0c\u968f\u673a\u5316\u6280\u672f\u90fd\u662f\u4e00\u79cd\u4e0d\u53ef\u6216\u7f3a\u7684\u5de5\u5177\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u751f\u6210\u968f\u673a\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>random<\/code>\u6a21\u5757\u6765\u751f\u6210\u968f\u673a\u6570\u3002\u8fd9\u4e2a\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u751f\u6210\u968f\u673a\u6574\u6570\u3001\u6d6e\u70b9\u6570\u4ee5\u53ca\u9009\u62e9\u968f\u673a\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>random.randint(a, b)<\/code>\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u8303\u56f4\u5728a\u5230b\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\uff0c\u800c<code>random.uniform(a, b)<\/code>\u5219\u53ef\u4ee5\u751f\u6210\u8303\u56f4\u5728a\u5230b\u4e4b\u95f4\u7684\u968f\u673a\u6d6e\u70b9\u6570\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u968f\u673a\u6253\u4e71\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff1f<\/strong><br \/>\u8981\u968f\u673a\u6253\u4e71\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528<code>random.shuffle()<\/code>\u51fd\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u4f1a\u76f4\u63a5\u4fee\u6539\u539f\u59cb\u5217\u8868\uff0c\u5c06\u5176\u5143\u7d20\u987a\u5e8f\u968f\u673a\u6253\u4e71\u3002\u4f8b\u5982\uff0c\u82e5\u6709\u4e00\u4e2a\u5217\u8868<code>my_list<\/code>\uff0c\u53ea\u9700\u8c03\u7528<code>random.shuffle(my_list)<\/code>\uff0c\u5373\u53ef\u5b9e\u73b0\u5143\u7d20\u7684\u968f\u673a\u6392\u5217\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u4ece\u4e00\u4e2a\u96c6\u5408\u4e2d\u968f\u673a\u9009\u62e9\u591a\u4e2a\u5143\u7d20\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>random.sample()<\/code>\u51fd\u6570\u4ece\u4e00\u4e2a\u96c6\u5408\u4e2d\u968f\u673a\u9009\u62e9\u591a\u4e2a\u5143\u7d20\u3002\u8be5\u51fd\u6570\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff1a\u8981\u9009\u62e9\u7684\u96c6\u5408\u548c\u9700\u8981\u9009\u62e9\u7684\u5143\u7d20\u6570\u91cf\u3002\u6bd4\u5982\uff0c<code>random.sample(my_set, k)<\/code>\u5c06\u4ece\u96c6\u5408<code>my_set<\/code>\u4e2d\u968f\u673a\u9009\u62e9k\u4e2a\u5143\u7d20\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5217\u8868\u3002\u6b64\u65b9\u6cd5\u786e\u4fdd\u9009\u62e9\u7684\u5143\u7d20\u4e0d\u4f1a\u91cd\u590d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u5bf9\u6570\u636e\u8fdb\u884c\u968f\u673a\u5316\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528random\u6a21\u5757\u3001shuffle\u65b9\u6cd5\u3001sample\u51fd\u6570\u7b49\u3002\u5176\u4e2d\uff0cr [&hellip;]","protected":false},"author":3,"featured_media":1002940,"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\/1002935"}],"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=1002935"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1002935\/revisions"}],"predecessor-version":[{"id":1002941,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1002935\/revisions\/1002941"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1002940"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1002935"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1002935"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1002935"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}