{"id":3111,"date":"2016-05-08T19:52:00","date_gmt":"2016-05-08T11:52:00","guid":{"rendered":"https:\/\/shanlin.info\/?p=3111"},"modified":"2021-04-25T19:53:14","modified_gmt":"2021-04-25T11:53:14","slug":"python%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%8f%8d%e8%bd%ac%e7%9a%843%e7%a7%8d%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/shanlin.info\/?p=3111","title":{"rendered":"Python\u5b57\u7b26\u4e32\u53cd\u8f6c\u76843\u79cd\u65b9\u6cd5"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote\"><p>\u524d\u6bb5\u65f6\u95f4\u770b\u5230letcode\u4e0a\u7684\u5143\u97f3\u5b57\u6bcd\u5b57\u7b26\u4e32\u53cd\u8f6c\u7684\u9898\u76ee\uff0c\u4eca\u5929\u6765\u7814\u7a76\u4e00\u4e0b\u5b57\u7b26\u4e32\u53cd\u8f6c\u7684\u5185\u5bb9\u3002\u4e3b\u8981\u6709\u4e09\u79cd\u65b9\u6cd5\uff1a<\/p><\/blockquote>\n\n\n\n<h6 class=\"wp-block-heading\">1.\u5207\u7247\u6cd5\uff08\u6700\u7b80\u6d01\u7684\u4e00\u79cd\uff09<\/h6>\n\n\n\n<p><em><\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#\u5207\u7247\u6cd5\ndef reverse1():\n    s=input(\"\u8bf7\u8f93\u5165\u9700\u8981\u53cd\u8f6c\u7684\u5185\u5bb9\uff1a\")\n    return s&#91;::-1]\nreverse1()\n\n#\u8fd0\u884c\u7ed3\u679c\nIn &#91;23]: def reverse1():\n    ...: s=input(\"\u8bf7\u8f93\u5165\u9700\u8981\u53cd\u8f6c\u7684\u5185\u5bb9\uff1a\")\n    ...: return s&#91;::-1]\n    ...: \n    ...: reverse1()\n\n\u8bf7\u8f93\u5165\u9700\u8981\u53cd\u8f6c\u7684\u5185\u5bb9\uff1a\u4f60\u662f\u4e00\u4e2a\u5c0f\u5357\u74dc\nOut&#91;23]: '\u74dc\u5357\u5c0f\u4e2a\u4e00\u662f\u4f60'\n<\/code><\/pre>\n\n\n\n<p>\u53c2\u8003stackflow\u4e0a\u7684<a href=\"https:\/\/link.jianshu.com\/?t=http:\/\/stackoverflow.com\/questions\/931092\/reverse-a-string-in-python\" target=\"_blank\" rel=\"noreferrer noopener\">\u7b54\u6848<\/a>\u3002<br>\u539f\u7406\u662f\uff1aThis is&nbsp;<a href=\"https:\/\/link.jianshu.com\/?t=http:\/\/docs.python.org\/2\/whatsnew\/2.3.html#extended-slices\" target=\"_blank\" rel=\"noreferrer noopener\">extended slice<\/a>&nbsp;syntax. It works by doing [begin:end:step] &#8211; by leaving begin and end off and specifying a step of -1, it reverses a string.<\/p>\n\n\n\n<h6 class=\"wp-block-heading\">2.\u9012\u5f52<\/h6>\n\n\n\n<p><em><\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#\u9012\u5f52\u53cd\u8f6c\ndef reverse2(s):\n    if s==\"\":\n        return s\n    else:\n        return reverse2(s&#91;1:])+s&#91;0]\nreverse2(\"sidfmawsmdisd\u662f\u5f53\u9762\u95ee\")\n\n#\u8fd0\u884c\u7ed3\u679c\nIn &#91;24]: def reverse2(s):\n    ...: if s==\"\":\n    ...: return s\n    ...: else:\n    ...: return reverse2(s&#91;1:])+s&#91;0]\n    ...: \n    ...: reverse2(\"sidfmawsmdisd\u662f\u5f53\u9762\u95ee\")\nOut&#91;24]: '\u95ee\u9762\u5f53\u662fdsidmswamfdis'\n<\/code><\/pre>\n\n\n\n<h6 class=\"wp-block-heading\">3.\u501f\u7528\u5217\u8868\uff0c\u4f7f\u7528reverse()\u65b9\u6cd5<\/h6>\n\n\n\n<p>Python\u4e2d\u81ea\u5e26reverse()\u51fd\u6570\uff0c\u53ef\u4ee5\u5904\u7406\u5217\u8868\u7684\u53cd\u8f6c\uff0c\u6765\u770b\u793a\u4f8b\uff1a<em><\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>In &#91;25]: l=&#91;'a', 'b', 'c', 'd']\n    ...: l.reverse()\n    ...: print (l)\n&#91;'d', 'c', 'b', 'a']\n<\/code><\/pre>\n\n\n\n<p>reverse()\u51fd\u6570\u5c06\u5217\u8868\u7684\u5185\u5bb9\u8fdb\u884c\u4e86\u53cd\u8f6c\uff0c\u501f\u52a9\u8fd9\u4e2a\u7279\u6027\uff0c\u53ef\u4ee5\u5148\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u6210\u5217\u8868\uff0c\u5229\u7528reverse()\u51fd\u6570\u8fdb\u884c\u53cd\u8f6c\u540e\uff0c\u518d\u5904\u7406\u6210\u5b57\u7b26\u4e32\u3002<em><\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#\u501f\u7528\u5217\u8868\uff0c\u4f7f\u7528reverse()\u65b9\u6cd5\ndef reverse3(s):\n    l=list(s)\n    l.reverse()\n    print(\"\".join(l))\nreverse3(\"soifmi34pomOsprey\uff0c\uff0c\u662f\")\n\n#\u8fd0\u884c\u7ed3\u679c\nIn &#91;26]: def reverse3(s):\n    ...: l=list(s)\n    ...: l.reverse()\n    ...: print(\"\".join(l))\n    ...: \n    ...: reverse3(\"soifmi34pomOsprey\uff0c\uff0c\u662f\")\n    ...: \n\u662f\uff0c\uff0cyerpsOmop43imfios\n<\/code><\/pre>\n\n\n\n<p>\u4eca\u5929\u7684\u5b66\u4e60\u5c31\u5230\u8fd9\u91cc\u3002\u8d34\u4e0aletcode\u4e0a\u5143\u97f3\u5b57\u6bcd\u53cd\u8f6c\u7684<a href=\"https:\/\/link.jianshu.com\/?t=https:\/\/leetcode.com\/problems\/reverse-vowels-of-a-string\/\" target=\"_blank\" rel=\"noreferrer noopener\">\u5730\u5740<\/a>\uff0c\u5f85\u540e\u7eed\u7814\u7a76\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u6bb5\u65f6\u95f4\u770b\u5230letcode\u4e0a\u7684\u5143\u97f3\u5b57\u6bcd\u5b57\u7b26\u4e32\u53cd\u8f6c\u7684\u9898\u76ee\uff0c\u4eca\u5929\u6765\u7814\u7a76\u4e00\u4e0b\u5b57\u7b26\u4e32\u53cd\u8f6c\u7684\u5185\u5bb9\u3002\u4e3b\u8981\u6709\u4e09\u79cd\u65b9\u6cd5\uff1a 1.<\/p><\/div>\n<div class=\"blog-btn\"><a href=\"https:\/\/shanlin.info\/?p=3111\" class=\"home-blog-btn\">\u9605\u8bfb\u66f4\u591a<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"_links":{"self":[{"href":"https:\/\/shanlin.info\/index.php?rest_route=\/wp\/v2\/posts\/3111"}],"collection":[{"href":"https:\/\/shanlin.info\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/shanlin.info\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/shanlin.info\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/shanlin.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3111"}],"version-history":[{"count":1,"href":"https:\/\/shanlin.info\/index.php?rest_route=\/wp\/v2\/posts\/3111\/revisions"}],"predecessor-version":[{"id":3112,"href":"https:\/\/shanlin.info\/index.php?rest_route=\/wp\/v2\/posts\/3111\/revisions\/3112"}],"wp:attachment":[{"href":"https:\/\/shanlin.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3111"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/shanlin.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3111"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/shanlin.info\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3111"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}