{"id":204844,"date":"2025-05-29T10:06:56","date_gmt":"2025-05-29T02:06:56","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204844\/"},"modified":"2025-05-29T10:06:56","modified_gmt":"2025-05-29T02:06:56","slug":"python-%e4%b8%ad-count-%e5%87%bd%e6%95%b0%e5%a6%82%e4%bd%95%e7%bb%9f%e8%ae%a1%e6%96%87%e6%9c%ac%e6%96%87%e4%bb%b6%e4%b8%ad%e7%89%b9%e5%ae%9a%e5%ad%97%e7%ac%a6%e7%9a%84%e6%ac%a1%e6%95%b0%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204844\/","title":{"rendered":"Python \u4e2d count \u51fd\u6570\u5982\u4f55\u7edf\u8ba1\u6587\u672c\u6587\u4ef6\u4e2d\u7279\u5b9a\u5b57\u7b26\u7684\u6b21\u6570\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Python \u4e2d count \u51fd\u6570\u5982\u4f55\u7edf\u8ba1\u6587\u672c\u6587\u4ef6\u4e2d\u7279\u5b9a\u5b57\u7b26\u7684\u6b21\u6570\uff1f<\/h1>\n<p>\u5927\u5bb6\u597d\uff0c\u4eca\u5929\u672c\u4eba\u7ed9\u5927\u5bb6\u5e26\u6765\u6587\u7ae0\uff0c\u6587\u4e2d\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span><\/span>\uff0c\u5982\u679c\u4f60\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u90a3\u5c31\u8bf7\u5404\u4f4d\u670b\u53cb\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427~\u5e0c\u671b\u80fd\u771f\u6b63\u5e2e\u5230\u4f60\u4eec\uff0c\u8c22\u8c22\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241117\/17318387296739c309545ad.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>python \u4e2d count \u51fd\u6570\u7528\u6cd5\u7684\u56f0\u6270<\/strong><\/p>\n<p>\u4f5c\u4e3a python \u521d\u5b66\u8005\uff0c\u5728\u7f16\u5199\u4ee3\u7801\u7684\u8fc7\u7a0b\u4e2d\u96be\u514d\u4f1a\u9047\u5230\u4e00\u4e9b\u7591\u95ee\u548c\u56f0\u96be\u3002\u5176\u4e2d\u4e4b\u4e00\u5c31\u662f count \u51fd\u6570\u5982\u4f55\u5728\u6587\u672c\u6587\u4ef6\u4e2d\u67e5\u627e\u7279\u5b9a\u5b57\u7b26\u7684\u6b21\u6570\u3002<\/p>\n<p>\u5982\u4e0a\u6240\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5c1d\u8bd5\u7edf\u8ba1\u540d\u4e3a &#8220;paper1.txt&#8221; \u7684\u6587\u4ef6\u4e2d &#8220;the&#8221; \u5b57\u7b26\u51fa\u73b0\u7684\u6b21\u6570\u3002<\/p>\n<p>\u521d\u59cb\u4ee3\u7801\uff1a<\/p>\n<pre>file_name = 'paper1.txt'\nwith open(file_name) as fn:\n    lines = fn.readlines()\nfor line in lines:\n        line.count('the')<\/pre>\n<p>\u7136\u800c\uff0c\u5728\u7ec8\u7aef\u4e2d\u8fd0\u884c\u6b64\u4ee3\u7801\u540e\uff0c\u5374\u6ca1\u6709\u5982\u9884\u671f\u822c\u663e\u793a\u4efb\u4f55\u7ed3\u679c\u3002\u8fd9\u662f\u56e0\u4e3a python \u7684 count \u51fd\u6570\u672c\u8eab\u4e0d\u4f1a\u8f93\u51fa\u7ed3\u679c\uff0c\u800c\u662f\u8fd4\u56de\u4e00\u4e2a\u503c\u3002\u4e3a\u4e86\u5728\u7ec8\u7aef\u4e2d\u770b\u5230\u7ed3\u679c\uff0c\u9700\u8981\u5728\u6bcf\u884c\u540e\u6dfb\u52a0 print \u8bed\u53e5\u3002<\/p>\n<p>\u4fee\u6b63\u540e\u7684\u4ee3\u7801\uff1a<\/p>\n<pre>file_name = 'paper1.txt'\nwith open(file_name) as fn:\n    lines = fn.readlines()\nfor line in lines:\n        print(line.count('the'))<\/pre>\n<p>\u901a\u8fc7\u4f7f\u7528 print\uff0ccount \u51fd\u6570\u7684\u8fd4\u56de\u503c\u5c06\u8f93\u51fa\u5230\u7ec8\u7aef\uff0c\u663e\u793a &#8220;the&#8221; \u5b57\u7b26\u5728\u6587\u4ef6\u4e2d\u51fa\u73b0\u7684\u6b21\u6570\u3002<\/p>\n<p>\u56e0\u6b64\uff0c\u5728 count \u51fd\u6570\u4e2d\u8f93\u51fa\u7ed3\u679c\u65f6\uff0c\u9700\u8981\u7262\u8bb0\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<ul>\n<li>\u901a\u8fc7 print \u663e\u793a\u51fd\u6570\u7684\u8fd4\u56de\u503c<\/li>\n<li>\u5728\u6bcf\u884c\u540e\u6dfb\u52a0 print \u8bed\u53e5<\/li>\n<\/ul>\n<p>\u4eca\u5929\u5173\u4e8e\u300aPython \u4e2d count \u51fd\u6570\u5982\u4f55\u7edf\u8ba1\u6587\u672c\u6587\u4ef6\u4e2d\u7279\u5b9a\u5b57\u7b26\u7684\u6b21\u6570\uff1f\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python \u4e2d count \u51fd&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-204844","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204844","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=204844"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204844\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204844"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204844"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204844"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}