{"id":2473,"date":"2017-07-20T10:53:15","date_gmt":"2017-07-20T02:53:15","guid":{"rendered":"http:\/\/cn.hostease.com\/xueyuan\/?p=2473"},"modified":"2025-01-08T16:00:40","modified_gmt":"2025-01-08T08:00:40","slug":"python%e8%bf%9b%e9%98%b606-%e5%be%aa%e7%8e%af%e5%af%b9%e8%b1%a1","status":"publish","type":"post","link":"https:\/\/cn.hostease.com\/xueyuan\/jishu\/python\/python%e8%bf%9b%e9%98%b606-%e5%be%aa%e7%8e%af%e5%af%b9%e8%b1%a1\/","title":{"rendered":"Python\u8fdb\u963606 \u5faa\u73af\u5bf9\u8c61"},"content":{"rendered":"<p>\u8fd9\u4e00\u8bb2\u7684\u4e3b\u8981\u76ee\u7684\u662f\u4e3a\u4e86\u5927\u5bb6\u5728\u8bfbPython\u7a0b\u5e8f\u7684\u65f6\u5019\u5bf9\u5faa\u73af\u5bf9\u8c61\u6709\u4e00\u4e2a\u57fa\u672c\u6982\u5ff5\u3002<\/p>\n<p>\u5faa\u73af\u5bf9\u8c61\u7684\u5e76\u4e0d\u662f\u968f\u7740Python\u7684\u8bde\u751f\u5c31\u5b58\u5728\u7684\uff0c\u4f46\u5b83\u7684\u53d1\u5c55\u8fc5\u901f\uff0c\u7279\u522b\u662fPython 3x\u7684\u65f6\u4ee3\uff0c\u5faa\u73af\u5bf9\u8c61\u6b63\u5728\u6210\u4e3a\u5faa\u73af\u7684\u6807\u51c6\u5f62\u5f0f\u3002<\/p>\n<h3>\u4ec0\u4e48\u662f\u5faa\u73af\u5bf9\u8c61<\/h3>\n<p>\u5faa\u73af\u5bf9\u8c61\u662f\u8fd9\u6837\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5b83\u5305\u542b\u6709\u4e00\u4e2anext()\u65b9\u6cd5(__next__()\u65b9\u6cd5\uff0c\u5728python 3x\u4e2d)\uff0c \u8fd9\u4e2a\u65b9\u6cd5\u7684\u76ee\u7684\u662f\u8fdb\u884c\u5230\u4e0b\u4e00\u4e2a\u7ed3\u679c\uff0c\u800c\u5728\u7ed3\u675f\u4e00\u7cfb\u5217\u7ed3\u679c\u4e4b\u540e\uff0c\u4e3e\u51faStopIteration\u9519\u8bef\u3002<\/p>\n<p>\u5f53\u4e00\u4e2a\u5faa\u73af\u7ed3\u6784\uff08\u6bd4\u5982for\uff09\u8c03\u7528\u5faa\u73af\u5bf9\u8c61\u65f6\uff0c\u5b83\u5c31\u4f1a\u6bcf\u6b21\u5faa\u73af\u7684\u65f6\u5019\u8c03\u7528next()\u65b9\u6cd5\uff0c\u76f4\u5230StopIteration\u51fa\u73b0\uff0cfor\u5faa\u73af\u63a5\u6536\u5230\uff0c\u5c31\u77e5\u9053\u5faa\u73af\u5df2\u7ecf\u7ed3\u675f\uff0c\u505c\u6b62\u8c03\u7528next()\u3002<\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2atest.txt\u7684\u6587\u4ef6:<\/p>\n<div class=\"cnblogs_code\">\n<pre>1234\nabcd\nefg<\/pre>\n<\/div>\n<p>\u6211\u4eec\u8fd0\u884c\u4e00\u4e0bpython\u547d\u4ee4\u884c\uff1a<\/p>\n<p>&gt;&gt;&gt;f = open(&#8216;test.txt&#8217;)<\/p>\n<p>&gt;&gt;&gt;f.next()<\/p>\n<p>&gt;&gt;&gt;f.next()<\/p>\n<p>&#8230;<br \/>\n\u4e0d\u65ad\u8f93\u5165f.next()\uff0c\u76f4\u5230\u6700\u540e\u51fa\u73b0StopIteration<\/p>\n<p>open()\u8fd4\u56de\u7684\u5b9e\u9645\u4e0a\u662f\u4e00\u4e2a\u5faa\u73af\u5bf9\u8c61\uff0c\u5305\u542b\u6709next()\u65b9\u6cd5\u3002\u800c\u8be5next()\u65b9\u6cd5\u6bcf\u6b21\u8fd4\u56de\u7684\u5c31\u662f\u65b0\u7684\u4e00\u884c\u7684\u5185\u5bb9\uff0c\u5230\u8fbe\u6587\u4ef6\u7ed3\u5c3e\u65f6\u4e3e\u51faStopIteration\u3002\u8fd9\u6837\uff0c\u6211\u4eec\u76f8\u5f53\u4e8e\u624b\u5de5\u8fdb\u884c\u4e86\u5faa\u73af\u3002<\/p>\n<p>\u81ea\u52a8\u8fdb\u884c\u7684\u8bdd\uff0c\u5c31\u662f\uff1a<\/p>\n<div class=\"cnblogs_code\">\n<pre>for line in open('test.txt'):\n    print line<\/pre>\n<\/div>\n<p>\u5728\u8fd9\u91cc\uff0cfor\u7ed3\u6784\u81ea\u52a8\u8c03\u7528next()\u65b9\u6cd5\uff0c\u5c06\u8be5\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u8d4b\u4e88\u7ed9line\u3002\u5faa\u73af\u77e5\u9053\u51fa\u73b0StopIteration\u7684\u65f6\u5019\u7ed3\u675f\u3002<\/p>\n<p>\u76f8\u5bf9\u4e8e\u5e8f\u5217\uff0c\u7528\u5faa\u73af\u5bf9\u8c61\u7684\u597d\u5904\u5728\u4e8e\uff1a\u4e0d\u7528\u5728\u5faa\u73af\u8fd8\u6ca1\u6709\u5f00\u59cb\u7684\u65f6\u5019\uff0c\u5c31\u751f\u6210\u597d\u8981\u4f7f\u7528\u7684\u5143\u7d20\u3002\u6240\u4f7f\u7528\u7684\u5143\u7d20\u53ef\u4ee5\u5728\u5faa\u73af\u8fc7\u7a0b\u4e2d\u9010\u6b21\u751f\u6210\u3002\u8fd9\u6837\uff0c\u8282\u7701\u4e86\u7a7a\u95f4\uff0c\u63d0\u9ad8\u4e86\u6548\u7387\uff0c\u7f16\u7a0b\u66f4\u7075\u6d3b\u3002<\/p>\n<h3>\u8fed\u4ee3\u5668<\/h3>\n<p>\u4ece\u6280\u672f\u4e0a\u6765\u8bf4\uff0c\u5faa\u73af\u5bf9\u8c61\u548cfor\u5faa\u73af\u8c03\u7528\u4e4b\u95f4\u8fd8\u6709\u4e00\u4e2a\u4e2d\u95f4\u5c42\uff0c\u5c31\u662f\u8981\u5c06\u5faa\u73af\u5bf9\u8c61\u8f6c\u6362\u6210\u8fed\u4ee3\u5668(iterator)\u3002\u8fd9\u4e00\u8f6c\u6362\u662f\u901a\u8fc7\u4f7f\u7528iter()\u51fd\u6570\u5b9e\u73b0\u7684\u3002\u4f46\u4ece\u903b\u8f91\u5c42\u9762\u4e0a\uff0c\u5e38\u5e38\u53ef\u4ee5\u5ffd\u7565\u8fd9\u4e00\u5c42\uff0c\u6240\u4ee5\u5faa\u73af\u5bf9\u8c61\u548c\u8fed\u4ee3\u5668\u5e38\u5e38\u76f8\u4e92\u6307\u4ee3\u5bf9\u65b9\u3002<\/p>\n<h3>\u751f\u6210\u5668<\/h3>\n<p>\u751f\u6210\u5668(generator)\u7684\u4e3b\u8981\u76ee\u7684\u662f\u6784\u6210\u4e00\u4e2a\u7528\u6237\u81ea\u5b9a\u4e49\u7684\u5faa\u73af\u5bf9\u8c61\u3002<\/p>\n<p>\u751f\u6210\u5668\u7684\u7f16\u5199\u65b9\u6cd5\u548c\u51fd\u6570\u5b9a\u4e49\u7c7b\u4f3c\uff0c\u53ea\u662f\u5728return\u7684\u5730\u65b9\u6539\u4e3ayield\u3002\u751f\u6210\u5668\u4e2d\u53ef\u4ee5\u6709\u591a\u4e2ayield\u3002\u5f53\u751f\u6210\u5668\u9047\u5230\u4e00\u4e2ayield\u65f6\uff0c\u4f1a\u6682\u505c\u8fd0\u884c\u751f\u6210\u5668\uff0c\u8fd4\u56deyield\u540e\u9762\u7684\u503c\u3002\u5f53\u518d\u6b21\u8c03\u7528\u751f\u6210\u5668\u7684\u65f6\u5019\uff0c\u4f1a\u4ece\u521a\u624d\u6682\u505c\u7684\u5730\u65b9\u7ee7\u7eed\u8fd0\u884c\uff0c\u76f4\u5230\u4e0b\u4e00\u4e2ayield\u3002\u751f\u6210\u5668\u81ea\u8eab\u53c8\u6784\u6210\u4e00\u4e2a\u5faa\u73af\u5668\uff0c\u6bcf\u6b21\u5faa\u73af\u4f7f\u7528\u4e00\u4e2ayield\u8fd4\u56de\u7684\u503c\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u751f\u6210\u5668:<\/p>\n<div class=\"cnblogs_code\">\n<pre>def gen():\n    a = 100\n    yield a\n    a = a*8\n    yield a\n    yield 1000<\/pre>\n<\/div>\n<p>\u8be5\u751f\u6210\u5668\u5171\u6709\u4e09\u4e2ayield\uff0c \u5982\u679c\u7528\u4f5c\u5faa\u73af\u5668\u65f6\uff0c\u4f1a\u8fdb\u884c\u4e09\u6b21\u5faa\u73af\u3002<\/p>\n<div class=\"cnblogs_code\">\n<pre>for i in gen():\n    print i<\/pre>\n<\/div>\n<p>\u518d\u8003\u8651\u5982\u4e0b\u4e00\u4e2a\u751f\u6210\u5668:<\/p>\n<div class=\"cnblogs_code\">\n<pre>def gen():\n    for i in range(4):\n        yield i<\/pre>\n<\/div>\n<p>\u5b83\u53c8\u53ef\u4ee5\u5199\u6210\u751f\u6210\u5668\u8868\u8fbe\u5f0f(Generator Expression):<\/p>\n<div class=\"cnblogs_code\">\n<pre>G = (x for x in range(4))<\/pre>\n<\/div>\n<p>\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u662f\u751f\u6210\u5668\u7684\u4e00\u79cd\u7b80\u4fbf\u7684\u7f16\u5199\u65b9\u5f0f\u3002\u8bfb\u8005\u53ef\u8fdb\u4e00\u6b65\u67e5\u9605\u3002<\/p>\n<h3>\u8868\u63a8\u5bfc<\/h3>\n<p>\u8868\u63a8\u5bfc(list comprehension)\u662f\u5feb\u901f\u751f\u6210\u8868\u7684\u65b9\u6cd5\u3002\u5b83\u7684\u8bed\u6cd5\u7b80\u5355\uff0c\u5f88\u6709\u5b9e\u7528\u4ef7\u503c\u3002<\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u751f\u6210\u8868L\uff1a<\/p>\n<div class=\"cnblogs_code\">\n<pre>L = []\nfor x in range(10):\n    L.append(x**2)<\/pre>\n<\/div>\n<p>\u4ee5\u4e0a\u4ea7\u751f\u4e86\u8868L\uff0c\u4f46\u5b9e\u9645\u4e0a\u6709\u5feb\u6377\u7684\u5199\u6cd5\uff0c\u4e5f\u5c31\u662f\u8868\u63a8\u5bfc\u7684\u65b9\u5f0f:<\/p>\n<div class=\"cnblogs_code\">\n<pre>L = [x**2 for x in range(10)]<\/pre>\n<\/div>\n<p>\u8fd9\u4e0e\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u7c7b\u4f3c\uff0c\u53ea\u4e0d\u8fc7\u7528\u7684\u662f\u4e2d\u62ec\u53f7\u3002<\/p>\n<p>\uff08\u8868\u63a8\u5bfc\u7684\u673a\u5236\u5b9e\u9645\u4e0a\u662f\u5229\u7528\u5faa\u73af\u5bf9\u8c61\uff0c\u6709\u5174\u8da3\u53ef\u4ee5\u67e5\u9605\u3002\uff09<\/p>\n<p>\u7ec3\u4e60\u00a0\u4e0b\u9762\u7684\u8868\u63a8\u5bfc\u4f1a\u751f\u6210\u4ec0\u4e48\uff1f<\/p>\n<div class=\"cnblogs_code\">\n<pre>xl = [1,3,5]\nyl = [9,12,13]\nL  = [ x**2 for (x,y) in zip(xl,yl) if y &gt; 10]<\/pre>\n<\/div>\n<h3>\u603b\u7ed3<\/h3>\n<p>\u5faa\u73af\u5bf9\u8c61<\/p>\n<p>\u751f\u6210\u5668<\/p>\n<p>\u8868\u63a8\u5bfc<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u4e00\u8bb2\u7684\u4e3b\u8981\u76ee\u7684\u662f\u4e3a\u4e86\u5927\u5bb6\u5728\u8bfbPython\u7a0b\u5e8f\u7684\u65f6\u5019\u5bf9\u5faa\u73af\u5bf9\u8c61\u6709\u4e00\u4e2a\u57fa\u672c\u6982\u5ff5\u3002 \u5faa\u73af\u5bf9\u8c61\u7684\u5e76\u4e0d\u662f\u968f\u7740Pytho &#8230; <a title=\"Python\u8fdb\u963606 \u5faa\u73af\u5bf9\u8c61\" class=\"read-more\" href=\"https:\/\/cn.hostease.com\/xueyuan\/jishu\/python\/python%e8%bf%9b%e9%98%b606-%e5%be%aa%e7%8e%af%e5%af%b9%e8%b1%a1\/\" aria-label=\"\u9605\u8bfb Python\u8fdb\u963606 \u5faa\u73af\u5bf9\u8c61\">\u9605\u8bfb\u66f4\u591a<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[654],"tags":[727],"class_list":["post-2473","post","type-post","status-publish","format-standard","hentry","category-python","tag-python"],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/posts\/2473","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/comments?post=2473"}],"version-history":[{"count":2,"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/posts\/2473\/revisions"}],"predecessor-version":[{"id":9193,"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/posts\/2473\/revisions\/9193"}],"wp:attachment":[{"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/media?parent=2473"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/categories?post=2473"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/tags?post=2473"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}