{"id":8732,"date":"2023-05-16T16:37:35","date_gmt":"2023-05-16T08:37:35","guid":{"rendered":"\/?p=8732"},"modified":"2023-05-16T16:37:35","modified_gmt":"2023-05-16T08:37:35","slug":"python%e7%94%9f%e6%88%90%e9%9a%8f%e6%9c%ba%e6%95%b0-%e9%9a%8f%e6%9c%ba%e5%ad%97%e7%ac%a6%e4%b8%b2","status":"publish","type":"post","link":"\/?p=8732","title":{"rendered":"python\u751f\u6210\u968f\u673a\u6570\/\u968f\u673a\u5b57\u7b26\u4e32"},"content":{"rendered":"<p><strong>\u5728\u5229\u7528python\u4ee3\u7801\u8fdb\u884c\u5f00\u53d1\u6216\u8005\u6d4b\u8bd5\u4e2d\uff0c\u96be\u514d\u4f1a\u9047\u5230\u4e00\u4e9b\u9700\u8981\u8f93\u5165\u968f\u673a\u503c\u7684\u76f8\u5173\u64cd\u4f5c\uff0c\u8fd9\u91cc\u7279\u610f\u603b\u7ed3\u4e86\u4e00\u4e0b\uff1a<\/strong><\/p>\n<h3>\u4e00\u3001\u968f\u673a\u6570\u5b57\u7684\u751f\u6210<\/h3>\n<p><strong>\uff081\uff09\u751f\u62100-9\uff08\u6216\u5176\u4ed6\u6570\u5b57\u6bb5\uff09\u4e2d\u7684\u4e00\u4e2a\uff08\u4e32\uff09\u968f\u673a\u6570\u5b57<\/strong><\/p>\n<pre><code class=\"language-python\">import random\ndef create_random_int_number():\n    &quot;&quot;&quot;\u968f\u673a\u8fd4\u56de\u4ece0-9\u4e4b\u95f4\u7684\u6574\u6570&quot;&quot;&quot;\n    return random.randint(0, 9)\n    #\u4e5f\u53ef\u4ee5\u6839\u636e\u7eed\u4fee\u8fd4\u56de\u5176\u4ed6\u957f\u5ea6\u7684\u6570\u5b57\uff0c\u8fd9\u91cc\u6211\u8fd4\u56de1000-9999\u4e2d\u7684\u4efb\u610f\u4e00\u4e2a\u6570\u5b57\n    # return random.randint(1000, 9999)\n\nprint(create_random_int_number())<\/code><\/pre>\n<p><em>\u8fd0\u884c\u7ed3\u679c\uff1a8<\/em><\/p>\n<p><strong>\uff082\uff09\u751f\u62100-9999\uff08\u6216\u5176\u4ed6\u6570\u5b57\u6bb5\uff09\u4e4b\u95f4\u7684\u4efb\u610f\u4e00\u4e2a\u6d6e\u70b9\u6570<\/strong><\/p>\n<pre><code class=\"language-python\">import random\ndef create_random_float_number():\n    &quot;&quot;&quot;\u968f\u673a\u8fd4\u56de\u4ece0-9999\u4e4b\u95f4\u7684\u6d6e\u70b9\u6570&quot;&quot;&quot;\n    return random.uniform(0, 9999)\n\nprint(create_random_float_number())<\/code><\/pre>\n<p><em>\u8fd0\u884c\u7ed3\u679c\uff1a9393.133900249248<\/em><\/p>\n<p><strong>\uff083\uff09\u751f\u62100-1\u4e4b\u95f4\u7684\u4efb\u610f\u4e00\u4e2a\u6d6e\u70b9\u6570<\/strong><\/p>\n<pre><code class=\"language-python\"> import random\n def create_random_0_1_number():\n     &quot;&quot;&quot;\u968f\u673a\u8fd4\u56de\u4e00\u4e2a0-1\u4e4b\u95f4\u7684\u6d6e\u70b9\u6570&quot;&quot;&quot;\n     return random.random()\n\n print(create_random_0_1_number())<\/code><\/pre>\n<p>\u200b       <em>\u8fd0\u884c\u7ed3\u679c\uff1a0.0803613273312489<\/em><\/p>\n<p><strong>\uff084\uff09\u968f\u673a\u9009\u62e9\u6570\u7ec4\u4e2d\u4efb\u610f\u4e00\u4e2a\u6216\u51e0\u4e2a\u5143\u7d20<\/strong><\/p>\n<pre><code class=\"language-python\">import random\ndef create_random_samples_from_list():\n    &quot;&quot;&quot;\u968f\u673a\u4ecels\u6570\u7ec4\u4e2d\u90092\u4e2a\u5143\u7d20&quot;&quot;&quot;\n    ls = [&quot;aaa&quot;, 1, &#039;class&#039;, &#039;createrandomsamplesfromlist&#039;, 9990]\n    print(random.choice(ls))\n    return random.sample(ls, 2)\n\nprint(create_random_samples_from_list())<\/code><\/pre>\n<p><em>\u8fd0\u884c\u7ed3\u679c\uff1a[1, \u2018aaa\u2019]<\/em><\/p>\n<h3>\u4e8c\u3001\u968f\u673a\u5927\u5c0f\u5199\u5b57\u6bcd\u7684\u751f\u6210<\/h3>\n<p><strong>\uff081\uff09\u968f\u673a\u751f\u6210\u4e00\u4e2a\u5927\u5199\u6216\u5c0f\u5199\u7684\u82f1\u6587\u5b57\u6bcd<\/strong><\/p>\n<pre><code class=\"language-python\">import random\ndef create_random_string():\n    &quot;&quot;&quot;\u968f\u673a\u751f\u6210\u4e00\u4e2a\u5927\u5199\u6216\u5c0f\u5199\u7684\u82f1\u6587\u5b57\u6bcd&quot;&quot;&quot;\n    return random.choice(string.ascii_letters)\n\nprint(create_random_string())<\/code><\/pre>\n<p><em>\u8fd0\u884c\u7ed3\u679c\uff1aG<\/em><\/p>\n<p><strong>\uff082\uff09\u968f\u673a\u751f\u6210\u4e00\u4e32\u5305\u542b\u5927\u5199\u6216\u5c0f\u5199\u7684\u82f1\u6587\u5b57\u6bcd<\/strong><\/p>\n<pre><code class=\"language-python\">import random, string\ndef create_random_strings():\n    &quot;&quot;&quot;\u968f\u673a\u751f\u6210\u4e00\u4e32\u5305\u542b\u5927\u5199\u6216\u5c0f\u5199\u7684\u82f1\u6587\u5b57\u6bcd&quot;&quot;&quot;\n    return string.ascii_letters\n\nprint(create_random_strings())<\/code><\/pre>\n<p><em>\u8fd0\u884c\u7ed3\u679c\uff1aabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<\/em><\/p>\n<h3>\u4e09\u3001\u968f\u673a\u6307\u5b9a\u4f4d\u6570\u7684\u5b57\u7b26+\u6570\u7ec4\u6df7\u5408\u7684\u5b57\u7b26\u4e32<\/h3>\n<pre><code class=\"language-python\">import random, string\ndef create_string_number(n):\n    &quot;&quot;&quot;\u751f\u6210\u4e00\u4e32\u6307\u5b9a\u4f4d\u6570\u7684\u5b57\u7b26+\u6570\u7ec4\u6df7\u5408\u7684\u5b57\u7b26\u4e32&quot;&quot;&quot;\n    m = random.randint(1, n)\n    a = &quot;&quot;.join([str(random.randint(0, 9)) for _ in range(m)])\n    b = &quot;&quot;.join([random.choice(string.ascii_letters) for _ in range(n - m)])\n    return &#039;&#039;.join(random.sample(list(a + b), n))\n\nprint(create_string_number(9))<\/code><\/pre>\n<p><em>\u8fd0\u884c\u7ed3\u679c\uff1a148k808S4<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u5229\u7528python\u4ee3\u7801\u8fdb\u884c\u5f00\u53d1\u6216\u8005\u6d4b\u8bd5\u4e2d\uff0c\u96be\u514d\u4f1a\u9047\u5230\u4e00\u4e9b\u9700\u8981\u8f93\u5165\u968f\u673a\u503c\u7684\u76f8\u5173\u64cd\u4f5c\uff0c\u8fd9\u91cc\u7279\u610f\u603b\u7ed3\u4e86\u4e00\u4e0b\uff1a \u4e00\u3001\u968f\u673a\u6570\u5b57\u7684\u751f\u6210 \uff081\uff09\u751f\u62100   \u2026 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0},"categories":[53],"tags":[16],"_links":{"self":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts\/8732"}],"collection":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8732"}],"version-history":[{"count":1,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/8732\/revisions"}],"predecessor-version":[{"id":8733,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/8732\/revisions\/8733"}],"wp:attachment":[{"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8732"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8732"},{"taxonomy":"post_tag","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8732"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}