{"id":135,"date":"2016-01-14T04:17:56","date_gmt":"2016-01-14T04:17:56","guid":{"rendered":"http:\/\/yellingstone.info\/?p=135"},"modified":"2016-04-13T00:19:03","modified_gmt":"2016-04-12T22:19:03","slug":"%e6%a0%b8%e5%bf%83%e8%af%ad%e8%a8%80%e5%ad%a6%e6%97%a0%e7%bc%96%e7%a8%8b%e5%9f%ba%e7%a1%80%e5%85%a5%e9%97%a8python%ef%bc%88%e7%ac%ac%e5%9b%9b%e7%ab%a0%ef%bc%89","status":"publish","type":"post","link":"http:\/\/yellingstone.info\/?p=135","title":{"rendered":"\u6838\u5fc3\u8bed\u8a00\u5b66\u65e0\u7f16\u7a0b\u57fa\u7840\u5165\u95e8Python\uff08\u7b2c\u56db\u7ae0\uff09"},"content":{"rendered":"<p>\u5efa\u8bae\u5148\u770b\u5b8c\u524d\u9762\u7684\u7ae0\u8282\uff0c\u524d\u9762\u7ae0\u8282\u5185\u5bb9\uff1a<!--more--><\/p>\n<ol>\n<li><a href=\"http:\/\/yellingstone.info\/?p=64\">\u548cPython\u597d\u597d\u8c08\u4e00\u8c08\uff08print\uff09<\/a><\/li>\n<li><a href=\"http:\/\/yellingstone.info\/?p=64\">Python\u4f60\u5e2e\u6211\u7701\u7701\u5fc3\u554a\uff08=\uff09<\/a><\/li>\n<li><a href=\"http:\/\/yellingstone.info\/?p=128\">\u628a\u6587\u5b57\u626d\u4e00\u626d\uff08\u5173\u4e8estring\u7684\u547d\u4ee4\uff09<\/a><\/li>\n<li><a href=\"http:\/\/yellingstone.info\/?p=128\">\u8ba9\u522b\u4eba\u4e5f\u6765\u53c2\u4e0e\u4e00\u4e0b\uff08raw_input\uff09<\/a><\/li>\n<li><a href=\"http:\/\/yellingstone.info\/?p=131\">\u6211\u771f\u5f97\u4e0d\u60f3\u770b\u5927\u6bb5\u7684\u5b57\u4e32\u554a\uff01\uff08list\u7684\u6982\u5ff5\uff09<\/a><\/li>\n<\/ol>\n<hr>\n<p>6 \u539f\u6765\u90fd\u662f\u70e4\u4e32\u513f\u800c\u5df2\uff08slicing\/indexing\uff09<\/p>\n<p>\u5df2\u7ecf\u5f15\u5165\u4e86\u5217\u8868\uff08list\uff09\u7684\u6982\u5ff5\uff0c\u4e5f\u63d0\u5230\u5217\u8868\u548c\u5b57\u4e32\uff08string\uff09\u5171\u4eab\u90e8\u5206\u547d\u4ee4\u3002\u73b0\u5728\u8bf4\u4e3a\u4ec0\u4e48\u3002\u56e0\u4e3a\u6240\u8c13\u5217\u8868\uff0c\u5c31\u662f\u4e00\u4e2a\u4e00\u4e2a\u5143\u7d20\u6309\u7167\u56fa\u5b9a\u987a\u5e8f\u6392\u5217\u800c\u5df2\uff1b\u800c\u6240\u8c13\u5b57\u4e32\uff0c\u662f\u4e00\u4e9b\u7279\u6b8a\u4e00\u70b9\u7684\u5143\u7d20\u2014\u2014\u5b57\u7b26\u2014\u2014\u6309\u7167\u56fa\u5b9a\u987a\u5e8f\u6392\u5217\u3002\u60c5\u5f62\u6709\u4e00\u70b9\u70b9\u50cf\u811a\u672c1\u3002<\/p>\n<p>[code language=&#8221;python&#8221;]<br \/>\n#\u811a\u672c1<br \/>\na=&#8217;python&#8217;<br \/>\nmylist=[&#8216;p&#8217;, &#8216;y&#8217;, &#8216;t&#8217;, &#8216;h&#8217;, &#8216;o&#8217;, &#8216;n&#8217;]<br \/>\n[\/code]<\/p>\n<p>\uff08\u8fd9\u4e00\u6bb5\u5176\u5b9e\u4e0d\u600e\u4e48\u91cd\u8981\uff09\u5355\u5c31\u5143\u7d20\u987a\u5e8f\u6392\u5217\u8fd9\u70b9\u6765\u770b\uff0c\u5b57\u4e32\uff08string\uff09\u6bd5\u7adf\u8fd8\u662f\u6bd4\u5217\u8868\uff08list\uff09\u7279\u6b8a\u4e00\u4e9b\uff0c\u6bd4\u5982\u811a\u672c1\u7684a\u8fd9\u4e2a\u5b57\u4e32\u91cc\uff0c\u53ef\u4ee5\u5305\u542b&#8217;py&#8217;, &#8216;thon&#8217;, &#8216;ytho&#8217;\u8fd9\u4e9b\u3010\u5b57\u7b26\u7684\u7ec4\u5408\u3011\uff0c\u800cmylist\u8fd9\u4e2a\u5217\u8868\u91cc\uff0c\u8fd9\u4e9b\u5143\u7d20\u5c31\u6ca1\u90a3\u4e48\u8f7b\u6613\u5730\u300c\u968f\u673a\u300d\u7ec4\u5408\u5728\u4e00\u8d77\u3002\u8df3\u5230\u7ed3\u8bba\u5427\uff0c\u5728\u5b57\u4e32\u91cc\u67e5\u627e\u67d0\u4e2a\u3010\u6216\u67d0\u4e9b\u3011\u5b57\u7b26\u5728\u54ea\u4e2a\u4f4d\u7f6e\uff0c\u7528find\uff1b\u5728\u5217\u8868\u91cc\u67e5\u627e\u67d0\u4e2a\u5143\u7d20\u5728\u54ea\u4e2a\u4f4d\u7f6e\uff0c\u7528index\uff0c\u8be6\u89c1\u811a\u672c2\uff08\u6ce8\uff1a\u5176\u5b9e\u5427\uff0c\u5982\u679c\u771f\u628a\u5b57\u4e32\u7406\u89e3\u4e3a\u4e00\u79cd\u7279\u6b8a\u7684\u5217\u8868\u7684\u8bdd\uff0cindex\u547d\u4ee4\u4e5f\u662f\u53ef\u4ee5\u7528\u4e8e\u5b57\u4e32\u7684\uff09\u3002<\/p>\n<p>[code language=&#8221;python&#8221;]<br \/>\n#\u811a\u672c2<br \/>\na=&#8217;python&#8217;<br \/>\nmylist=[&#8216;p&#8217;, &#8216;y&#8217;, &#8216;t&#8217;, &#8216;h&#8217;, &#8216;o&#8217;, &#8216;n&#8217;]<br \/>\naf=a.find(&#8216;y&#8217;)<br \/>\nmf=mylist.index(&#8216;h&#8217;)<br \/>\nprint af<br \/>\nprint mf<br \/>\n# &gt;&gt; 1<br \/>\n# 3<br \/>\n[\/code]<\/p>\n<p>\u7406\u89e3\u4e86\u5176\u5b9e\u65e0\u8bba\u5b57\u4e32\uff08string\uff09\u8fd8\u662f\u5217\u8868\uff08list\uff09\u672c\u8d28\u4e0a\u90fd\u662f\u70e4\u4e32\u513f\u800c\u5df2\uff0c\u5c31\u53ef\u4ee5\u5165\u624b\u4e00\u4e2a\u88ab\u79f0\u4e3aslicing\/indexing\u7684\u529f\u80fd\u4e86\u3010\u9009\u53d6\u4e00\u6839\u4e32\u513f\u4e0a\u9700\u8981\u7684\u90e8\u5206\u3011\u3002\u5b9e\u73b0\u8fd9\u4e2a\u529f\u80fd\u7684\u65b9\u6cd5\u662f\u7528 \u4e32[\u53c2\u6570]\u3002\u4e0b\u9762\u7684\u4f8b\u5b50\u91cc\u9ed8\u8ba4\u5b57\u4e32 a=&#8217;python&#8217; \u5217\u8868 mylist=[&#8216;p&#8217;, &#8216;y&#8217;, &#8216;t&#8217;, &#8216;h&#8217;, &#8216;o&#8217;, &#8216;n&#8217;]<\/p>\n<p><code>a[0]<\/code>\t\t\t\t\u5b57\u4e32a\u91cc\u9009\u53d6\u7b2c1\u4e2a\u5143\u7d20<br \/>\n<code>mylist[1]<\/code>\t\t\t\u5217\u8868mylist\u91cc\u9009\u53d6\u7b2c2\u4e2a\u5143\u7d20<br \/>\n<code>a[-1]<\/code>\t\t\t\t\u5b57\u4e32a\u91cc\u9009\u53d6\u6700\u540e1\u4e2a\u5143\u7d20<br \/>\n<code>mylist[-2]<\/code>\t\t\t\u5217\u8868mylist\u91cc\u9009\u53d6\u5012\u6570\u7b2c2\u4e2a\u5143\u7d20<\/p>\n<p>\u4e5f\u53ef\u4ee5\u4e00\u6b21\u6309\u987a\u5e8f\u9009\u591a\u4e2a\u5143\u7d20\uff0c\u4e2d\u62ec\u53f7\u91cc\u8bb0\u5f97\u5199[\u8d77\u59cb\u4f4d\u7f6e:\u7ed3\u675f\u4f4d\u7f6e]\uff0c\u3010\u4e0d\u8fc7\u8981\u6ce8\u610f\u7684\u662f\u7ed3\u675f\u4f4d\u7f6e\u5e76\u4e0d\u5305\u542b\u5728\u91cc\u9762\uff01\u3011<\/p>\n<p><code>a[0:2]<\/code>\t\t\t\u5b57\u4e32a\u91cc\u9009\u53d6\u524d2\u4e2a\u5143\u7d20<br \/>\n<code>mylist[2:5]<\/code>\t\t\u5217\u8868mylist\u91cc\u9009\u53d6\u7b2c3\u3001\u7b2c4\u3001\u7b2c5\u4e2a\u5143\u7d20<br \/>\n<code>a[-3:-1]<\/code>\t\t\t\u5b57\u4e32\u91cc\u9009\u53d6\u5012\u6570\u7b2c3\u548c\u5012\u6570\u7b2c2\u4e2a\u5143\u7d20<\/p>\n<p>\u5982\u679c\u60f3\u5305\u542b\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff0c\u90a3\u5192\u53f7\u540e\u9762\u53ef\u4ee5\u4e0d\u586b\uff1b\u53e6\u5916\u5982\u679c\u662f\u4ece\u7b2c\u4e00\u4e2a\u5143\u7d20\u5f00\u59cb\u6570\uff0c\u5192\u53f7\u524d\u9762\u4e5f\u53ef\u4ee5\u4e0d\u5199\u3002<\/p>\n<p><code>a[:3]<\/code>\t\t\t\t\u5b57\u4e32a\u91cc\u9009\u53d6\u524d3\u4e2a\u5143\u7d20<br \/>\n<code>mylist[-2:]<\/code>\t\t\t\u5217\u8868mylist\u91cc\u9009\u53d6\u6700\u540e\u4e24\u4e2a\u5143\u7d20<\/p>\n<p>\u6700\u540e\uff0c\u5982\u679c\u6211\u4eec\u5bf9\u4e8e\u5b57\u4e32\uff08string\uff09\u548c\u5217\u8868\uff08list\uff09\u90fd\u57f9\u517b\u597d\u4e86\u7aef\u6b63\u7684\u300c\u70e4\u4e32\u89c2\u300d\u4e4b\u540e\uff0c\u6709\u4e00\u4e2a\u5f88\u6709\u7528\u7684\u547d\u4ee4\u53eblen\uff0c\u4f5c\u7528\u662f\u67e5\u4e00\u4e0b\u8fd9\u4e2a\u4e32\u513f\u4e0a\u4e00\u5171\u6709\u591a\u5c11\u4e2a\u5143\u7d20\u3002<\/p>\n<p>[code language=&#8221;python&#8221;]<br \/>\n#\u811a\u672c3<br \/>\na=&#8217;python&#8217;<br \/>\nmylist=[&#8216;Python&#8217;, &#8216;so&#8217;, &#8216;easy&#8217;]<br \/>\nal=len(a)<br \/>\nml=len(mylist)<br \/>\nprint al<br \/>\nprint ml<br \/>\n# &gt;&gt; 6<br \/>\n# 3<br \/>\n[\/code]<\/p>\n<p>\u8865\u4e00\u53e5\uff1alen\u53ef\u4ee5\u5e2e\u52a9\u66f4\u597d\u7684slicing\uff0c\u56e0\u4e3a\u4f60\u9009\u53d6\u9700\u8981\u7684\u90e8\u5206\u4e4b\u524d\u603b\u5f52\u5f97\u8981\u77e5\u9053\u8fd9\u6839\u4e32\u513f\u4e0a\u6709\u591a\u5c11\u4e1c\u897f\u3002<\/p>\n<hr>\n<p>7 \u4e32\u513f\u4e0a\u7684\u8089\u8981\u4e00\u5757\u4e00\u5757\u5730\u5403\uff08for &#8230; in &#8230;\uff09<\/p>\n<p>\u8fd8\u662f\u518d\u5f3a\u8c03\u4e00\u4e0b\u5b57\u4e32\uff08string\uff09\u662f\u4e32\u513f\uff0c\u5217\u8868\uff08list\uff09\u4e5f\u662f\u4e32\u513f\uff0c\u6709\u4e86\u975e\u5e38\u7a33\u56fa\u7684\u300c\u70e4\u4e32\u89c2\u300d\u4ee5\u540e\uff0c\u5c31\u53ef\u4ee5\u8ddf\u7740Python\u4e00\u5757\u513f\u5403\u8089\u4e86\u3002\u6700\u7b80\u5355\u7684\u662f for &#8230; in &#8230; \u8fd9\u4e2a\u547d\u4ee4\uff0c\u7528\u6cd5\u5982\u4e0b\uff1a<\/p>\n<p>[code language=&#8221;python&#8221;]<br \/>\nfor \u65b0\u5efa\u53d8\u91cf in \u5df2\u6709\u7684\u4e32\u513f:<br \/>\n\t\u505a\u70b9\u513f\u4ec0\u4e48<br \/>\n[\/code]<\/p>\n<p>[code language=&#8221;python&#8221;]<br \/>\n#\u811a\u672c4<br \/>\nmylist=[&#8216;Python&#8217;, &#8216;so&#8217;, &#8216;easy&#8217;]<br \/>\nfor words in mylist:<br \/>\n\tprint words<br \/>\n# &gt;&gt; Python<br \/>\n# so<br \/>\n# easy<br \/>\n[\/code]<\/p>\n<p>\u8fd9\u662f\u7b2c\u4e00\u6b21\u63a5\u89e6\u5230\u6240\u8c13loop\uff08\u5faa\u73af\uff09\u7684\u6982\u5ff5\u3002\u5176\u5b9e\u8bf4\u5b9e\u5728\u7684\uff0cfor&#8230; in&#8230;\u8fd9\u4e2a\u547d\u4ee4\u7ec4\u5408\u4e00\u70b9\u4e5f\u6ca1\u6709\u300c\u5faa\u73af\u300d\u7684\u611f\u89c9\uff0c\u5b83\u7684\u529f\u80fd\u5f88\u7b80\u5355\uff1a<\/p>\n<p>\u5c06\u300c\u4e32\u300d\u91cc\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u8bfb\u53d6\u5230\u67d0\u4e2a\u300c\u65b0\u5efa\u53d8\u91cf\u300d\u4e4b\u540e\uff08\u6bd4\u5982\u811a\u672c4\u91cc\u662fwords\uff09\uff0c\u518d\u300c\u505a\u70b9\u513f\u4ec0\u4e48\u300d\uff08\u811a\u672c4\u91cc\u662fprint words\uff09\uff0c\u505a\u5b8c\u4e4b\u540e\u7ee7\u7eed\u8bfb\u53d6\u4e0b\u4e00\u4e2a\u5143\u7d20\uff0c\u8986\u76d6\u521a\u624d\u90a3\u4e2a\u300c\u65b0\u5efa\u53d8\u91cf\u300d\uff0c\u518d\u505a\u300c\u540c\u6837\u7684\u4e8b\u60c5\u300d\u2026\u2026\u4ee5\u6b64\u7c7b\u63a8\u2026\u2026\u76f4\u5230\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u88ab\u8bfb\u53d6\u5230\u8fd9\u4e2a\u300c\u65b0\u5efa\u53d8\u91cf\u300d\u4e4b\u540e\uff0c\u300c\u540c\u6837\u7684\u4e8b\u60c5\u300d\u4e5f\u5e72\u5b8c\u4e86\uff0c\u5c31\u7ed3\u675f\u3002<\/p>\n<p>\u4e00\u822c\u6765\u8bf4for &#8230; in &#8230;\u662f\u4e0d\u4f1a\u9677\u5165\u6b7b\u5faa\u73af\u7684\uff0c\u56e0\u4e3a\u4e00\u4e2a\u300c\u4e32\u300d\u7684\u5f00\u5934\u548c\u7ed3\u5c3e\u90fd\u5df2\u7ecf\u5b9a\u4e49\u597d\u4e86\u3002\u9664\u975e\u662f\u4f7f\u7528\u53d8\u91cf\u3010\u505a\u7684\u4e8b\u60c5\u6709\u95ee\u9898\u3011\uff0cPython\u4e00\u76f4\u5e72\u53c8\u5e72\u4e0d\u5b8c\uff1b\u6216\u8005\u662f\u4f60\u5728\u4f7f\u7528\u53d8\u91cf\u7684\u65f6\u5019\uff0c\u505a\u7684\u4e8b\u60c5\u8ba9\u300c\u4e32\u300d\u4e0a\u7684\u5143\u7d20\u53d8\u5f97\u8d8a\u6765\u8d8a\u591a\uff0cPython\u81ea\u7136\u6c38\u8fdc\u8bfb\u4e0d\u5230\u300c\u4e32\u300d\u6700\u540e\u7684\u90a3\u4e2a\u5143\u7d20\u3002\u8fd9\u4e2a\u65f6\u5019Ctrl+C\u662f\u6700\u597d\u7684\u670b\u53cb\uff0c\u53ef\u4ee5\u624b\u52a8\u4e2d\u65ad\u4e00\u4e2a\u811a\u672c\u3002<\/p>\n<p>\u5728\u53e5\u6cd5\u4e0a\uff0c\u6709\u4e24\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u70b9\u5343\u4e07\u8981\u6ce8\u610f\uff1a\u7b2c\u4e00\u70b9\u662ffor &#8230; in &#8230; \u7684\u90a3\u884c\u8981\u7528\u5192\u53f7:\u6765\u7ed3\u675f\uff1b\u7b2c\u4e8c\u70b9\u662f\u63a5\u7740\u7684\u90a3\u884c\u9700\u8981\u4f7f\u7528\u3010\u4e00\u4e2a\u3011Tab\u952e\u4f5c\u4e3a\u8d77\u59cb\uff08\u5f3a\u8c03\u300c\u4e00\u4e2a\u300d\u662f\u56e0\u4e3a\u6709\u53ef\u80fd\u4f1a\u9047\u4e0a\u5faa\u73af\u5957\u5faa\u73af\u7684\u72b6\u51b5\uff0c\u8fd9\u65f6\u524d\u9762\u6709\u51e0\u4e2aTab\u952e\u5c31\u975e\u5e38\u5173\u952e\u4e86\uff09\u3002<\/p>\n<p>\u8fd8\u662f\u5b9e\u6218\u5427\uff0c\u4e0d\u8fc7\u8fd9\u6b21\u7684\u5b9e\u6218\u9700\u8981\u4e00\u70b9\u590d\u4e60\u548c\u94fa\u57ab\u3002\u4efb\u52a1\u662f\u3010\u6211\u4eec\u6709\u4e0d\u957f\u7684\u51e0\u53e5\u8bdd\uff0c\u60f3\u77e5\u9053\u7279\u5b9a\u7684\u5143\u97f3\u5728\u8fd9\u51e0\u53e5\u8bdd\u91cc\u7684\u54ea\u51e0\u4e2a\u8bcd\u91cc\u51fa\u73b0\u8fc7\uff0c\u5728\u51fa\u73b0\u8fd9\u4e2a\u5143\u97f3\u7684\u5355\u8bcd\u91cc\u8fd9\u4e2a\u5143\u97f3\u53c8\u51fa\u73b0\u8fc7\u51e0\u6b21\u3011<\/p>\n<p>[code language=&#8221;python&#8221;]<br \/>\n#\u811a\u672c5<br \/>\n#\u8f93\u5165\u6587\u672c\uff0c\u7a0d\u5fae\u6574\u7406\u4e00\u4e0b#<br \/>\ntext=&quot;Python is a widely used general-purpose, high-level programming language. Its design philosophy emphasizes code readability.&quot;<br \/>\ntext_cl=text.lower().replace(&#8216;,&#8217;,&#8221;).replace(&#8216;.&#8217;,&#8221;)<br \/>\nwords=text_cl.split()<br \/>\n#\u95ee\u4e0b\u7528\u6237\u60f3\u67e5\u54ea\u4e2a\u5143\u97f3#<br \/>\nprint &#8216;You are reading the following text:\\n&#8217;<br \/>\nprint text+&#8217;\\n&#8217;<br \/>\nvowel=raw_input(&#8216;Which vowel is in your mind? &#8216;)<br \/>\n#\u6309\u987a\u5e8f\u8bfb\u4e00\u4e2a\uff0c\u627e\u4e00\u4e2a\uff0cprint\u4e00\u4e2a#<br \/>\nfor word in words:<br \/>\n\trst=str(word.count(vowel))<br \/>\n\tprint word+&#8217;\\t&#8217;+rst<br \/>\n[\/code]<\/p>\n<hr>\n<p>\u5c0f\u6284\uff1a\u4e32[\u6570\u5b57]  \u4e32[\u6570\u5b57:\u6570\u5b57]     len(\u4e32)    for&#8230; in&#8230; \u55ef\uff1f\\t \\n\uff1f<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5efa\u8bae\u5148\u770b\u5b8c\u524d\u9762\u7684\u7ae0\u8282\uff0c\u524d\u9762\u7ae0\u8282\u5185\u5bb9\uff1a<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"http:\/\/yellingstone.info\/index.php?rest_route=\/wp\/v2\/posts\/135"}],"collection":[{"href":"http:\/\/yellingstone.info\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/yellingstone.info\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/yellingstone.info\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/yellingstone.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=135"}],"version-history":[{"count":0,"href":"http:\/\/yellingstone.info\/index.php?rest_route=\/wp\/v2\/posts\/135\/revisions"}],"wp:attachment":[{"href":"http:\/\/yellingstone.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=135"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/yellingstone.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=135"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/yellingstone.info\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=135"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}