{"id":1161,"date":"2019-11-18T21:28:13","date_gmt":"2019-11-18T13:28:13","guid":{"rendered":"http:\/\/vip.fxxkpython.com\/?p=1161"},"modified":"2020-03-09T22:21:53","modified_gmt":"2020-03-09T14:21:53","slug":"python-%e7%88%ac%e9%98%b6%e6%a2%af-yield-%e6%98%af%e4%bb%80%e4%b9%88%e9%ad%94%e9%ac%bc%ef%bc%9fpython%e7%94%9f%e6%88%90%e5%99%a8%e5%8f%88%e6%98%af%e4%b8%aa%e5%95%a5%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/vip.fxxkpython.com\/?p=1161","title":{"rendered":"Python \u722c\u9636\u68af | yield \u662f\u4ec0\u4e48\u9b54\u9b3c\uff1fPython\u751f\u6210\u5668\u53c8\u662f\u4e2a\u5565\uff1f"},"content":{"rendered":"\n<p>\u6211\u4eec\u5728\u4e0a\u4e00\u7bc7\u4e2d\u77e5\u9053\u4e86\u5982\u4f55\u901a\u8fc7\u5217\u8868\u7684\u751f\u6210\u65b9\u5f0f\u53ef\u4ee5\u4e00\u884c\u4ee3\u7801\u5c31\u751f\u6210\u4e00\u4e2a\u5217\u8868\uff0c\u8fd9\u7bc7\u7ee7\u7eed\u8bb2\u5217\u8868\uff08\u5bb9\u5668\uff09\u76f8\u5173\u7684\u3002<\/p>\n\n\n\n<p>\u9996\u5148\u6211\u4eec\u6765\u770b\u4e0b\u8fd9\u4e24\u4e2a\u5217\u8868\u7684\u533a\u522b\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" width=\"734\" height=\"488\" src=\"https:\/\/vip.fxxkpython.com\/wp-content\/uploads\/2019\/11\/Screenshot-from-2019-11-18-20-05-25.png\" alt=\"\" class=\"wp-image-1162\" srcset=\"https:\/\/vip.fxxkpython.com\/wp-content\/uploads\/2019\/11\/Screenshot-from-2019-11-18-20-05-25.png 734w, https:\/\/vip.fxxkpython.com\/wp-content\/uploads\/2019\/11\/Screenshot-from-2019-11-18-20-05-25-300x199.png 300w\" sizes=\"(max-width: 734px) 100vw, 734px\" \/><\/figure><\/div>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230\uff0c list_1 \u662f  [i for i in range(20)]  \uff0c\u800c list_2 \u662f (i for i in range(20))\uff0c\u4e3b\u8981\u533a\u522b\u5c31\u662f\u4e00\u4e2a\u662f [] , \u4e00\u4e2a\u662f \uff08\uff09\uff0c\u800c\u540e\u8005\u5c31\u662f\u751f\u6210\u5668\u3002<\/p>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u5f53\u6211\u4eec\u53bb\u6267\u884c list_2 \u7684\u65f6\u5019\uff0c\u8f93\u51fa\u7ed3\u679c\u662f\u4e00\u4e2a generator\uff08\u751f\u6210\u5668\uff09\u5bf9\u8c61\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 next() \u53bb\u8c03\u7528\u8fd9\u4e2a\u751f\u6210\u5668\u91cc\u9762\u7684\u5143\u7d20\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" width=\"281\" height=\"411\" src=\"https:\/\/vip.fxxkpython.com\/wp-content\/uploads\/2019\/11\/Screenshot-from-2019-11-18-20-09-59.png\" alt=\"\" class=\"wp-image-1163\" srcset=\"https:\/\/vip.fxxkpython.com\/wp-content\/uploads\/2019\/11\/Screenshot-from-2019-11-18-20-09-59.png 281w, https:\/\/vip.fxxkpython.com\/wp-content\/uploads\/2019\/11\/Screenshot-from-2019-11-18-20-09-59-205x300.png 205w\" sizes=\"(max-width: 281px) 100vw, 281px\" \/><\/figure><\/div>\n\n\n\n<p>\u6211\u60f3\u5230\u8fd9\u91cc\u4f60\u4f1a\u6709\u70b9\u7591\u60d1\uff0c\u5c31\u662f\u5b83\u4eec\u4e4b\u95f4\u6709\u4ec0\u4e48\u533a\u522b\uff1f<\/p>\n\n\n\n<p>\u6211\u4eec\u8fd9\u6837\uff0c\u6765\u8ba1\u7b97\u4e00\u4e0b\u4ee5\u4e0b\u8fd9\u4e24\u4e2a\u5217\u8868\u751f\u6210\u6240\u9700\u8981\u7684\u65f6\u95f4\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">list_1 = [i for i in range(66666666)]<br><br>list_2 = (i for i in range(66666666))<br><br><\/pre>\n\n\n\n<p>\u6211\u4eec\u5206\u522b\u628a\u5b83\u4eec\u653e\u5230\u65b9\u6cd5\u91cc\u9762\u6267\u884c\uff0c\u7136\u540e\u7528<a href=\"https:\/\/vip.fxxkpython.com\/?p=1127#comment-761\">\u88c5\u9970\u5668<\/a>\u7684\u65b9\u5f0f\u5bf9\u5b83\u4eec\u5404\u81ea\u7684\u65b9\u6cd5\u6267\u884c\u65f6\u95f4\u505a\u4e2a\u8bb0\u5f55\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" width=\"859\" height=\"468\" src=\"https:\/\/vip.fxxkpython.com\/wp-content\/uploads\/2019\/11\/Screenshot-from-2019-11-18-20-22-27.png\" alt=\"\" class=\"wp-image-1164\" srcset=\"https:\/\/vip.fxxkpython.com\/wp-content\/uploads\/2019\/11\/Screenshot-from-2019-11-18-20-22-27.png 859w, https:\/\/vip.fxxkpython.com\/wp-content\/uploads\/2019\/11\/Screenshot-from-2019-11-18-20-22-27-300x163.png 300w, https:\/\/vip.fxxkpython.com\/wp-content\/uploads\/2019\/11\/Screenshot-from-2019-11-18-20-22-27-768x418.png 768w, https:\/\/vip.fxxkpython.com\/wp-content\/uploads\/2019\/11\/Screenshot-from-2019-11-18-20-22-27-769x419.png 769w\" sizes=\"(max-width: 859px) 100vw, 859px\" \/><\/figure><\/div>\n\n\n\n<p>\u6267\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" width=\"424\" height=\"69\" src=\"https:\/\/vip.fxxkpython.com\/wp-content\/uploads\/2019\/11\/Screenshot-from-2019-11-18-20-23-24.png\" alt=\"\" class=\"wp-image-1165\" srcset=\"https:\/\/vip.fxxkpython.com\/wp-content\/uploads\/2019\/11\/Screenshot-from-2019-11-18-20-23-24.png 424w, https:\/\/vip.fxxkpython.com\/wp-content\/uploads\/2019\/11\/Screenshot-from-2019-11-18-20-23-24-300x49.png 300w\" sizes=\"(max-width: 424px) 100vw, 424px\" \/><\/figure><\/div>\n\n\n\n<p>\u4f60\u4f1a\u53d1\u73b0\uff0c\u65b9\u6cd5\u4e8c\uff0c\u4e5f\u5c31\u662f\u751f\u6210\u5668\u751f\u6210\u7684\u5217\u8868 list_2 \u6240\u6d88\u8017\u7684\u65f6\u95f4\u8fdc\u4f4e\u4e8e list_1\u3002\u8fd9\u662f\u56e0\u4e3a<div style=\"text-align:center;border:1px dashed #FF9A9A;padding:8px;margin:10px auto;color:#FF6666;\">\u672c\u6587\u9690\u85cf\u5185\u5bb9 <a href=\"https:\/\/vip.fxxkpython.com\/wp-login.php?redirect_to=https%3A%2F%2Fvip.fxxkpython.com%2F%3Fp%3D1161\">\u767b\u9646<\/a> \u540e\u624d\u53ef\u4ee5\u6d4f\u89c8<\/div>\u53e6\u5916\uff0cyield \u540e\u9762\u53ef\u4ee5\u8ddf\u5e38\u7528\u7684\u6570\u636e\u7c7b\u578b\uff0c\u6bd4\u5982 string\uff0cint\uff0cdict\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" width=\"493\" height=\"90\" src=\"https:\/\/vip.fxxkpython.com\/wp-content\/uploads\/2019\/11\/Screenshot-from-2019-11-18-21-15-43.png\" alt=\"\" class=\"wp-image-1175\" srcset=\"https:\/\/vip.fxxkpython.com\/wp-content\/uploads\/2019\/11\/Screenshot-from-2019-11-18-21-15-43.png 493w, https:\/\/vip.fxxkpython.com\/wp-content\/uploads\/2019\/11\/Screenshot-from-2019-11-18-21-15-43-300x55.png 300w\" sizes=\"(max-width: 493px) 100vw, 493px\" \/><\/figure><\/div>\n\n\n\n<p>\u8fd9\u6837\u901a\u8fc7 yield \u5f97\u5230\u751f\u6210\u5668\uff0c\u5f53\u6211\u4eec\u9700\u8981\u7528\u5230\u6570\u636e\u7684\u65f6\u5019\uff0c\u76f4\u63a5\u5728\u91cc\u9762\u53d6\u51fa\u6765\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n\n\n\n<p>ok\uff0c\u4ee5\u4e0a\u5c31\u662f\u751f\u6210\u5668\u548c yield \u7684\u7528\u6cd5\uff0c\u4f60\u4ee5\u540e\u8fd8\u4f1a\u7ecf\u5e38\u770b\u5230\u5b83\u7684\u8eab\u5f71\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6211\u4eec\u5728\u4e0a\u4e00\u7bc7\u4e2d\u77e5\u9053\u4e86\u5982\u4f55\u901a\u8fc7\u5217\u8868\u7684\u751f\u6210\u65b9\u5f0f\u53ef\u4ee5\u4e00\u884c\u4ee3\u7801\u5c31\u751f\u6210\u4e00\u4e2a\u5217\u8868\uff0c\u8fd9\u7bc7\u7ee7\u7eed\u8bb2\u5217\u8868\uff08\u5bb9\u5668\uff09\u76f8\u5173\u7684\u3002 \u9996\u5148\u6211\u4eec\u6765\u770b\u4e0b\u8fd9\u4e24\u4e2a\u5217\u8868\u7684\u533a\u522b\uff1a \u53ef\u4ee5\u770b\u5230\uff0c list_1 \u662f [i for i in range(20)] \uff0c\u800c list_2 \u662f (i for &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[140],"tags":[164,166,141,165,52,163,168],"_links":{"self":[{"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=\/wp\/v2\/posts\/1161"}],"collection":[{"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1161"}],"version-history":[{"count":4,"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=\/wp\/v2\/posts\/1161\/revisions"}],"predecessor-version":[{"id":4135,"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=\/wp\/v2\/posts\/1161\/revisions\/4135"}],"wp:attachment":[{"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1161"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1161"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1161"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}