{"id":1056666,"date":"2024-12-31T15:01:33","date_gmt":"2024-12-31T07:01:33","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1056666.html"},"modified":"2024-12-31T15:01:35","modified_gmt":"2024-12-31T07:01:35","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e5%90%91%e9%87%8f%e9%87%8d%e5%a4%8dn%e6%ac%a1","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1056666.html","title":{"rendered":"python\u5982\u4f55\u5c06\u5411\u91cf\u91cd\u590dn\u6b21"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/d09e32eb-11cf-4fb5-8462-c1e0df49f495.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u5c06\u5411\u91cf\u91cd\u590dn\u6b21\" \/><\/p>\n<p><p> <strong>Python\u5c06\u5411\u91cf\u91cd\u590dn\u6b21\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528numpy\u5e93\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790\u3001\u4f7f\u7528itertools\u5e93\u3002<\/strong>\u5176\u4e2d\uff0c\u6700\u63a8\u8350\u7684\u65b9\u6cd5\u662f\u4f7f\u7528numpy\u5e93\uff0c\u56e0\u4e3anumpy\u4e0d\u4ec5\u7b80\u6d01\u800c\u4e14\u6548\u7387\u9ad8\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528numpy\u5e93\u6765\u5b8c\u6210\u5411\u91cf\u91cd\u590d\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528Numpy\u5e93<\/h3>\n<\/p>\n<p><p>Numpy\u662fPython\u4e2d\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u7ec4\u64cd\u4f5c\u529f\u80fd\u3002\u8981\u5c06\u5411\u91cf\u91cd\u590dn\u6b21\uff0c\u53ef\u4ee5\u4f7f\u7528numpy\u5e93\u4e2d\u7684<code>tile<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5411\u91cf<\/strong><\/h2>\n<p>vector = np.array([1, 2, 3])<\/p>\n<h2><strong>\u5c06\u5411\u91cf\u91cd\u590dn\u6b21<\/strong><\/h2>\n<p>n = 3<\/p>\n<p>repeated_vector = np.tile(vector, n)<\/p>\n<p>print(repeated_vector)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ee3\u7801\u5c06\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>[1 2 3 1 2 3 1 2 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<p>numpy\u7684<code>tile<\/code>\u51fd\u6570\u53ef\u4ee5\u6cbf\u6307\u5b9a\u7684\u8f74\u91cd\u590d\u6570\u7ec4\u3002<code>tile<\/code>\u51fd\u6570\u7684\u8bed\u6cd5\u662f<code>numpy.tile(array, reps)<\/code>\uff0c\u5176\u4e2d<code>array<\/code>\u662f\u8f93\u5165\u6570\u7ec4\uff0c<code>reps<\/code>\u662f\u91cd\u590d\u7684\u6b21\u6570\u3002\u901a\u8fc7\u8c03\u7528<code>np.tile(vector, n)<\/code>\uff0c\u53ef\u4ee5\u5c06<code>vector<\/code>\u6cbf\u7b2c\u4e00\u4e2a\u8f74\u91cd\u590d<code>n<\/code>\u6b21\u3002\u8fd9\u4e2a\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u6d01\u660e\u4e86\uff0c\u4e14\u6027\u80fd\u4f18\u8d8a\uff0c\u9002\u7528\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4e0d\u60f3\u4f9d\u8d56\u5916\u90e8\u5e93\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5217\u8868\u89e3\u6790\u6765\u5b9e\u73b0\u76f8\u540c\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u5411\u91cf<\/p>\n<p>vector = [1, 2, 3]<\/p>\n<h2><strong>\u5c06\u5411\u91cf\u91cd\u590dn\u6b21<\/strong><\/h2>\n<p>n = 3<\/p>\n<p>repeated_vector = vector * n<\/p>\n<p>print(repeated_vector)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ee3\u7801\u5c06\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>[1, 2, 3, 1, 2, 3, 1, 2, 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<p>\u5728Python\u4e2d\uff0c\u5217\u8868\u7684\u4e58\u6cd5\u64cd\u4f5c\u7b26<code>*<\/code>\u53ef\u4ee5\u7528\u6765\u91cd\u590d\u5217\u8868\u5143\u7d20\u3002\u901a\u8fc7<code>vector * n<\/code>\uff0c\u53ef\u4ee5\u5c06\u5217\u8868<code>vector<\/code>\u4e2d\u7684\u5143\u7d20\u91cd\u590d<code>n<\/code>\u6b21\uff0c\u5e76\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u5217\u8868\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4f9d\u8d56\u4e8e\u5916\u90e8\u5e93\uff0c\u975e\u5e38\u9002\u5408\u7b80\u5355\u7684\u3001\u8f83\u5c0f\u7684\u6570\u636e\u96c6\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528itertools\u5e93<\/h3>\n<\/p>\n<p><p>itertools\u5e93\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u7528\u4e8e\u9ad8\u6548\u5faa\u73af\u7684\u8fed\u4ee3\u5668\u3002\u53ef\u4ee5\u4f7f\u7528<code>itertools.ch<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n<\/code>\u548c<code>itertools.repeat<\/code>\u6765\u5b9e\u73b0\u5411\u91cf\u7684\u91cd\u590d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import itertools<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5411\u91cf<\/strong><\/h2>\n<p>vector = [1, 2, 3]<\/p>\n<h2><strong>\u5c06\u5411\u91cf\u91cd\u590dn\u6b21<\/strong><\/h2>\n<p>n = 3<\/p>\n<p>repeated_vector = list(itertools.chain.from_iterable(itertools.repeat(vector, n)))<\/p>\n<p>print(repeated_vector)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ee3\u7801\u5c06\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>[1, 2, 3, 1, 2, 3, 1, 2, 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<p><code>itertools.repeat<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06\u8f93\u5165\u91cd\u590d\u6307\u5b9a\u6b21\u6570\uff0c\u800c<code>itertools.chain.from_iterable<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06\u591a\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u5c55\u5e73\u4e3a\u4e00\u4e2a\u8fed\u4ee3\u5668\u3002\u901a\u8fc7\u5c06<code>itertools.repeat(vector, n)<\/code>\u7684\u7ed3\u679c\u4f20\u9012\u7ed9<code>itertools.chain.from_iterable<\/code>\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5411\u91cf\u7684\u91cd\u590d\u5e76\u5c55\u5e73\u4e3a\u4e00\u4e2a\u5217\u8868\u3002\u8fd9\u4e2a\u65b9\u6cd5\u4e5f\u9002\u7528\u4e8e\u751f\u6210\u5927\u89c4\u6a21\u7684\u91cd\u590d\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5c06\u5411\u91cf\u91cd\u590dn\u6b21\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<strong>\u63a8\u8350\u4f7f\u7528numpy\u5e93\u4e2d\u7684<code>tile<\/code>\u51fd\u6570\uff0c\u56e0\u4e3a\u5b83\u4e0d\u4ec5\u7b80\u6d01\u800c\u4e14\u9ad8\u6548\uff0c\u9002\u7528\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u5904\u7406\u3002<\/strong>\u5982\u679c\u4e0d\u60f3\u4f9d\u8d56\u5916\u90e8\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u89e3\u6790\uff0c\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u6613\u61c2\uff0c\u975e\u5e38\u9002\u5408\u8f83\u5c0f\u7684\u6570\u636e\u96c6\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528itertools\u5e93\u4e2d\u7684\u51fd\u6570\u6765\u5b9e\u73b0\u91cd\u590d\u64cd\u4f5c\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u573a\u666f\uff0c\u7406\u89e3\u5e76\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\u7075\u6d3b\u5e94\u7528\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u91cd\u590d\u7684\u5411\u91cf\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u6765\u521b\u5efa\u91cd\u590d\u7684\u5411\u91cf\u3002\u901a\u8fc7<code>numpy.repeat()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u8fd9\u4e2a\u529f\u80fd\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u5411\u91cf<code>[1, 2, 3]<\/code>\uff0c\u5e76\u5e0c\u671b\u5c06\u5176\u91cd\u590d3\u6b21\uff0c\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">import numpy as np\nvector = np.array([1, 2, 3])\nrepeated_vector = np.repeat(vector, 3)\nprint(repeated_vector)\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u8f93\u51fa<code>[1, 1, 1, 2, 2, 2, 3, 3, 3]<\/code>\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u4f7f\u7528\u5217\u8868\u7406\u89e3\u5982\u4f55\u91cd\u590d\u5411\u91cf\uff1f<\/strong><br \/>\u9664\u4e86NumPy\uff0cPython\u7684\u5185\u7f6e\u5217\u8868\u4e5f\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u5411\u91cf\u91cd\u590d\u3002\u4f7f\u7528\u5217\u8868\u7406\u89e3\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u5c06\u539f\u59cb\u5217\u8868\u4e0e\u6570\u5b57\u91cd\u590d\u76f8\u4e58\u6765\u521b\u5efa\u65b0\u7684\u5217\u8868\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">vector = [1, 2, 3]\nn = 3\nrepeated_vector = [item for item in vector for _ in range(n)]\nprint(repeated_vector)\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u60a8\u5c06\u5f97\u5230\u540c\u6837\u7684\u7ed3\u679c\uff1a<code>[1, 1, 1, 2, 2, 2, 3, 3, 3]<\/code>\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u5411\u91cf\u91cd\u590dn\u6b21\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u5e0c\u671b\u91cd\u590d\u5b57\u7b26\u4e32\u5411\u91cf\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684\u4e58\u6cd5\u8fd0\u7b97\u3002\u6bd4\u5982\uff0c\u5047\u8bbe\u60a8\u6709\u4e00\u4e2a\u5305\u542b\u5b57\u7b26\u4e32\u7684\u5217\u8868<code>[&quot;a&quot;, &quot;b&quot;, &quot;c&quot;]<\/code>\uff0c\u60f3\u8981\u5c06\u5176\u91cd\u590d2\u6b21\uff0c\u53ef\u4ee5\u8fd9\u6837\u5b9e\u73b0\uff1a  <\/p>\n<pre><code class=\"language-python\">string_vector = [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;]\nn = 2\nrepeated_string_vector = [item for item in string_vector for _ in range(n)]\nprint(repeated_string_vector)\n<\/code><\/pre>\n<p>\u8f93\u51fa\u7ed3\u679c\u5c06\u662f<code>[&#39;a&#39;, &#39;a&#39;, &#39;b&#39;, &#39;b&#39;, &#39;c&#39;, &#39;c&#39;]<\/code>\uff0c\u5b9e\u73b0\u4e86\u5b57\u7b26\u4e32\u5411\u91cf\u7684\u91cd\u590d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5c06\u5411\u91cf\u91cd\u590dn\u6b21\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528numpy\u5e93\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790\u3001\u4f7f\u7528itertools\u5e93\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1056675,"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\/1056666"}],"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=1056666"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1056666\/revisions"}],"predecessor-version":[{"id":1056676,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1056666\/revisions\/1056676"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1056675"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1056666"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1056666"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1056666"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}