{"id":1062029,"date":"2024-12-31T15:48:05","date_gmt":"2024-12-31T07:48:05","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1062029.html"},"modified":"2024-12-31T15:48:07","modified_gmt":"2024-12-31T07:48:07","slug":"python%e5%a6%82%e4%bd%95%e9%9a%8f%e6%9c%ba%e9%80%89%e5%8f%96%e5%ad%97%e7%ac%a6%e4%b8%b2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1062029.html","title":{"rendered":"python\u5982\u4f55\u968f\u673a\u9009\u53d6\u5b57\u7b26\u4e32"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/41ddcdd0-4dae-49ce-8c1b-1d17b5ad68b1.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u968f\u673a\u9009\u53d6\u5b57\u7b26\u4e32\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u968f\u673a\u9009\u53d6\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u4e3b\u8981\u5305\u62ec\uff1a\u4f7f\u7528<code>random.choice()<\/code>\u51fd\u6570\u3001\u4f7f\u7528<code>random.choices()<\/code>\u51fd\u6570\u3001\u4f7f\u7528<code>secrets.choice()<\/code>\u51fd\u6570\u3002\u8fd9\u4e9b\u65b9\u6cd5\u90fd\u662f\u57fa\u4e8ePython\u7684\u6807\u51c6\u5e93<code>random<\/code>\u548c<code>secrets<\/code>\u5b9e\u73b0\u7684\u3002<\/strong> \u5176\u4e2d\uff0c<code>random.choice()<\/code>\u51fd\u6570\u662f\u6700\u5e38\u7528\u7684\u65b9\u5f0f\uff0c\u5b83\u53ef\u4ee5\u4ece\u4e00\u4e2a\u5e8f\u5217\u4e2d\u968f\u673a\u9009\u53d6\u4e00\u4e2a\u5143\u7d20\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0<code>random.choice()<\/code>\u51fd\u6570\u7684\u4f7f\u7528\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p><strong>1\u3001\u4f7f\u7528random.choice()\u51fd\u6570<\/strong><\/p>\n<\/p>\n<p><p><code>random.choice()<\/code>\u51fd\u6570\u662fPython\u4e2d\u7528\u6765\u4ece\u4e00\u4e2a\u975e\u7a7a\u5e8f\u5217\u4e2d\u968f\u673a\u9009\u53d6\u4e00\u4e2a\u5143\u7d20\u7684\u51fd\u6570\u3002\u4ee5\u4e0b\u662f\u5b83\u7684\u57fa\u672c\u7528\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>string = &quot;Hello, World!&quot;<\/p>\n<h2><strong>\u4ece\u5b57\u7b26\u4e32\u4e2d\u968f\u673a\u9009\u53d6\u4e00\u4e2a\u5b57\u7b26<\/strong><\/h2>\n<p>random_char = random.choice(string)<\/p>\n<p>print(random_char)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86<code>random<\/code>\u6a21\u5757\uff0c\u7136\u540e\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5b57\u7b26\u4e32<code>string<\/code>\uff0c\u63a5\u7740\u4f7f\u7528<code>random.choice()<\/code>\u51fd\u6570\u4ece\u5b57\u7b26\u4e32\u4e2d\u968f\u673a\u9009\u53d6\u4e00\u4e2a\u5b57\u7b26\uff0c\u5e76\u5c06\u5176\u8d4b\u503c\u7ed9<code>random_char<\/code>\u53d8\u91cf\uff0c\u6700\u540e\u6253\u5370\u51fa\u968f\u673a\u9009\u53d6\u7684\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><p><strong>2\u3001\u4f7f\u7528random.choices()\u51fd\u6570<\/strong><\/p>\n<\/p>\n<p><p><code>random.choices()<\/code>\u51fd\u6570\u53ef\u4ee5\u4ece\u4e00\u4e2a\u5e8f\u5217\u4e2d\u968f\u673a\u9009\u53d6\u591a\u4e2a\u5143\u7d20\uff0c\u5e76\u4e14\u53ef\u4ee5\u6307\u5b9a\u9009\u53d6\u7684\u5143\u7d20\u4e2a\u6570\u3002\u4ee5\u4e0b\u662f\u5b83\u7684\u57fa\u672c\u7528\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>string = &quot;Hello, World!&quot;<\/p>\n<h2><strong>\u4ece\u5b57\u7b26\u4e32\u4e2d\u968f\u673a\u9009\u53d6\u4e09\u4e2a\u5b57\u7b26<\/strong><\/h2>\n<p>random_chars = random.choices(string, k=3)<\/p>\n<p>print(random_chars)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>random.choices()<\/code>\u51fd\u6570\u4ece\u5b57\u7b26\u4e32\u4e2d\u968f\u673a\u9009\u53d6\u4e09\u4e2a\u5b57\u7b26\uff0c\u5e76\u5c06\u5176\u8d4b\u503c\u7ed9<code>random_chars<\/code>\u53d8\u91cf\u3002<code>k<\/code>\u53c2\u6570\u7528\u4e8e\u6307\u5b9a\u9009\u53d6\u7684\u5143\u7d20\u4e2a\u6570\u3002<\/p>\n<\/p>\n<p><p><strong>3\u3001\u4f7f\u7528secrets.choice()\u51fd\u6570<\/strong><\/p>\n<\/p>\n<p><p><code>secrets.choice()<\/code>\u51fd\u6570\u662fPython 3.6\u5f15\u5165\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u7528\u4e8e\u4ece\u4e00\u4e2a\u5e8f\u5217\u4e2d\u968f\u673a\u9009\u53d6\u4e00\u4e2a\u5143\u7d20\u3002\u4e0e<code>random.choice()<\/code>\u51fd\u6570\u76f8\u6bd4\uff0c<code>secrets.choice()<\/code>\u51fd\u6570\u4f7f\u7528\u7684\u662f\u66f4\u5b89\u5168\u7684\u968f\u673a\u6570\u751f\u6210\u5668\u3002\u4ee5\u4e0b\u662f\u5b83\u7684\u57fa\u672c\u7528\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import secrets<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>string = &quot;Hello, World!&quot;<\/p>\n<h2><strong>\u4ece\u5b57\u7b26\u4e32\u4e2d\u968f\u673a\u9009\u53d6\u4e00\u4e2a\u5b57\u7b26<\/strong><\/h2>\n<p>random_char = secrets.choice(string)<\/p>\n<p>print(random_char)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>secrets.choice()<\/code>\u51fd\u6570\u4ece\u5b57\u7b26\u4e32\u4e2d\u968f\u673a\u9009\u53d6\u4e00\u4e2a\u5b57\u7b26\uff0c\u5e76\u5c06\u5176\u8d4b\u503c\u7ed9<code>random_char<\/code>\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><p><strong>4\u3001\u7ed3\u5408random.sample()\u51fd\u6570<\/strong><\/p>\n<\/p>\n<p><p><code>random.sample()<\/code>\u51fd\u6570\u53ef\u4ee5\u4ece\u4e00\u4e2a\u5e8f\u5217\u4e2d\u968f\u673a\u9009\u53d6\u6307\u5b9a\u6570\u91cf\u7684\u4e0d\u91cd\u590d\u5143\u7d20\u3002\u4ee5\u4e0b\u662f\u5b83\u7684\u57fa\u672c\u7528\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>string = &quot;Hello, World!&quot;<\/p>\n<h2><strong>\u4ece\u5b57\u7b26\u4e32\u4e2d\u968f\u673a\u9009\u53d6\u4e09\u4e2a\u4e0d\u91cd\u590d\u7684\u5b57\u7b26<\/strong><\/h2>\n<p>random_chars = random.sample(string, 3)<\/p>\n<p>print(random_chars)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>random.sample()<\/code>\u51fd\u6570\u4ece\u5b57\u7b26\u4e32\u4e2d\u968f\u673a\u9009\u53d6\u4e09\u4e2a\u4e0d\u91cd\u590d\u7684\u5b57\u7b26\uff0c\u5e76\u5c06\u5176\u8d4b\u503c\u7ed9<code>random_chars<\/code>\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><p><strong>5\u3001\u7ed3\u5408numpy\u5e93<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u9700\u8981\u4ece\u5b57\u7b26\u4e32\u4e2d\u968f\u673a\u9009\u53d6\u5143\u7d20\uff0c\u5e76\u4e14\u8981\u8fdb\u884c\u5927\u91cf\u7684\u968f\u673a\u9009\u53d6\u64cd\u4f5c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>numpy<\/code>\u5e93\u4e2d\u7684<code>numpy.random.choice()<\/code>\u51fd\u6570\u3002\u4ee5\u4e0b\u662f\u5b83\u7684\u57fa\u672c\u7528\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>string = &quot;Hello, World!&quot;<\/p>\n<h2><strong>\u4ece\u5b57\u7b26\u4e32\u4e2d\u968f\u673a\u9009\u53d6\u4e09\u4e2a\u5b57\u7b26<\/strong><\/h2>\n<p>random_chars = np.random.choice(list(string), 3)<\/p>\n<p>print(random_chars)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>numpy.random.choice()<\/code>\u51fd\u6570\u4ece\u5b57\u7b26\u4e32\u4e2d\u968f\u673a\u9009\u53d6\u4e09\u4e2a\u5b57\u7b26\uff0c\u5e76\u5c06\u5176\u8d4b\u503c\u7ed9<code>random_chars<\/code>\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><p><strong>6\u3001\u603b\u7ed3<\/strong><\/p>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ecb\u7ecd\u4e86Python\u4e2d\u968f\u673a\u9009\u53d6\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528<code>random.choice()<\/code>\u51fd\u6570\u3001<code>random.choices()<\/code>\u51fd\u6570\u3001<code>secrets.choice()<\/code>\u51fd\u6570\u3001<code>random.sample()<\/code>\u51fd\u6570\u4ee5\u53ca\u7ed3\u5408<code>numpy<\/code>\u5e93\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u5177\u4f53\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u6765\u51b3\u5b9a\u3002\u5982\u679c\u4f60\u9700\u8981\u4ece\u4e00\u4e2a\u5e8f\u5217\u4e2d\u968f\u673a\u9009\u53d6\u4e00\u4e2a\u5143\u7d20\uff0c<code>random.choice()<\/code>\u51fd\u6570\u662f\u6700\u7b80\u5355\u548c\u5e38\u7528\u7684\u65b9\u6cd5\u3002\u5982\u679c\u4f60\u9700\u8981\u4ece\u4e00\u4e2a\u5e8f\u5217\u4e2d\u968f\u673a\u9009\u53d6\u591a\u4e2a\u5143\u7d20\uff0c\u5e76\u4e14\u53ef\u4ee5\u6307\u5b9a\u9009\u53d6\u7684\u5143\u7d20\u4e2a\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528<code>random.choices()<\/code>\u51fd\u6570\u3002\u5982\u679c\u4f60\u9700\u8981\u4ece\u4e00\u4e2a\u5e8f\u5217\u4e2d\u968f\u673a\u9009\u53d6\u4e0d\u91cd\u590d\u7684\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528<code>random.sample()<\/code>\u51fd\u6570\u3002\u5982\u679c\u4f60\u9700\u8981\u8fdb\u884c\u5927\u91cf\u7684\u968f\u673a\u9009\u53d6\u64cd\u4f5c\uff0c\u53ef\u4ee5\u7ed3\u5408<code>numpy<\/code>\u5e93\u4f7f\u7528<code>numpy.random.choice()<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4ece\u5b57\u7b26\u4e32\u4e2d\u9009\u62e9\u968f\u673a\u5b57\u7b26\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>random<\/code>\u6a21\u5757\u4e2d\u7684<code>choice()<\/code>\u51fd\u6570\u4ece\u5b57\u7b26\u4e32\u4e2d\u968f\u673a\u9009\u62e9\u4e00\u4e2a\u5b57\u7b26\u3002\u9996\u5148\uff0c\u5bfc\u5165<code>random<\/code>\u6a21\u5757\uff0c\u7136\u540e\u8c03\u7528<code>random.choice()<\/code>\uff0c\u5e76\u5c06\u76ee\u6807\u5b57\u7b26\u4e32\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\u3002\u4f8b\u5982\uff0c<code>random.choice(&quot;hello&quot;)<\/code>\u5c06\u968f\u673a\u8fd4\u56de&#39; h&#39;\u3001&#39;e&#39;\u3001&#39;l&#39;\u3001&#39;o&#39;\u4e2d\u7684\u4e00\u4e2a\u5b57\u7b26\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u4ece\u5b57\u7b26\u4e32\u5217\u8868\u4e2d\u968f\u673a\u9009\u62e9\u4e00\u4e2a\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u4f7f\u7528<code>random<\/code>\u6a21\u5757\u7684<code>choice()<\/code>\u51fd\u6570\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5b57\u7b26\u4e32\u5217\u8868\u3002\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u5b57\u7b26\u4e32\u7684\u5217\u8868\uff0c\u7136\u540e\u4f7f\u7528<code>random.choice()<\/code>\u4ece\u4e2d\u968f\u673a\u9009\u62e9\u4e00\u4e2a\u3002\u4f8b\u5982\uff0c<code>random.choice([&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;])<\/code>\u5c06\u968f\u673a\u8fd4\u56de\u8fd9\u4e09\u4e2a\u5b57\u7b26\u4e32\u4e2d\u7684\u4e00\u4e2a\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u8bbe\u7f6e\u968f\u673a\u9009\u62e9\u7684\u79cd\u5b50\uff0c\u4ee5\u4fbf\u6bcf\u6b21\u9009\u62e9\u90fd\u76f8\u540c\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7<code>random.seed()<\/code>\u51fd\u6570\u8bbe\u7f6e\u968f\u673a\u6570\u751f\u6210\u5668\u7684\u79cd\u5b50\u3002\u8fd9\u6837\u505a\u53ef\u4ee5\u786e\u4fdd\u6bcf\u6b21\u8fd0\u884c\u7a0b\u5e8f\u65f6\uff0c\u968f\u673a\u9009\u62e9\u7684\u7ed3\u679c\u90fd\u662f\u76f8\u540c\u7684\u3002\u4f8b\u5982\uff0c<code>random.seed(1)<\/code>\u540e\u8c03\u7528<code>random.choice()<\/code>\u5c06\u59cb\u7ec8\u8fd4\u56de\u76f8\u540c\u7684\u7ed3\u679c\u3002\u8fd9\u5728\u8c03\u8bd5\u6216\u9700\u8981\u53ef\u91cd\u590d\u7ed3\u679c\u7684\u573a\u666f\u4e2d\u7279\u522b\u6709\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u968f\u673a\u9009\u53d6\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u4e3b\u8981\u5305\u62ec\uff1a\u4f7f\u7528random.choice()\u51fd\u6570\u3001\u4f7f\u7528random. [&hellip;]","protected":false},"author":3,"featured_media":1062038,"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\/1062029"}],"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=1062029"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1062029\/revisions"}],"predecessor-version":[{"id":1062042,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1062029\/revisions\/1062042"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1062038"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1062029"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1062029"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1062029"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}