{"id":171280,"date":"2023-08-01T04:48:28","date_gmt":"2023-07-31T20:48:28","guid":{"rendered":"http:\/\/www.idc.net\/help\/171280\/"},"modified":"2023-08-01T04:48:28","modified_gmt":"2023-07-31T20:48:28","slug":"%e5%90%8e%e6%b5%aa%e4%ba%91python%e6%95%99%e7%a8%8b%ef%bc%9a%e6%80%8e%e4%b9%88%e7%94%a8python%e8%be%93%e5%87%ba%e5%92%8c%e8%be%93%e5%85%a5%e6%96%87%e4%bb%b6%e5%8f%8a%e4%bf%a1%e6%81%af","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/171280\/","title":{"rendered":"\u540e\u6d6a\u4e91Python\u6559\u7a0b\uff1a\u600e\u4e48\u7528python\u8f93\u51fa\u548c\u8f93\u5165\u6587\u4ef6\u53ca\u4fe1\u606f?"},"content":{"rendered":"<p style=\"line-height: 1.75em\"><span>\u6240\u6709\u4ee3\u7801\u7a0b\u5e8f\u7531\u81ea\u5df1\u7f16\u5199\u540e\uff0c\u603b\u5f52\u662f\u4e3a\u4e86\u670d\u52a1\u7528\u6237\uff0c\u56e0\u4e3a\u5728\u4e86\u89e3\u5230\u4fe1\u606f\u65f6\uff0c\u4f7f\u7528\u7f16\u7a0b\u8f6f\u4ef6\uff0c\u8981\u53bb\u81ea\u52a8\u5316\u5904\u7406\u8fd9\u4e9b\u5185\u5bb9\uff0c\u600e\u4e48\u505a\u5462\uff1f\u4e00\u8d77\u6765\u770b\u4e0b\u5427~<\/span><\/p>\n<p style=\"line-height: 1.75em\"><strong><span>\u5229\u7528\u8bed\u53e5\u6709\uff1ainput\u548cprint\u8bed\u53e5<\/span><\/strong><\/p>\n<p style=\"line-height: 1.75em\"><strong><span>\u5173\u4e8eInput\u4ee3\u7801\u6f14\u793a\uff1a<\/span><\/strong><\/p>\n<pre>name&nbsp;=&nbsp;input('your&nbsp;name:')\ngender&nbsp;=&nbsp;input('you&nbsp;are&nbsp;a&nbsp;boy?(y\/n)')\n&nbsp;\n######&nbsp;\u8f93\u5165&nbsp;######\nyour&nbsp;name:Jack\nyou&nbsp;are&nbsp;a&nbsp;boy?\n&nbsp;\nwelcome_str&nbsp;=&nbsp;'Welcome&nbsp;to&nbsp;the&nbsp;matrix&nbsp;{prefix}&nbsp;{name}.'\nwelcome_dic&nbsp;=&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;'prefix':&nbsp;'Mr.'&nbsp;if&nbsp;gender&nbsp;==&nbsp;'y'&nbsp;else&nbsp;'Mrs',\n&nbsp;&nbsp;&nbsp;&nbsp;'name':&nbsp;name\n}\n&nbsp;\nprint('authorizing...')\nprint(welcome_str.format(**welcome_dic))\n&nbsp;\n##########&nbsp;\u8f93\u51fa&nbsp;##########\nauthorizing...\nWelcome&nbsp;to&nbsp;the&nbsp;matrix&nbsp;Mr.&nbsp;Jack.<\/pre>\n<p style=\"line-height: 1.75em\"><span>input\u51fd\u6570\u6682\u505c\u8fd0\u884c\uff0c\u7b49\u5f85\u952e\u76d8\u8f93\u5165\uff0c\u76f4\u5230\u6309\u4e0b\u56de\u8f66\uff0c\u8f93\u5165\u7684\u7c7b\u578b\u6c38\u8fdc\u662f\u5b57\u7b26\u4e32<\/span><\/p>\n<pre>a&nbsp;=&nbsp;input()\n1\nb&nbsp;=&nbsp;input()\n2\n&nbsp;\nprint('a&nbsp;+&nbsp;b&nbsp;=&nbsp;{}'.format(a&nbsp;+&nbsp;b))\n##########&nbsp;\u8f93\u51fa&nbsp;##############\na&nbsp;+&nbsp;b&nbsp;=&nbsp;12\nprint('type&nbsp;of&nbsp;a&nbsp;is&nbsp;{},&nbsp;type&nbsp;of&nbsp;b&nbsp;is&nbsp;{}'.format(type(a),&nbsp;type(b)))\n##########&nbsp;\u8f93\u51fa&nbsp;##############\ntype&nbsp;of&nbsp;a&nbsp;is&nbsp;&lt;class&nbsp;'str'&gt;,&nbsp;type&nbsp;of&nbsp;b&nbsp;is&nbsp;&lt;class&nbsp;'str'&gt;\nprint('a&nbsp;+&nbsp;b&nbsp;=&nbsp;{}'.format(int(a)&nbsp;+&nbsp;int(b)))\n##########&nbsp;\u8f93\u51fa&nbsp;##############\na&nbsp;+&nbsp;b&nbsp;=&nbsp;3<\/pre>\n<p style=\"line-height: 1.75em\"><strong><span>\u6587\u4ef6\u8f93\u5165\u548c\u8f93\u51fa<\/span><\/strong><\/p>\n<p style=\"line-height: 1.75em\"><span>\u751f\u4ea7\u7ea7\u522b\u7684 Python \u4ee3\u7801\uff0c\u5927\u90e8\u5206 I\/O \u5219\u6765\u81ea\u4e8e\u6587\u4ef6\uff0c<\/span><span>\u8fd9\u91cc\u6709\u4e2ain.text\uff1a<\/span><\/p>\n<pre>Mr.&nbsp;Johnson&nbsp;had&nbsp;never&nbsp;been&nbsp;up&nbsp;in&nbsp;an&nbsp;aerophane&nbsp;before&nbsp;and&nbsp;he&nbsp;had&nbsp;read&nbsp;a&nbsp;lot&nbsp;about&nbsp;air&nbsp;accidents,&nbsp;so&nbsp;one&nbsp;day&nbsp;when&nbsp;a&nbsp;friend&nbsp;offered&nbsp;to&nbsp;take&nbsp;him&nbsp;for&nbsp;a&nbsp;ride&nbsp;in&nbsp;his&nbsp;own&nbsp;small&nbsp;phane,&nbsp;Mr.&nbsp;Johnson&nbsp;was&nbsp;very&nbsp;worried&nbsp;about&nbsp;accepting.&nbsp;Finally,&nbsp;however,&nbsp;his&nbsp;friend&nbsp;persuaded&nbsp;him&nbsp;that&nbsp;it&nbsp;was&nbsp;very&nbsp;safe,&nbsp;and&nbsp;Mr.&nbsp;Johnson&nbsp;boarded&nbsp;the&nbsp;plane.\n&nbsp;\nHis&nbsp;friend&nbsp;started&nbsp;the&nbsp;engine&nbsp;and&nbsp;began&nbsp;to&nbsp;taxi&nbsp;onto&nbsp;the&nbsp;runway&nbsp;of&nbsp;the&nbsp;airport.&nbsp;Mr.&nbsp;Johnson&nbsp;had&nbsp;heard&nbsp;that&nbsp;the&nbsp;most&nbsp;dangerous&nbsp;part&nbsp;of&nbsp;a&nbsp;flight&nbsp;were&nbsp;the&nbsp;take-off&nbsp;and&nbsp;the&nbsp;landing,&nbsp;so&nbsp;he&nbsp;was&nbsp;extremely&nbsp;frightened&nbsp;and&nbsp;closed&nbsp;his&nbsp;eyes.\n&nbsp;\nAfter&nbsp;a&nbsp;minute&nbsp;or&nbsp;two&nbsp;he&nbsp;opened&nbsp;them&nbsp;again,&nbsp;looked&nbsp;out&nbsp;of&nbsp;the&nbsp;window&nbsp;of&nbsp;the&nbsp;plane,&nbsp;and&nbsp;said&nbsp;to&nbsp;his&nbsp;friend\u3002\n&nbsp;\n\"Look&nbsp;at&nbsp;those&nbsp;people&nbsp;down&nbsp;there.&nbsp;They&nbsp;look&nbsp;as&nbsp;small&nbsp;as&nbsp;ants,&nbsp;don't&nbsp;they?\"\n&nbsp;\n\"Those&nbsp;are&nbsp;ants,\"&nbsp;answered&nbsp;his&nbsp;friend.&nbsp;\"We're&nbsp;still&nbsp;on&nbsp;the&nbsp;ground.\"<\/pre>\n<p style=\"line-height: 1.75em\"><strong><span>\u73b0\u5728<\/span><span>\u8bfb\u53d6\u6587\u4ef6\uff1a<\/span><\/strong><\/p>\n<ul>\n<li>\n<p style=\"line-height: 1.75em\"><span>\u53bb\u6389\u6240\u6709\u6807\u70b9\u548c\u6362\u884c\u7b26\uff0c\u5c06\u5927\u5199\u53d8\u4e3a\u5c0f\u5199<\/span><\/p>\n<\/li>\n<li>\n<p style=\"line-height: 1.75em\"><span>\u5408\u5e76\u76f8\u540c\u7684\u8bcd\uff0c\u7edf\u8ba1\u6bcf\u4e2a\u8bcd\u51fa\u73b0\u7684\u9891\u7387\uff0c\u5c06\u8bcd\u9891\u4ece\u5927\u5230\u5c0f\u6392\u5e8f<\/span><\/p>\n<\/li>\n<li>\n<p style=\"line-height: 1.75em\"><span>\u5c06\u7ed3\u679c\u6309\u884c\u8f93\u51fa\u6587\u4ef6out.txt<\/span><\/p>\n<\/li>\n<\/ul>\n<pre>import&nbsp;re\n&nbsp;\n#&nbsp;\u4f60\u4e0d\u7528\u592a\u5173\u5fc3\u8fd9\u4e2a\u51fd\u6570\ndef&nbsp;parse(text):\n&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u53bb\u9664\u6807\u70b9\u7b26\u53f7\u548c\u6362\u884c\u7b26\n&nbsp;&nbsp;&nbsp;&nbsp;text&nbsp;=&nbsp;re.sub(r'[^\\w&nbsp;]',&nbsp;'',&nbsp;text)\n&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;\u8f6c\u4e3a\u5c0f\u5199\n&nbsp;&nbsp;&nbsp;&nbsp;text&nbsp;=&nbsp;text.lower()\n&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;\u751f\u6210\u6240\u6709\u5355\u8bcd\u7684\u5217\u8868\n&nbsp;&nbsp;&nbsp;&nbsp;word_list&nbsp;=&nbsp;text.split('&nbsp;')\n&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;\u53bb\u9664\u7a7a\u767d\u5355\u8bcd\n&nbsp;&nbsp;&nbsp;&nbsp;word_list&nbsp;=&nbsp;filter(None,&nbsp;word_list)\n&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;\u751f\u6210\u5355\u8bcd\u548c\u8bcd\u9891\u7684\u5b57\u5178\n&nbsp;&nbsp;&nbsp;&nbsp;word_cnt&nbsp;=&nbsp;{}\n&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;word&nbsp;in&nbsp;word_list:\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;word&nbsp;not&nbsp;in&nbsp;word_cnt:\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;word_cnt[word]&nbsp;=&nbsp;0\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;word_cnt[word]&nbsp;+=&nbsp;1\n&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;\u6309\u7167\u8bcd\u9891\u6392\u5e8f\n&nbsp;&nbsp;&nbsp;&nbsp;sorted_word_cnt&nbsp;=&nbsp;sorted(word_cnt.items(),&nbsp;key=lambda&nbsp;kv:&nbsp;kv[1],&nbsp;reverse=True)\n&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;sorted_word_cnt\n&nbsp;\nwith&nbsp;open('in.txt',&nbsp;'r')&nbsp;as&nbsp;fin:\n&nbsp;&nbsp;&nbsp;&nbsp;text&nbsp;=&nbsp;fin.read()\n&nbsp;\nword_and_freq&nbsp;=&nbsp;parse(text)\n&nbsp;\nwith&nbsp;open('out.txt',&nbsp;'w')&nbsp;as&nbsp;fout:\n&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;word,&nbsp;freq&nbsp;in&nbsp;word_and_freq:\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fout.write('{}&nbsp;{}\\n'.format(word,&nbsp;freq))\n&nbsp;\n##########&nbsp;\u8f93\u51fa&nbsp;(\u7701\u7565\u8f83\u957f\u7684\u4e2d\u95f4\u7ed3\u679c)&nbsp;##########<\/pre>\n<p style=\"line-height: 1.75em\"><span><\/span><\/p>\n<p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/oss.py.cn\/pycn\/upload\/image\/633\/162\/590\/1603790031590317.png\" style=\"width: 650px;height: 369px\" class=\"aligncenter\"><\/p>\n<p style=\"line-height: 1.75em\"><span>\u5927\u5bb6\u4e5f\u53ef\u4ee5\u6839\u636e\u4e0a\u9762\u4ee3\u7801\u6559\u5b66\uff0c\u5957\u7528\u5c1d\u8bd5\u4e0b\u54e6~\u5982\u9700\u66f4\u591apython\u5b9e\u7528\u77e5\u8bc6\uff0c\u70b9\u51fb\u8fdb\u5165<span>PyThon\u5b66\u4e60\u7f51\u6559\u5b66\u4e2d\u5fc3<\/span>\u3002<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6240\u6709\u4ee3\u7801\u7a0b\u5e8f\u7531\u81ea\u5df1\u7f16\u5199\u540e\uff0c\u603b\u5f52\u662f\u4e3a\u4e86\u670d\u52a1\u7528\u6237\uff0c\u56e0\u4e3a\u5728\u4e86\u89e3\u5230\u4fe1\u606f\u65f6\uff0c\u4f7f\u7528\u7f16\u7a0b\u8f6f\u4ef6\uff0c\u8981\u53bb\u81ea\u52a8\u5316\u5904\u7406\u8fd9\u4e9b\u5185\u5bb9\uff0c\u600e\u4e48\u505a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":171281,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[182397],"tags":[],"class_list":["post-171280","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/171280","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=171280"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/171280\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media\/171281"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=171280"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=171280"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=171280"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}