{"id":131,"date":"2016-01-13T19:53:02","date_gmt":"2016-01-13T19:53:02","guid":{"rendered":"http:\/\/yellingstone.info\/?p=131"},"modified":"2016-04-13T00:19:25","modified_gmt":"2016-04-12T22:19:25","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%e4%b8%89%e7%ab%a0%ef%bc%89","status":"publish","type":"post","link":"http:\/\/yellingstone.info\/?p=131","title":{"rendered":"\u6838\u5fc3\u8bed\u8a00\u5b66\u65e0\u7f16\u7a0b\u57fa\u7840\u5165\u95e8Python\uff08\u7b2c\u4e09\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<\/ol>\n<hr>\n<p>5 \u6211\u771f\u7684\u4e0d\u60f3\u770b\u5927\u6bb5\u7684\u5b57\u4e32\u554a\uff01\uff08list\u7684\u6982\u5ff5\uff09<\/p>\n<p>\u8fd9\u7ae0\u6211\u89c9\u5f97\u4f1a\u65e0\u6bd4\u7684\u96be\uff0c\u56e0\u4e3a\u8981\u91cd\u65b0\u7406\u89e3\u4e00\u4e0bPython\u5bf9\u4e8e\u6570\u636e\u7684\u6574\u7406\u6a21\u5f0f\u3002\u5728\u5b66\u4e60\u7528=\u8fdb\u884c\u8d4b\u503c\u7684\u65f6\u5019\u5927\u6982\u63d0\u4e86\u4e00\u4e0b\uff0c\u7528=\u53ef\u4ee5\u7ed9\u4e00\u4e2a\u53d8\u91cf\u8d4b\u4e88\u4e00\u4e2a\u300c\u5b57\u4e32\u300d\u7684\u503c\uff08\u5982\u811a\u672c1\uff09\uff0c\u4e5f\u53ef\u4ee5\u7ed9\u4e00\u4e2a\u53d8\u91cf\u8d4b\u4e88\u4e00\u4e2a\u300c\u6570\u5b57\u300d\u7684\u503c\uff08\u5982\u811a\u672c2\uff09\u3002<\/p>\n<p>[code language=&#8221;python&#8221;]<br \/>\n#\u811a\u672c1<br \/>\na=&quot;Python so easy&quot;<br \/>\n[\/code]<\/p>\n<p>[code language=&#8221;python&#8221;]<br \/>\n#\u811a\u672c2<br \/>\na=2016<br \/>\n[\/code]<\/p>\n<p>\u53ef\u662f\u5bf9\u4e8e\u8bed\u8a00\u5b66\u5de5\u4f5c\u8005\u6765\u8bf4\uff0c\u5982\u679c\u53ea\u6709\u5b57\u4e32\uff08string\uff09\u80fd\u5904\u7406\u6587\u5b57\uff0c\u5c31\u7b97\u662f\u5b57\u4e32\u547d\u4ee4\u5f88\u591a\uff08\u89c1\u7b2c\u4e8c\u7ae0\uff09\uff0c\u4e5f\u5e38\u6709\u6349\u895f\u89c1\u8098\u7684\u611f\u89c9\u3002\u597d\u5728\uff0cPython\u5f88\u53cb\u5584\u5730\u52a0\u5165\u4e86\u4e00\u4e2a\u53eb\u505a\u5217\u8868\uff08list\uff09\u7684\u53d8\u91cf\u7c7b\u578b\uff0c\u5f62\u8c61\u4e00\u70b9\u6765\u8bf4\u5c31\u662f\u4e00\u53ea\u5927\u9c7c\u5403\u4e86\u4e00\u53ea\u5c0f\u9c7c\uff0c\u8fd9\u53ea\u5c0f\u9c7c\u53ef\u4ee5\u662f\u5b57\u4e32\uff08string\uff09\uff0c\u53ef\u4ee5\u662f\u6570\u5b57\uff08number\uff09\uff0c\u4e5f\u53ef\u4ee5\u662f\u2026\u2026\u7b97\u4e86\uff0c\u5176\u4ed6\u7684\u73b0\u5728\u8fd8\u4e0d\u91cd\u8981\u3002\u91cd\u8981\u7684\u662f\uff0c\u7ed9\u4e00\u4e2a\u53d8\u91cf\u8d4b\u503c\u4e3a\u5217\u8868\u7684\u65f6\u5019\u8981\u4f7f\u7528\u4e2d\u62ec\u53f7[]\u3002<\/p>\n<p>[code language=&#8221;python&#8221;]<br \/>\n#\u811a\u672c3<br \/>\na=[&quot;Python&quot;, &quot;so&quot;, &quot;easy&quot;]<br \/>\nprint a<br \/>\n# =&gt; [&quot;Python&quot;, &quot;so&quot;, &quot;easy&quot;]<br \/>\n[\/code]<\/p>\n<p>\u4ece\u5b9e\u7528\u7684\u89d2\u5ea6\u6765\u8bf4\uff0c\u5982\u679c\u6211\u6709\u4e00\u53e5\u8bdd\uff0c\u6211\u628a\u8fd9\u53e5\u8bdd\u5b9a\u4e49\u4e3a\u5b57\u4e32\uff08string\uff09\u4e4b\u540e\uff0c\u6211\u53ef\u4ee5\u4f7f\u7528\u5b57\u4e32\u7684split\u547d\u4ee4\u975e\u5e38\u8f7b\u677e\u5730\u3010\u628a\u8fd9\u53e5\u8bdd\u91cc\u7684\u6bcf\u4e00\u4e2a\u5355\u8bcd\u53d8\u6210\u4e00\u4e2a\u5b57\u4e32\uff0c\u5e76\u653e\u8fdb\u4e00\u4e2a\u5217\u8868\u91cc\u3011\uff08\u5982\u811a\u672c4\uff09\u3002\uff08\u8bf4\u597d\u7684wordlist\u8fd9\u4e48\u5feb\u5c31\u8981\u6765\u4e86\uff1f\uff01\uff09<\/p>\n<p>[code language=&#8221;python&#8221;]<br \/>\n#\u811a\u672c4<br \/>\ntext=&quot;Python is a widely used general-purpose, high-level programming language.&quot;<br \/>\nwords=text.split()<br \/>\nprint words<br \/>\n# =&gt; [&#8216;Python&#8217;, &#8216;is&#8217;, &#8216;a&#8217;, &#8216;widely&#8217;, &#8216;used&#8217;, &#8216;general-purpose,&#8217;, &#8216;high-level&#8217;, &#8216;programming&#8217;, &#8216;language.&#8217;]<br \/>\n[\/code]<\/p>\n<p>\u5217\u8868\uff08list\uff09\u548c\u5b57\u4e32\uff08string\uff09\u6709\u4e9b\u547d\u4ee4\u662f\u300c\u5171\u4eab\u300d\u7684\uff0c\u6bd4\u5982count()\u8fd9\u4e2a\u547d\u4ee4\uff0c\u4e5f\u53ef\u4ee5\u7528\u6765\u8ba1\u7b97\u4e00\u4e2a\u5217\u8868\u91cc\u67d0\u4e00\u4e2a\u5143\u7d20\u51fa\u73b0\u7684\u6b21\u6570\uff08\u811a\u672c5\uff09\uff1a<\/p>\n<p>[code language=&#8221;python&#8221;]<br \/>\n#\u811a\u672c5<br \/>\nmylist=[&#8216;Python&#8217;, &#8216;is&#8217;, &#8216;a&#8217;, &#8216;widely&#8217;, &#8216;used&#8217;, &#8216;general-purpose,&#8217;, &#8216;high-level&#8217;, &#8216;programming&#8217;, &#8216;language.&#8217;]<br \/>\nb=mylist.count(&#8216;is&#8217;)<br \/>\nprint b<br \/>\n# =&gt; 1<br \/>\n[\/code]<\/p>\n<p>\u53ef\u662f\u5728\u5b57\u4e32\uff08string\uff09\u91cc\u67e5\u627e\u67d0\u4e00\u4e2a\u5c0f\u5b57\u4e32\u4f4d\u7f6e\u7684\u547d\u4ee4find()\u5e76\u4e0d\u9002\u7528\u4e8e\u5217\u8868\uff08list\uff09\uff0c\u5728\u5904\u7406\u5217\u8868\u65f6\u9700\u8981\u4f7f\u7528index()\u8fd9\u4e2a\u547d\u4ee4\uff0c\u5c31\u50cf\u811a\u672c6\u4e00\u6837\u3002<\/p>\n<p>[code language=&#8221;python&#8221;]<br \/>\n#\u811a\u672c6<br \/>\nmylist=[&#8216;Python&#8217;, &#8216;is&#8217;, &#8216;a&#8217;, &#8216;widely&#8217;, &#8216;used&#8217;, &#8216;general-purpose,&#8217;, &#8216;high-level&#8217;, &#8216;programming&#8217;, &#8216;language.&#8217;]<br \/>\nb=mylist.index(&#8216;a&#8217;)<br \/>\nprint b<br \/>\n# =&gt; 2<br \/>\n[\/code]<\/p>\n<p>\u5176\u5b9e\u5bf9\u4e8e\u5217\u8868\uff08list\uff09\u6765\u8bf4\uff0c\u6700\u91cd\u8981\u7684\u547d\u4ee4\u4e5f\u8bb8\u5e76\u4e0d\u662fcount\u6216index\uff0c\u800c\u662fappend\uff08\u5728\u5217\u8868\u7684\u6700\u540e\u52a0\u5165\u4e00\u4e2a\u5143\u7d20\uff09\u548cpop\uff08\u628a\u5217\u8868\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u63d0\u53d6\u51fa\u6765\uff0c\u3010\u5e76\u4e14\u628a\u8fd9\u4e2a\u5143\u7d20\u4ece\u5217\u8868\u6700\u540e\u7684\u4f4d\u7f6e\u5220\u6389\u3011\uff09\u3002\u4ec0\u4e48\uff1f\u4f60\u6ca1\u542c\u8bf4\u8fc7\u3010\u4eba\u751f\u5c31\u662f\u4e0d\u505c\u5730append\u518dpop\u3011\u8fd9\u53e5\u8bdd\uff1f<\/p>\n<p><code>mylist.append('linguistics')<\/code>\t\t\u5728mylist\u8fd9\u4e2a\u5217\u8868\u7684\u6700\u540e\u52a0\u5165\u5b57\u4e32&#8221;linguistics&#8221;<br \/>\n<code>mylist.pop()<\/code>\t\t\t\t\t\u628a\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u62bd\u53d6\u51fa\u6765\uff08\u5343\u4e07\u8bb0\u5f97\u8981\u7528=\u3010\u8d4b\u503c\uff01\u3011\uff09<br \/>\n<code>mylist.insert(0,\"linguistics\")<\/code>\t\t\u5728\u6700\u524d\u9762\u52a0\u5165\u5b57\u4e32&#8221;linguistics&#8221;\uff08insert\u4e5f\u53ef\u4ee5\u7528\u4e8e\u63d2\u5165\u4efb\u4f55\u4f4d\u7f6e\uff09<br \/>\n<code>mylist.pop(0)<\/code>\t\t\t\t\t\u628a\u7b2c\u4e00\u4e2a\u5143\u7d20\u62bd\u53d6\u51fa\u6765\uff08\u5343\u4e07\u8bb0\u5f97\u8981\u7528=\u3010\u8d4b\u503c\uff01\u3011\uff09<\/p>\n<p>\u5b9e\u6218\u662f\u6700\u597d\u7684\u8001\u5e08\u3002\u3010\u8ba9\u6267\u884c\u811a\u672c\u7684\u4eba\u8f93\u5165\u4e09\u4e2a\u5b57\u4e32\uff0c\u5b58\u5728\u4e00\u4e2a\u5217\u8868\u91cc\u3011\u3002\u5bf9\u4e86\uff0c\u5fd8\u4e86\u8bf4\u4e00\u4e2a\u5c0f\u6280\u5de7\uff0c\u5728\u5bf9\u5217\u8868\uff08list\uff09\u8fdb\u884c\u64cd\u4f5c\u7684\u65f6\u5019\uff0c\u6700\u597d\u9884\u5148\u5c06\u90a3\u4e2a\u53d8\u91cf\u5b9a\u4e49\u6210\u4e00\u4e2a\u7a7a\u7684\u5217\u8868\uff0c\u4ee5\u514d\u53d1\u751f\u610f\u5916\u3002<\/p>\n<p>[code language=&#8221;python&#8221;]<br \/>\n#\u811a\u672c7<br \/>\nmylist=[]<br \/>\na1=raw_input(&quot;your first word? &quot;)<br \/>\nmylist.append(a1)<br \/>\na2=raw_input(&quot;your second word? &quot;)<br \/>\nmylist.append(a2)<br \/>\na3=raw_input(&quot;your third word? &quot;)<br \/>\nmylist.append(a3)<br \/>\nprint mylist<br \/>\n[\/code]<\/p>\n<hr>\n<p>\u3010\u9690\u85cf\u7ae0\u8282\u3011<\/p>\n<p>\u7a81\u7136\u89c9\u5f97\u5982\u679c\u8fd9\u6837\u5c31\u628a\u5173\u4e8e\u5217\u8868\uff08list\uff09\u7684\u90e8\u5206\u8fc7\u6389\uff0c\u5f88\u591a\u4eba\u53ef\u80fd\u4f1a\u4e0d\u5f00\u5fc3\u3002\u5217\u8868\uff08list\uff09\u6709\u4e00\u4e2a\u5e76\u4e0d\u5c11\u89c1\u7684\u7528\u6cd5\u662f\u3010\u5728\u5217\u8868\u91cc\u68c0\u67e5\u67d0\u4e2a\u5143\u7d20\u662f\u5426\u51fa\u73b0\u8fc7\u3011\uff08\u6216\u8005\u6362\u4e2a\u8bf4\u6cd5\uff0c\u5217\u8868\u662f\u5426\u5305\u542b\u67d0\u4e2a\u5143\u7d20\uff09\u3002\u53d7\u6b22\u8fce\u7684\u539f\u56e0\u53ef\u80fd\u662f\u56e0\u4e3a\u5217\u8868\uff08list\uff09\u672c\u8eab\u65e2\u53ef\u4ee5\u5305\u542b\u5b57\u4e32\uff08string\uff09\u4e5f\u53ef\u4ee5\u5305\u542b\u6570\u5b57\uff08number\uff09\uff0c\u6bd4\u5982\u811a\u672c8\u4e2d\u7684mylist\u8fd9\u4e2a\u5217\u8868\u91cc\u65e2\u6709\u5b57\u4e32\u7684&#8221;2&#8243;\u53c8\u6709\u6570\u5b57\u76843\u3002\u4f7f\u7528in\u53ef\u4ee5\u68c0\u67e5\u67d0\u4e2a\u5143\u7d20\u662f\u4e0d\u662f\u5728\u5217\u8868\u91cc\u3002<\/p>\n<p>[code language=&#8221;python&#8221;]<br \/>\n#\u811a\u672c8<br \/>\nmylist=[&quot;python&quot;, &quot;2&quot;, &quot;or&quot;, &quot;python&quot;, 3]<br \/>\nc3=3 in mylist<br \/>\nc2=2 in mylist<br \/>\nprint c3<br \/>\nprint c2<br \/>\n# =&gt;  True<br \/>\n# False<br \/>\n[\/code]<\/p>\n<p>\u7ed3\u679c\u662f\u8bf4\u5728mylist\u91cc\u627e\u5230\u4e86\u6570\u5b57\u76843\uff0c\u4f46\u662f\u6ca1\u627e\u5230\u6570\u5b57\u76842\u3002\u5343\u4e07\u8981\u6ce8\u610f\u7684\u662f\u8fd9\u91cc\u7684\u3010True\u548cFalse\u90fd\u4e0d\u662f\u5b57\u4e32\u3011\uff0c\u800c\u662f\u4ee3\u8868\u4e86\u4e00\u79cd\u300c\u903b\u8f91\u5173\u7cfb\u300d\uff0c\u751a\u81f3\u6700\u597d\u7406\u89e3\u4e3a\u4e24\u4e2a\u6570\u5b57\u2026True\u662f1\uff0cFalse\u662f0\u2026\u2026<\/p>\n<hr>\n<p>\u5c0f\u6284\uff1a\u5b57\u4e32.split()\uff0clist=[]\uff0c\u5217\u8868.count(\u67d0\u5143\u7d20)\uff0c\u5217\u8868.index(\u67d0\u5143\u7d20)\uff0cappend vs pop<\/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\/131"}],"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=131"}],"version-history":[{"count":0,"href":"http:\/\/yellingstone.info\/index.php?rest_route=\/wp\/v2\/posts\/131\/revisions"}],"wp:attachment":[{"href":"http:\/\/yellingstone.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=131"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/yellingstone.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=131"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/yellingstone.info\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=131"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}