{"id":1167877,"date":"2025-01-15T15:51:09","date_gmt":"2025-01-15T07:51:09","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1167877.html"},"modified":"2025-01-15T15:51:12","modified_gmt":"2025-01-15T07:51:12","slug":"python%e5%a6%82%e4%bd%95%e5%88%9b%e5%bb%ba%e9%9a%8f%e6%9c%ba%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1167877.html","title":{"rendered":"python\u5982\u4f55\u521b\u5efa\u968f\u673a\u6570\u7ec4"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26065259\/e623f36c-06e5-43d2-ace0-2fb27c1de7db.webp\" alt=\"python\u5982\u4f55\u521b\u5efa\u968f\u673a\u6570\u7ec4\" \/><\/p>\n<p><p> \u8981\u5728Python\u4e2d\u521b\u5efa\u968f\u673a\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528NumPy\u5e93\u3001random\u6a21\u5757\u7b49\u3002\u5176\u4e2d<strong>NumPy\u5e93\u662f\u6700\u4e3a\u5e38\u89c1\u548c\u9ad8\u6548\u7684\u9009\u62e9<\/strong>\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528NumPy\u5e93<\/strong>\uff1aNumPy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u51fd\u6570\u6765\u751f\u6210\u5404\u79cd\u7c7b\u578b\u7684\u968f\u673a\u6570\u7ec4\u3002<\/li>\n<li><strong>\u4f7f\u7528random\u6a21\u5757<\/strong>\uff1aPython\u81ea\u5e26\u7684random\u6a21\u5757\u4e5f\u53ef\u4ee5\u751f\u6210\u968f\u673a\u6570\uff0c\u4f46\u751f\u6210\u6570\u7ec4\u7684\u529f\u80fd\u76f8\u5bf9\u6709\u9650\u3002<\/li>\n<\/ol>\n<p><p>\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528NumPy\u5e93\u6765\u521b\u5efa\u968f\u673a\u6570\u7ec4\uff0c\u5e76\u4e3e\u4f8b\u8bf4\u660e\u5982\u4f55\u4f7f\u7528random\u6a21\u5757\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u4f7f\u7528NumPy\u5e93<\/strong><\/h2>\n<p><h2>1\u3001\u5b89\u88c5NumPy\u5e93<\/h2>\n<\/p>\n<p><p>\u5728\u4f7f\u7528NumPy\u4e4b\u524d\uff0c\u9700\u8981\u5148\u5b89\u88c5\u8fd9\u4e2a\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001\u751f\u6210\u968f\u673a\u6570\u7ec4<\/h2>\n<\/p>\n<p><h3>2.1 \u968f\u673a\u751f\u6210\u5747\u5300\u5206\u5e03\u7684\u6570\u7ec4<\/h3>\n<\/p>\n<p><p>NumPy\u7684<code>numpy.random.rand<\/code>\u51fd\u6570\u53ef\u4ee5\u751f\u6210\u5747\u5300\u5206\u5e03\u7684\u968f\u673a\u6570\u7ec4\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a\u5f62\u72b6\u4e3a(3, 4)\u7684\u968f\u673a\u6570\u7ec4\uff0c\u503c\u57280\u52301\u4e4b\u95f4<\/strong><\/h2>\n<p>array = np.random.rand(3, 4)<\/p>\n<p>print(&quot;\u968f\u673a\u751f\u6210\u7684\u5747\u5300\u5206\u5e03\u6570\u7ec4\uff1a&quot;)<\/p>\n<p>print(array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2.2 \u968f\u673a\u751f\u6210\u6b63\u6001\u5206\u5e03\u7684\u6570\u7ec4<\/h3>\n<\/p>\n<p><p><code>numpy.random.randn<\/code>\u51fd\u6570\u53ef\u4ee5\u751f\u6210\u7b26\u5408\u6807\u51c6\u6b63\u6001\u5206\u5e03\uff08\u5747\u503c\u4e3a0\uff0c\u65b9\u5dee\u4e3a1\uff09\u7684\u968f\u673a\u6570\u7ec4\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a\u5f62\u72b6\u4e3a(3, 4)\u7684\u968f\u673a\u6570\u7ec4\uff0c\u7b26\u5408\u6807\u51c6\u6b63\u6001\u5206\u5e03<\/strong><\/h2>\n<p>array = np.random.randn(3, 4)<\/p>\n<p>print(&quot;\u968f\u673a\u751f\u6210\u7684\u6b63\u6001\u5206\u5e03\u6570\u7ec4\uff1a&quot;)<\/p>\n<p>print(array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2.3 \u968f\u673a\u751f\u6210\u6307\u5b9a\u8303\u56f4\u7684\u6574\u6570\u6570\u7ec4<\/h3>\n<\/p>\n<p><p><code>numpy.random.randint<\/code>\u51fd\u6570\u53ef\u4ee5\u751f\u6210\u6307\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6574\u6570\u6570\u7ec4\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a\u5f62\u72b6\u4e3a(3, 4)\u7684\u968f\u673a\u6574\u6570\u6570\u7ec4\uff0c\u503c\u57280\u523010\u4e4b\u95f4<\/strong><\/h2>\n<p>array = np.random.randint(0, 10, (3, 4))<\/p>\n<p>print(&quot;\u968f\u673a\u751f\u6210\u7684\u6574\u6570\u6570\u7ec4\uff1a&quot;)<\/p>\n<p>print(array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2.4 \u968f\u673a\u751f\u6210\u81ea\u5b9a\u4e49\u5206\u5e03\u7684\u6570\u7ec4<\/h3>\n<\/p>\n<p><p><code>numpy.random.choice<\/code>\u51fd\u6570\u53ef\u4ee5\u4ece\u7ed9\u5b9a\u7684\u4e00\u7ef4\u6570\u7ec4\u4e2d\u751f\u6210\u968f\u673a\u6570\u7ec4\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u4ece\u7ed9\u5b9a\u7684\u4e00\u7ef4\u6570\u7ec4\u4e2d\u968f\u673a\u9009\u62e9\u5143\u7d20\uff0c\u751f\u6210\u5f62\u72b6\u4e3a(3, 4)\u7684\u968f\u673a\u6570\u7ec4<\/strong><\/h2>\n<p>array = np.random.choice([10, 20, 30, 40, 50], size=(3, 4))<\/p>\n<p>print(&quot;\u968f\u673a\u751f\u6210\u7684\u81ea\u5b9a\u4e49\u5206\u5e03\u6570\u7ec4\uff1a&quot;)<\/p>\n<p>print(array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2.5 \u968f\u673a\u751f\u6210\u7b26\u5408\u7279\u5b9a\u5206\u5e03\u7684\u6570\u7ec4<\/h3>\n<\/p>\n<p><p>NumPy\u8fd8\u63d0\u4f9b\u4e86\u8bb8\u591a\u51fd\u6570\u6765\u751f\u6210\u7b26\u5408\u7279\u5b9a\u5206\u5e03\u7684\u968f\u673a\u6570\u7ec4\uff0c\u4f8b\u5982\u5747\u5300\u5206\u5e03\u3001\u6b63\u6001\u5206\u5e03\u3001\u6cca\u677e\u5206\u5e03\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u751f\u6210\u5747\u5300\u5206\u5e03\u7684\u968f\u673a\u6570\u7ec4<\/strong><\/h2>\n<p>uniform_array = np.random.uniform(low=0.0, high=1.0, size=(3, 4))<\/p>\n<p>print(&quot;\u5747\u5300\u5206\u5e03\u7684\u968f\u673a\u6570\u7ec4\uff1a&quot;)<\/p>\n<p>print(uniform_array)<\/p>\n<h2><strong>\u751f\u6210\u6b63\u6001\u5206\u5e03\u7684\u968f\u673a\u6570\u7ec4<\/strong><\/h2>\n<p>normal_array = np.random.normal(loc=0.0, scale=1.0, size=(3, 4))<\/p>\n<p>print(&quot;\u6b63\u6001\u5206\u5e03\u7684\u968f\u673a\u6570\u7ec4\uff1a&quot;)<\/p>\n<p>print(normal_array)<\/p>\n<h2><strong>\u751f\u6210\u6cca\u677e\u5206\u5e03\u7684\u968f\u673a\u6570\u7ec4<\/strong><\/h2>\n<p>poisson_array = np.random.poisson(lam=5.0, size=(3, 4))<\/p>\n<p>print(&quot;\u6cca\u677e\u5206\u5e03\u7684\u968f\u673a\u6570\u7ec4\uff1a&quot;)<\/p>\n<p>print(poisson_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e8c\u3001\u4f7f\u7528random\u6a21\u5757<\/strong><\/h2>\n<p><p>Python\u81ea\u5e26\u7684random\u6a21\u5757\u4e5f\u53ef\u4ee5\u751f\u6210\u968f\u673a\u6570\uff0c\u4f46\u751f\u6210\u6570\u7ec4\u7684\u529f\u80fd\u4e0d\u5982NumPy\u4e30\u5bcc\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u751f\u6210\u968f\u673a\u6574\u6570\u6570\u7ec4<\/h2>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528random\u6a21\u5757\u7684<code>random.randint<\/code>\u51fd\u6570\u751f\u6210\u968f\u673a\u6574\u6570\uff0c\u7136\u540e\u5c06\u5176\u6dfb\u52a0\u5230\u5217\u8868\u4e2d\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a\u5f62\u72b6\u4e3a(3, 4)\u7684\u968f\u673a\u6574\u6570\u6570\u7ec4\uff0c\u503c\u57280\u523010\u4e4b\u95f4<\/strong><\/h2>\n<p>array = [[random.randint(0, 10) for _ in range(4)] for _ in range(3)]<\/p>\n<p>print(&quot;\u968f\u673a\u751f\u6210\u7684\u6574\u6570\u6570\u7ec4\uff1a&quot;)<\/p>\n<p>print(array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001\u751f\u6210\u968f\u673a\u6d6e\u70b9\u6570\u6570\u7ec4<\/h2>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528random\u6a21\u5757\u7684<code>random.uniform<\/code>\u51fd\u6570\u751f\u6210\u968f\u673a\u6d6e\u70b9\u6570\uff0c\u7136\u540e\u5c06\u5176\u6dfb\u52a0\u5230\u5217\u8868\u4e2d\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a\u5f62\u72b6\u4e3a(3, 4)\u7684\u968f\u673a\u6d6e\u70b9\u6570\u6570\u7ec4\uff0c\u503c\u57280\u52301\u4e4b\u95f4<\/strong><\/h2>\n<p>array = [[random.uniform(0, 1) for _ in range(4)] for _ in range(3)]<\/p>\n<p>print(&quot;\u968f\u673a\u751f\u6210\u7684\u6d6e\u70b9\u6570\u6570\u7ec4\uff1a&quot;)<\/p>\n<p>print(array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>3\u3001\u4ece\u7ed9\u5b9a\u5217\u8868\u4e2d\u968f\u673a\u9009\u62e9\u5143\u7d20\u751f\u6210\u6570\u7ec4<\/h2>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528random\u6a21\u5757\u7684<code>random.choice<\/code>\u51fd\u6570\u4ece\u7ed9\u5b9a\u5217\u8868\u4e2d\u968f\u673a\u9009\u62e9\u5143\u7d20\uff0c\u7136\u540e\u5c06\u5176\u6dfb\u52a0\u5230\u5217\u8868\u4e2d\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<h2><strong>\u4ece\u7ed9\u5b9a\u5217\u8868\u4e2d\u968f\u673a\u9009\u62e9\u5143\u7d20\uff0c\u751f\u6210\u5f62\u72b6\u4e3a(3, 4)\u7684\u968f\u673a\u6570\u7ec4<\/strong><\/h2>\n<p>choices = [10, 20, 30, 40, 50]<\/p>\n<p>array = [[random.choice(choices) for _ in range(4)] for _ in range(3)]<\/p>\n<p>print(&quot;\u968f\u673a\u751f\u6210\u7684\u81ea\u5b9a\u4e49\u5206\u5e03\u6570\u7ec4\uff1a&quot;)<\/p>\n<p>print(array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e09\u3001NumPy\u4e0erandom\u6a21\u5757\u7684\u5bf9\u6bd4<\/strong><\/h2>\n<p><h2>1\u3001\u529f\u80fd\u4e30\u5bcc\u6027<\/h2>\n<\/p>\n<p><p><strong>NumPy\u5e93<\/strong>\u63d0\u4f9b\u4e86\u66f4\u52a0\u4e30\u5bcc\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u751f\u6210\u5404\u79cd\u7c7b\u578b\u7684\u968f\u673a\u6570\u7ec4\uff0c\u5305\u62ec\u5747\u5300\u5206\u5e03\u3001\u6b63\u6001\u5206\u5e03\u3001\u6cca\u677e\u5206\u5e03\u7b49\u3002\u800c<strong>random\u6a21\u5757<\/strong>\u7684\u529f\u80fd\u76f8\u5bf9\u6709\u9650\uff0c\u4e3b\u8981\u7528\u4e8e\u751f\u6210\u968f\u673a\u6574\u6570\u548c\u6d6e\u70b9\u6570\u3002<\/p>\n<\/p>\n<p><h2>2\u3001\u6027\u80fd<\/h2>\n<\/p>\n<p><p>\u5728\u751f\u6210\u5927\u89c4\u6a21\u7684\u968f\u673a\u6570\u7ec4\u65f6\uff0c<strong>NumPy\u5e93<\/strong>\u7684\u6027\u80fd\u66f4\u9ad8\uff0c\u56e0\u4e3a\u5b83\u662f\u4e13\u95e8\u4e3a\u79d1\u5b66\u8ba1\u7b97\u8bbe\u8ba1\u7684\uff0c\u5e95\u5c42\u4f7f\u7528\u4e86\u9ad8\u6548\u7684C\u8bed\u8a00\u5b9e\u73b0\u3002\u800c<strong>random\u6a21\u5757<\/strong>\u5728\u751f\u6210\u5c0f\u89c4\u6a21\u968f\u673a\u6570\u65f6\u6027\u80fd\u5dee\u5f02\u4e0d\u5927\uff0c\u4f46\u5728\u5927\u89c4\u6a21\u6570\u636e\u751f\u6210\u65f6\u53ef\u80fd\u4f1a\u663e\u5f97\u6548\u7387\u8f83\u4f4e\u3002<\/p>\n<\/p>\n<p><h2>3\u3001\u6613\u7528\u6027<\/h2>\n<\/p>\n<p><p>\u5bf9\u4e8e\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\u4efb\u52a1\uff0c<strong>NumPy\u5e93<\/strong>\u66f4\u52a0\u6613\u7528\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u51fd\u6570\u548c\u65b9\u6cd5\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u5404\u79cd\u64cd\u4f5c\u3002\u800c<strong>random\u6a21\u5757<\/strong>\u66f4\u9002\u5408\u7528\u4e8e\u7b80\u5355\u7684\u968f\u673a\u6570\u751f\u6210\u4efb\u52a1\u3002<\/p>\n<\/p>\n<h2><strong>\u56db\u3001\u603b\u7ed3<\/strong><\/h2>\n<p><p>\u5728Python\u4e2d\u521b\u5efa\u968f\u673a\u6570\u7ec4\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u548crandom\u6a21\u5757\u3002<strong>NumPy\u5e93\u63d0\u4f9b\u4e86\u66f4\u52a0\u4e30\u5bcc\u548c\u9ad8\u6548\u7684\u51fd\u6570<\/strong>\uff0c\u9002\u5408\u751f\u6210\u5404\u79cd\u7c7b\u578b\u7684\u968f\u673a\u6570\u7ec4\uff0c\u7279\u522b\u662f\u5728\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\u4e2d\u3002<strong>random\u6a21\u5757\u66f4\u9002\u7528\u4e8e\u7b80\u5355\u7684\u968f\u673a\u6570\u751f\u6210\u4efb\u52a1<\/strong>\u3002\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u9700\u6c42\u548c\u5e94\u7528\u573a\u666f\u3002\u901a\u8fc7\u5408\u7406\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u751f\u6210\u5404\u79cd\u7c7b\u578b\u7684\u968f\u673a\u6570\u7ec4\uff0c\u4ee5\u6ee1\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u751f\u6210\u5305\u542b\u7279\u5b9a\u8303\u56f4\u5185\u968f\u673a\u6570\u7684\u6570\u7ec4\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u6765\u521b\u5efa\u5305\u542b\u7279\u5b9a\u8303\u56f4\u5185\u968f\u673a\u6570\u7684\u6570\u7ec4\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528<code>numpy.random.randint(low, high, size)<\/code>\u51fd\u6570\u751f\u6210\u6307\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6574\u6570\uff0c\u6216\u8005\u4f7f\u7528<code>numpy.random.uniform(low, high, size)<\/code>\u751f\u6210\u6307\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6d6e\u70b9\u6570\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u60f3\u751f\u6210\u4e00\u4e2a\u5305\u542b10\u4e2a\u968f\u673a\u6574\u6570\u7684\u6570\u7ec4\uff0c\u8303\u56f4\u57281\u5230100\u4e4b\u95f4\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\nrandom_array = np.random.randint(1, 100, size=10)\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u4f7f\u7528Python\u751f\u6210\u591a\u7ef4\u968f\u673a\u6570\u7ec4\uff1f<\/strong><br \/>\u5229\u7528NumPy\u5e93\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u521b\u5efa\u591a\u7ef4\u968f\u673a\u6570\u7ec4\u3002\u901a\u8fc7\u5728<code>size<\/code>\u53c2\u6570\u4e2d\u6307\u5b9a\u4e00\u4e2a\u5143\u7ec4\uff0c\u60a8\u53ef\u4ee5\u751f\u6210\u4efb\u610f\u7ef4\u5ea6\u7684\u968f\u673a\u6570\u7ec4\u3002\u4f8b\u5982\uff0c\u8981\u751f\u6210\u4e00\u4e2a3&#215;3\u7684\u968f\u673a\u6d6e\u70b9\u6570\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528<code>numpy.random.random(size=(3, 3))<\/code>\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\nrandom_2d_array = np.random.random(size=(3, 3))\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u8fd4\u56de\u4e00\u4e2a\u5305\u542b3\u884c3\u5217\u7684\u6570\u7ec4\uff0c\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u4e3a0\u52301\u4e4b\u95f4\u7684\u968f\u673a\u6d6e\u70b9\u6570\u3002<\/p>\n<p><strong>\u5982\u4f55\u786e\u4fdd\u751f\u6210\u7684\u968f\u673a\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u4e0d\u91cd\u590d\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u5e0c\u671b\u751f\u6210\u4e00\u4e2a\u4e0d\u5305\u542b\u91cd\u590d\u5143\u7d20\u7684\u968f\u673a\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528<code>numpy.random.choice<\/code>\u51fd\u6570\uff0c\u5e76\u8bbe\u7f6e<code>replace=False<\/code>\u53c2\u6570\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u60f3\u4ece1\u523050\u4e2d\u9009\u62e910\u4e2a\u4e0d\u91cd\u590d\u7684\u968f\u673a\u6570\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\nunique_random_array = np.random.choice(range(1, 51), size=10, replace=False)\n<\/code><\/pre>\n<p>\u6b64\u4ee3\u7801\u5c06\u751f\u6210\u4e00\u4e2a\u5305\u542b10\u4e2a\u4e0d\u91cd\u590d\u968f\u673a\u6574\u6570\u7684\u6570\u7ec4\uff0c\u786e\u4fdd\u6bcf\u4e2a\u6570\u5b57\u53ea\u51fa\u73b0\u4e00\u6b21\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u521b\u5efa\u968f\u673a\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528NumPy\u5e93\u3001random\u6a21\u5757\u7b49\u3002\u5176\u4e2dNumPy [&hellip;]","protected":false},"author":3,"featured_media":1167886,"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\/1167877"}],"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=1167877"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1167877\/revisions"}],"predecessor-version":[{"id":1167887,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1167877\/revisions\/1167887"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1167886"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1167877"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1167877"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1167877"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}