{"id":1056197,"date":"2024-12-31T14:57:36","date_gmt":"2024-12-31T06:57:36","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1056197.html"},"modified":"2024-12-31T14:57:39","modified_gmt":"2024-12-31T06:57:39","slug":"python%e5%a6%82%e4%bd%95%e5%90%91%e5%90%8e%e7%a7%bb%e5%8a%a8%e5%ad%97%e7%ac%a6%e4%b8%b2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1056197.html","title":{"rendered":"python\u5982\u4f55\u5411\u540e\u79fb\u52a8\u5b57\u7b26\u4e32"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/4c3a3dcf-b087-4cb9-acbf-ba856de5c99b.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u5411\u540e\u79fb\u52a8\u5b57\u7b26\u4e32\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u5411\u540e\u79fb\u52a8\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u4f8b\u5982\u901a\u8fc7\u5207\u7247\u3001\u5faa\u73af\u6216\u5185\u7f6e\u51fd\u6570\u3002<\/strong>\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u51e0\u79cd\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u793a\u4f8b\u4ee3\u7801\u6765\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u5b83\u4eec\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u5207\u7247<\/h2>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u5207\u7247\u662fPython\u4e2d\u64cd\u4f5c\u5b57\u7b26\u4e32\u7684\u4e00\u4e2a\u5f3a\u5927\u529f\u80fd\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5207\u7247\u8f7b\u677e\u5730\u5c06\u5b57\u7b26\u4e32\u5411\u540e\u79fb\u52a8\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u6211\u4eec\u60f3\u8981\u5c06\u5176\u5411\u540e\u79fb\u52a8n\u4e2a\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def move_string_slice(s, n):<\/p>\n<p>    n = n % len(s)  # \u9632\u6b62n\u8d85\u8fc7\u5b57\u7b26\u4e32\u957f\u5ea6<\/p>\n<p>    return s[-n:] + s[:-n]<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>s = &quot;abcdef&quot;<\/p>\n<p>n = 2<\/p>\n<p>print(move_string_slice(s, n))  # \u8f93\u51fa: &quot;efabcd&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u7b26<code>[-n:]<\/code>\u6765\u83b7\u53d6\u5b57\u7b26\u4e32\u7684\u6700\u540en\u4e2a\u5b57\u7b26\uff0c<code>[:-n]<\/code>\u6765\u83b7\u53d6\u5b57\u7b26\u4e32\u7684\u524d\u90e8\u5206\uff0c\u7136\u540e\u5c06\u5b83\u4eec\u8fde\u63a5\u5728\u4e00\u8d77\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528\u5faa\u73af<\/h2>\n<\/p>\n<p><p>\u9664\u4e86\u5207\u7247\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u6765\u5b9e\u73b0\u5b57\u7b26\u4e32\u7684\u5411\u540e\u79fb\u52a8\u3002\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u6ca1\u6709\u5207\u7247\u7b80\u6d01\uff0c\u4f46\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u6df1\u5165\u5730\u7406\u89e3\u5b57\u7b26\u4e32\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def move_string_loop(s, n):<\/p>\n<p>    n = n % len(s)  # \u9632\u6b62n\u8d85\u8fc7\u5b57\u7b26\u4e32\u957f\u5ea6<\/p>\n<p>    result = &quot;&quot;<\/p>\n<p>    for i in range(len(s)):<\/p>\n<p>        result += s[(i - n) % len(s)]<\/p>\n<p>    return result<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>s = &quot;abcdef&quot;<\/p>\n<p>n = 2<\/p>\n<p>print(move_string_loop(s, n))  # \u8f93\u51fa: &quot;efabcd&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u5faa\u73af\u6765\u904d\u5386\u5b57\u7b26\u4e32\u7684\u6bcf\u4e2a\u5b57\u7b26\uff0c\u5e76\u6839\u636e\u65b0\u7684\u4f4d\u7f6e\u5c06\u5b83\u4eec\u91cd\u65b0\u7ec4\u5408\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528collections.deque<\/h2>\n<\/p>\n<p><p><code>collections.deque<\/code>\u662f\u4e00\u4e2a\u53cc\u7aef\u961f\u5217\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u4ece\u4e24\u7aef\u6dfb\u52a0\u6216\u5220\u9664\u5143\u7d20\u3002\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u5b83\u7684\u65cb\u8f6c\u529f\u80fd\u6765\u5b9e\u73b0\u5b57\u7b26\u4e32\u7684\u5411\u540e\u79fb\u52a8\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import deque<\/p>\n<p>def move_string_deque(s, n):<\/p>\n<p>    d = deque(s)<\/p>\n<p>    d.rotate(n)<\/p>\n<p>    return &#39;&#39;.join(d)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>s = &quot;abcdef&quot;<\/p>\n<p>n = 2<\/p>\n<p>print(move_string_deque(s, n))  # \u8f93\u51fa: &quot;efabcd&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u53cc\u7aef\u961f\u5217\uff0c\u7136\u540e\u4f7f\u7528<code>rotate<\/code>\u65b9\u6cd5\u5c06\u5176\u65cb\u8f6cn\u4e2a\u4f4d\u7f6e\uff0c\u6700\u540e\u5c06\u5176\u8f6c\u6362\u56de\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u62fc\u63a5<\/h2>\n<\/p>\n<p><p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u901a\u8fc7\u5b57\u7b26\u4e32\u62fc\u63a5\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002\u867d\u7136\u8fd9\u79cd\u65b9\u6cd5\u770b\u8d77\u6765\u6bd4\u8f83\u76f4\u89c2\uff0c\u4f46\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u5b83\u4f1a\u6bd4\u5207\u7247\u548cdeque\u6162\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def move_string_concat(s, n):<\/p>\n<p>    n = n % len(s)  # \u9632\u6b62n\u8d85\u8fc7\u5b57\u7b26\u4e32\u957f\u5ea6<\/p>\n<p>    return s[len(s)-n:] + s[:len(s)-n]<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>s = &quot;abcdef&quot;<\/p>\n<p>n = 2<\/p>\n<p>print(move_string_concat(s, n))  # \u8f93\u51fa: &quot;efabcd&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u5b57\u7b26\u4e32\u62fc\u63a5\u6765\u5b9e\u73b0\u5411\u540e\u79fb\u52a8\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u8003\u8651\u8fb9\u754c\u60c5\u51b5<\/h2>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u8fd8\u9700\u8981\u8003\u8651\u4e00\u4e9b\u8fb9\u754c\u60c5\u51b5\u3002\u4f8b\u5982\uff0c\u5b57\u7b26\u4e32\u4e3a\u7a7a\uff0c\u6216\u8005\u79fb\u52a8\u7684\u6b21\u6570\u4e3a\u96f6\uff0c\u6216\u8005\u79fb\u52a8\u7684\u6b21\u6570\u5927\u4e8e\u5b57\u7b26\u4e32\u957f\u5ea6\u3002\u5bf9\u4e8e\u8fd9\u4e9b\u60c5\u51b5\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u51fd\u6570\u4e2d\u6dfb\u52a0\u4e00\u4e9b\u68c0\u67e5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def move_string(s, n):<\/p>\n<p>    if not s or n == 0:<\/p>\n<p>        return s<\/p>\n<p>    n = n % len(s)<\/p>\n<p>    return s[-n:] + s[:-n]<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>print(move_string(&quot;&quot;, 2))  # \u8f93\u51fa: &quot;&quot;<\/p>\n<p>print(move_string(&quot;abcdef&quot;, 0))  # \u8f93\u51fa: &quot;abcdef&quot;<\/p>\n<p>print(move_string(&quot;abcdef&quot;, 8))  # \u8f93\u51fa: &quot;efabcd&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u6dfb\u52a0\u4e86\u4e00\u4e9b\u68c0\u67e5\uff0c\u4ee5\u786e\u4fdd\u51fd\u6570\u80fd\u591f\u6b63\u786e\u5904\u7406\u5404\u79cd\u8fb9\u754c\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h2>\u516d\u3001\u6027\u80fd\u6bd4\u8f83<\/h2>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u5177\u4f53\u65b9\u6cd5\u65f6\uff0c\u6211\u4eec\u8fd8\u9700\u8981\u8003\u8651\u6027\u80fd\u3002\u5bf9\u4e8e\u8f83\u957f\u7684\u5b57\u7b26\u4e32\uff0c\u67d0\u4e9b\u65b9\u6cd5\u53ef\u80fd\u4f1a\u6bd4\u5176\u4ed6\u65b9\u6cd5\u66f4\u9ad8\u6548\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>timeit<\/code>\u6a21\u5757\u6765\u6bd4\u8f83\u4e0d\u540c\u65b9\u6cd5\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import timeit<\/p>\n<p>s = &quot;abcdef&quot; * 1000<\/p>\n<p>n = 200<\/p>\n<p>print(timeit.timeit(lambda: move_string_slice(s, n), number=1000))<\/p>\n<p>print(timeit.timeit(lambda: move_string_loop(s, n), number=1000))<\/p>\n<p>print(timeit.timeit(lambda: move_string_deque(s, n), number=1000))<\/p>\n<p>print(timeit.timeit(lambda: move_string_concat(s, n), number=1000))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>timeit<\/code>\u6a21\u5757\u6765\u6bd4\u8f83\u56db\u79cd\u65b9\u6cd5\u7684\u6267\u884c\u65f6\u95f4\uff0c\u4ece\u800c\u9009\u62e9\u6700\u9002\u5408\u6211\u4eec\u9700\u6c42\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h2>\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u51e0\u79cd\u5728Python\u4e2d\u5411\u540e\u79fb\u52a8\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u5b57\u7b26\u4e32\u5207\u7247\u3001\u5faa\u73af\u3001<code>collections.deque<\/code>\u548c\u5b57\u7b26\u4e32\u62fc\u63a5\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u5728\u9009\u62e9\u65b9\u6cd5\u65f6\uff0c\u6211\u4eec\u8fd8\u9700\u8981\u8003\u8651\u8fb9\u754c\u60c5\u51b5\u548c\u6027\u80fd\uff0c\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u7684\u6b63\u786e\u6027\u548c\u9ad8\u6548\u6027\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5411\u540e\u79fb\u52a8\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u5411\u540e\u79fb\u52a8\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\uff0c\u53ef\u4ee5\u4f7f\u7528\u5207\u7247\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u5207\u7247\u64cd\u4f5c\uff0c\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u7684\u6700\u540e\u51e0\u4e2a\u5b57\u7b26\u79fb\u52a8\u5230\u5f00\u5934\u3002\u4f8b\u5982\uff0c\u5982\u679c\u8981\u5c06\u5b57\u7b26\u4e32\u201chello\u201d\u5411\u540e\u79fb\u52a82\u4e2a\u5b57\u7b26\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<code>s[-2:] + s[:-2]<\/code>\uff0c\u8fd9\u5c06\u8fd4\u56de\u201clohel\u201d\u3002<\/p>\n<p><strong>\u5411\u540e\u79fb\u52a8\u5b57\u7b26\u4e32\u65f6\uff0c\u5982\u4f55\u5904\u7406\u7a7a\u5b57\u7b26\u4e32\u6216\u7279\u6b8a\u60c5\u51b5\uff1f<\/strong><br \/>\u5728\u5904\u7406\u7a7a\u5b57\u7b26\u4e32\u6216\u5b57\u7b26\u4e32\u957f\u5ea6\u5c0f\u4e8e\u8981\u79fb\u52a8\u7684\u5b57\u7b26\u6570\u65f6\uff0c\u53ef\u4ee5\u5148\u68c0\u67e5\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u3002\u5982\u679c\u5b57\u7b26\u4e32\u4e3a\u7a7a\u6216\u8005\u79fb\u52a8\u7684\u5b57\u7b26\u6570\u5927\u4e8e\u5b57\u7b26\u4e32\u957f\u5ea6\uff0c\u76f4\u63a5\u8fd4\u56de\u539f\u5b57\u7b26\u4e32\u5373\u53ef\u3002\u8fd9\u907f\u514d\u4e86\u4e0d\u5fc5\u8981\u7684\u9519\u8bef\u6216\u5f02\u5e38\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u5faa\u73af\u79fb\u52a8\u5b57\u7b26\u4e32\u7684\u529f\u80fd\uff1f<\/strong><br \/>\u5faa\u73af\u79fb\u52a8\u5b57\u7b26\u4e32\u7684\u529f\u80fd\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u6765\u5b9e\u73b0\u3002\u8be5\u51fd\u6570\u63a5\u53d7\u5b57\u7b26\u4e32\u548c\u79fb\u52a8\u7684\u5b57\u7b26\u6570\u4f5c\u4e3a\u53c2\u6570\uff0c\u7136\u540e\u4f7f\u7528\u5207\u7247\u5c06\u5b57\u7b26\u4e32\u8fdb\u884c\u5207\u5206\u5e76\u91cd\u65b0\u7ec4\u5408\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">def rotate_string(s, n):\n    if not s or n &lt;= 0:\n        return s\n    n = n % len(s)  # \u5904\u7406n\u5927\u4e8e\u5b57\u7b26\u4e32\u957f\u5ea6\u7684\u60c5\u51b5\n    return s[-n:] + s[:-n]\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9e\u73b0\u4efb\u610f\u6570\u91cf\u7684\u5b57\u7b26\u5411\u540e\u79fb\u52a8\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5411\u540e\u79fb\u52a8\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u4f8b\u5982\u901a\u8fc7\u5207\u7247\u3001\u5faa\u73af\u6216\u5185\u7f6e\u51fd\u6570\u3002\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u51e0\u79cd [&hellip;]","protected":false},"author":3,"featured_media":1056205,"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\/1056197"}],"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=1056197"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1056197\/revisions"}],"predecessor-version":[{"id":1056208,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1056197\/revisions\/1056208"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1056205"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1056197"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1056197"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1056197"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}