{"id":969948,"date":"2024-12-27T05:21:35","date_gmt":"2024-12-26T21:21:35","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/969948.html"},"modified":"2024-12-27T05:21:38","modified_gmt":"2024-12-26T21:21:38","slug":"python%e5%a6%82%e4%bd%95%e9%9a%8f%e6%9c%ba%e6%8e%92%e5%88%97%e5%ad%97%e5%85%b8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/969948.html","title":{"rendered":"python\u5982\u4f55\u968f\u673a\u6392\u5217\u5b57\u5178"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24184005\/542e3519-90b5-400b-9795-52bc7be1c01f.webp\" alt=\"python\u5982\u4f55\u968f\u673a\u6392\u5217\u5b57\u5178\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5c06\u5b57\u5178\u7684\u9879\u8f6c\u6362\u4e3a\u5217\u8868\u5e76\u4f7f\u7528<code>random.shuffle()<\/code>\u6765\u968f\u673a\u6392\u5217\u5b57\u5178\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\u5c06\u5b57\u5178\u7684\u9879\u8f6c\u6362\u4e3a\u5217\u8868\u3001\u5bf9\u5217\u8868\u8fdb\u884c\u968f\u673a\u6392\u5217\u3001\u518d\u5c06\u5217\u8868\u8f6c\u6362\u56de\u5b57\u5178\u3002\u9996\u5148\uff0c\u5c06\u5b57\u5178\u7684\u9879\u8f6c\u6362\u4e3a\u5217\u8868\uff0c\u7136\u540e\u4f7f\u7528<code>random.shuffle()<\/code>\u5bf9\u5217\u8868\u8fdb\u884c\u968f\u673a\u6392\u5217\uff0c\u6700\u540e\u5c06\u5217\u8868\u8f6c\u6362\u56de\u5b57\u5178\u3002<\/strong> \u8fd9\u79cd\u65b9\u6cd5\u5229\u7528\u4e86Python\u7684\u5185\u7f6e\u5e93<code>random<\/code>\uff0c\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5b9e\u73b0\u5b57\u5178\u9879\u7684\u968f\u673a\u6392\u5217\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5c06\u5b57\u5178\u7684\u9879\u8f6c\u6362\u4e3a\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5b57\u5178\u662f\u4e00\u79cd\u65e0\u5e8f\u7684\u6570\u636e\u7ed3\u6784\uff0c\u4f46\u6709\u65f6\u6211\u4eec\u5e0c\u671b\u5bf9\u5b57\u5178\u7684\u9879\u8fdb\u884c\u968f\u673a\u6392\u5217\u3002\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u7684\u7b2c\u4e00\u6b65\u662f\u5c06\u5b57\u5178\u7684\u9879\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5217\u8868\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>list()<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002\u901a\u8fc7\u5c06\u5b57\u5178\u7684\u9879\u8f6c\u6362\u4e3a\u5217\u8868\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u8fd9\u4e9b\u9879\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>\u5b57\u5178\u9879\u53ef\u4ee5\u901a\u8fc7<code>items()<\/code>\u65b9\u6cd5\u83b7\u53d6\uff0c\u5b83\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u952e\u503c\u5bf9\u7684\u89c6\u56fe\u5bf9\u8c61\u3002\u6211\u4eec\u53ef\u4ee5\u5c06\u8fd9\u4e2a\u89c6\u56fe\u5bf9\u8c61\u4f20\u9012\u7ed9<code>list()<\/code>\u51fd\u6570\uff0c\u4ece\u800c\u5f97\u5230\u4e00\u4e2a\u5305\u542b\u6240\u6709\u5b57\u5178\u9879\u7684\u5217\u8868\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3}<\/p>\n<p>items_list = list(my_dict.items())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u5bf9\u5217\u8868\u8fdb\u884c\u968f\u673a\u6392\u5217<\/h3>\n<\/p>\n<p><p>\u4e00\u65e6\u6211\u4eec\u5c06\u5b57\u5178\u7684\u9879\u8f6c\u6362\u4e3a\u5217\u8868\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u4f7f\u7528<code>random.shuffle()<\/code>\u51fd\u6570\u5bf9\u5217\u8868\u8fdb\u884c\u968f\u673a\u6392\u5217\u3002<code>random.shuffle()<\/code>\u662f<code>random<\/code>\u6a21\u5757\u4e2d\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u53ef\u4ee5\u5bf9\u5217\u8868\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u5c31\u5730\u968f\u673a\u6392\u5217\u3002\u8c03\u7528\u6b64\u51fd\u6570\u540e\uff0c\u5217\u8868\u4e2d\u7684\u5143\u7d20\u5c06\u4ee5\u968f\u673a\u987a\u5e8f\u6392\u5217\u3002<\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528<code>random.shuffle()<\/code>\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u5bfc\u5165<code>random<\/code>\u6a21\u5757\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>random.shuffle(items_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5c06\u5217\u8868\u8f6c\u6362\u56de\u5b57\u5178<\/h3>\n<\/p>\n<p><p>\u5728\u5bf9\u5217\u8868\u8fdb\u884c\u968f\u673a\u6392\u5217\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5176\u8f6c\u6362\u56de\u5b57\u5178\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>dict()<\/code>\u51fd\u6570\u5c06\u5217\u8868\u8f6c\u6362\u56de\u5b57\u5178\u3002<code>dict()<\/code>\u51fd\u6570\u53ef\u4ee5\u63a5\u53d7\u4e00\u4e2a\u5305\u542b\u952e\u503c\u5bf9\u7684\u5217\u8868\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><p>\u7ecf\u8fc7\u8fd9\u4e00\u6b65\uff0c\u6211\u4eec\u5c31\u6210\u529f\u5730\u5b9e\u73b0\u4e86\u5b57\u5178\u9879\u7684\u968f\u673a\u6392\u5217\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">randomized_dict = dict(items_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u968f\u673a\u6392\u5217\u5b57\u5178\u7684\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u968f\u673a\u6392\u5217\u5b57\u5178\u7684\u9879\u53ef\u80fd\u662f\u6709\u7528\u7684\u3002\u4f8b\u5982\uff0c\u5728\u6570\u636e\u5904\u7406\u6216<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u5e0c\u671b\u968f\u673a\u6392\u5217\u6570\u636e\u96c6\uff0c\u4ee5\u4fbf\u8fdb\u884c\u8bad\u7ec3\u548c\u6d4b\u8bd5\u3002\u6b64\u5916\uff0c\u5728\u67d0\u4e9b\u6e38\u620f\u6216\u6a21\u62df\u7a0b\u5e8f\u4e2d\uff0c\u968f\u673a\u6392\u5217\u5b57\u5178\u7684\u9879\u4e5f\u53ef\u4ee5\u589e\u52a0\u7a0b\u5e8f\u7684\u8da3\u5473\u6027\u548c\u4e0d\u53ef\u9884\u6d4b\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5b8c\u6574\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5728Python\u4e2d\u968f\u673a\u6392\u5217\u5b57\u5178\u7684\u9879\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<h2><strong>\u539f\u59cb\u5b57\u5178<\/strong><\/h2>\n<p>my_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3, &#39;d&#39;: 4, &#39;e&#39;: 5}<\/p>\n<h2><strong>\u5c06\u5b57\u5178\u7684\u9879\u8f6c\u6362\u4e3a\u5217\u8868<\/strong><\/h2>\n<p>items_list = list(my_dict.items())<\/p>\n<h2><strong>\u5bf9\u5217\u8868\u8fdb\u884c\u968f\u673a\u6392\u5217<\/strong><\/h2>\n<p>random.shuffle(items_list)<\/p>\n<h2><strong>\u5c06\u5217\u8868\u8f6c\u6362\u56de\u5b57\u5178<\/strong><\/h2>\n<p>randomized_dict = dict(items_list)<\/p>\n<h2><strong>\u6253\u5370\u7ed3\u679c<\/strong><\/h2>\n<p>print(&quot;Original dictionary:&quot;, my_dict)<\/p>\n<p>print(&quot;Randomized dictionary:&quot;, randomized_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u6267\u884c\u4e0a\u8ff0\u4ee3\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u5b57\u5178\u9879\u88ab\u968f\u673a\u6392\u5217\u540e\u7684\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><p>\u5728\u5bf9\u5b57\u5178\u7684\u9879\u8fdb\u884c\u968f\u673a\u6392\u5217\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b57\u5178\u7684\u65e0\u5e8f\u6027<\/strong>\uff1a\u5b57\u5178\u672c\u8eab\u662f\u4e00\u79cd\u65e0\u5e8f\u7684\u6570\u636e\u7ed3\u6784\u3002\u5373\u4f7f\u6211\u4eec\u5bf9\u5b57\u5178\u7684\u9879\u8fdb\u884c\u968f\u673a\u6392\u5217\uff0c\u7ed3\u679c\u4e5f\u4e0d\u4e00\u5b9a\u5728\u6240\u6709Python\u7248\u672c\u4e2d\u4fdd\u6301\u4e00\u81f4\u3002\u56e0\u6b64\uff0c\u5728\u9700\u8981\u4fdd\u6301\u987a\u5e8f\u7684\u60c5\u51b5\u4e0b\uff0c\u8003\u8651\u4f7f\u7528<code>collections.OrderedDict<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>Python\u7248\u672c<\/strong>\uff1a\u5728Python 3.7\u53ca\u66f4\u9ad8\u7248\u672c\u4e2d\uff0c\u5b57\u5178\u7684\u63d2\u5165\u987a\u5e8f\u662f\u4fdd\u8bc1\u7684\uff0c\u8fd9\u610f\u5473\u7740\u5b57\u5178\u5728\u63d2\u5165\u5143\u7d20\u65f6\u4f1a\u4fdd\u6301\u5143\u7d20\u7684\u987a\u5e8f\u3002\u7136\u800c\uff0c\u8fd9\u5e76\u4e0d\u5f71\u54cd\u6211\u4eec\u8fdb\u884c\u968f\u673a\u6392\u5217\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6570\u636e\u7c7b\u578b<\/strong>\uff1a\u786e\u4fdd\u5b57\u5178\u7684\u952e\u548c\u503c\u662f\u53ef\u54c8\u5e0c\u4e14\u652f\u6301\u6bd4\u8f83\u7684\u6570\u636e\u7c7b\u578b\uff0c\u5426\u5219\u53ef\u80fd\u4f1a\u5bfc\u81f4\u610f\u5916\u884c\u4e3a\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u5bf9\u5b57\u5178\u7684\u9879\u8fdb\u884c\u968f\u673a\u6392\u5217\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u6570\u636e\u5904\u7406\u548c\u7b97\u6cd5\u8bbe\u8ba1\u3002\u8fd9\u79cd\u6280\u672f\u53ef\u4ee5\u7528\u4e8e\u6570\u636e\u9884\u5904\u7406\u3001\u6a21\u62df\u5b9e\u9a8c\u4ee5\u53ca\u5176\u4ed6\u9700\u8981\u968f\u673a\u5316\u8f93\u5165\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u5b57\u5178\u7684\u968f\u673a\u6392\u5217\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5b57\u5178\u662f\u65e0\u5e8f\u7684\u96c6\u5408\uff0c\u56e0\u6b64\u8981\u968f\u673a\u6392\u5217\u5b57\u5178\u7684\u5185\u5bb9\uff0c\u60a8\u53ef\u4ee5\u5c06\u5b57\u5178\u7684\u9879\u8f6c\u6362\u4e3a\u5217\u8868\uff0c\u7136\u540e\u4f7f\u7528<code>random.shuffle()<\/code>\u51fd\u6570\u5bf9\u5217\u8868\u8fdb\u884c\u968f\u673a\u6392\u5217\u3002\u6700\u540e\uff0c\u60a8\u53ef\u4ee5\u5c06\u6392\u5217\u540e\u7684\u5217\u8868\u8f6c\u6362\u56de\u5b57\u5178\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u8ba9\u60a8\u83b7\u5f97\u4e00\u4e2a\u968f\u673a\u6392\u5217\u7684\u5b57\u5178\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u7684\u968f\u673a\u6a21\u5757\u65f6\uff0c\u6709\u54ea\u4e9b\u6ce8\u610f\u4e8b\u9879\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Python\u7684<code>random<\/code>\u6a21\u5757\u65f6\uff0c\u786e\u4fdd\u5728\u5bfc\u5165\u6a21\u5757\u65f6\u6ca1\u6709\u547d\u540d\u51b2\u7a81\uff0c\u6bd4\u5982\u4f7f\u7528<code>import random<\/code>\u800c\u4e0d\u662f\u5176\u4ed6\u540d\u79f0\u3002\u540c\u65f6\u8981\u6ce8\u610f<code>random.shuffle()<\/code>\u51fd\u6570\u4f1a\u76f4\u63a5\u4fee\u6539\u5217\u8868\uff0c\u56e0\u6b64\u5982\u679c\u60a8\u5e0c\u671b\u4fdd\u7559\u539f\u59cb\u5b57\u5178\u7684\u987a\u5e8f\uff0c\u5efa\u8bae\u5148\u590d\u5236\u4e00\u4efd\u5b57\u5178\u7684\u9879\u3002<\/p>\n<p><strong>\u5982\u4f55\u786e\u4fdd\u5b57\u5178\u7684\u968f\u673a\u6392\u5217\u80fd\u591f\u91cd\u590d\u4f7f\u7528\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u5e0c\u671b\u5728\u591a\u6b21\u8fd0\u884c\u7a0b\u5e8f\u65f6\u83b7\u5f97\u76f8\u540c\u7684\u968f\u673a\u6392\u5217\u7ed3\u679c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>random.seed()<\/code>\u51fd\u6570\u8bbe\u5b9a\u79cd\u5b50\u503c\u3002\u901a\u8fc7\u8bbe\u5b9a\u76f8\u540c\u7684\u79cd\u5b50\u503c\uff0c\u60a8\u53ef\u4ee5\u786e\u4fdd\u6bcf\u6b21\u8fd0\u884c\u4ee3\u7801\u65f6\u5f97\u5230\u7684\u968f\u673a\u6392\u5217\u90fd\u662f\u4e00\u81f4\u7684\uff0c\u8fd9\u5728\u8c03\u8bd5\u6216\u7ed3\u679c\u91cd\u73b0\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5c06\u5b57\u5178\u7684\u9879\u8f6c\u6362\u4e3a\u5217\u8868\u5e76\u4f7f\u7528random.shuffle()\u6765\u968f\u673a\u6392\u5217\u5b57\u5178\u3002\u5177\u4f53\u6b65\u9aa4\u5305 [&hellip;]","protected":false},"author":3,"featured_media":969953,"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\/969948"}],"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=969948"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/969948\/revisions"}],"predecessor-version":[{"id":969954,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/969948\/revisions\/969954"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/969953"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=969948"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=969948"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=969948"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}