{"id":85259,"date":"2026-07-27T08:24:53","date_gmt":"2026-07-27T00:24:53","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/85259.html"},"modified":"2026-07-27T08:24:53","modified_gmt":"2026-07-27T00:24:53","slug":"python%e8%af%ad%e8%a8%80%e5%9f%ba%e7%a1%80%ef%bc%9a17_%e8%bf%ad%e4%bb%a3%e5%99%a8%e4%b8%8e%e7%94%9f%e6%88%90%e5%99%a8","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/85259.html","title":{"rendered":"Python\u8bed\u8a00\u57fa\u7840\uff1a17_\u8fed\u4ee3\u5668\u4e0e\u751f\u6210\u5668"},"content":{"rendered":"<p>\u8fd9\u4e24\u4e2a\u6982\u5ff5\u662f Python \u8fdb\u9636\u7684\u6838\u5fc3\u77e5\u8bc6\u70b9&#xff0c;\u4e5f\u662f\u65b0\u624b\u6700\u5bb9\u6613\u61f5\u7684\u5730\u65b9\u3002\u5168\u7a0b\u7528\u767d\u8bdd &#043; \u53ef\u8fd0\u884c\u793a\u4f8b&#xff0c;\u4ece\u6700\u57fa\u7840\u7684\u300c\u53ef\u8fed\u4ee3\u5bf9\u8c61\u300d\u8bb2\u8d77&#xff0c;\u4e00\u6b65\u6b65\u62c6\u89e3\u539f\u7406\u3001\u7528\u6cd5\u548c\u533a\u522b&#xff0c;\u4f60\u53ef\u4ee5\u628a\u4ee3\u7801\u590d\u5236\u5230 Python \u91cc\u8fb9\u8fd0\u884c\u8fb9\u7406\u89e3\u3002<\/p>\n<hr \/>\n<h3>\u4e00\u3001\u524d\u7f6e\u94fa\u57ab&#xff1a;\u5148\u641e\u61c2\u300c\u53ef\u8fed\u4ee3\u5bf9\u8c61\u300d<\/h3>\n<p>\u6211\u4eec\u5e73\u65f6\u7528 for \u5faa\u73af\u904d\u5386\u5217\u8868\u3001\u5b57\u7b26\u4e32\u3001\u5b57\u5178\u3001\u5143\u7ec4&#xff0c;\u8fd9\u4e9b\u80fd\u88ab for \u5faa\u73af\u9010\u4e2a\u53d6\u51fa\u5143\u7d20\u7684\u4e1c\u897f&#xff0c;\u90fd\u53eb\u300c\u53ef\u8fed\u4ee3\u5bf9\u8c61&#xff08;Iterable&#xff09;\u300d\u3002<\/p>\n<p>\u901a\u4fd7\u7406\u89e3&#xff1a;\u53ef\u8fed\u4ee3\u5bf9\u8c61\u5c31\u662f\u300c\u4e00\u6574\u76d2\u4e52\u4e53\u7403\u300d&#xff0c;\u91cc\u9762\u88c5\u4e86\u5f88\u591a\u5143\u7d20&#xff0c;\u4f60\u53ef\u4ee5\u6309\u987a\u5e8f\u4e00\u4e2a\u4e00\u4e2a\u62ff\u51fa\u6765\u7528\u3002<\/p>\n<h4>1. \u5e38\u89c1\u7684\u53ef\u8fed\u4ee3\u5bf9\u8c61<\/h4>\n<ul>\n<li>\u5e8f\u5217\u7c7b\u578b&#xff1a;\u5217\u8868 list\u3001\u5143\u7ec4 tuple\u3001\u5b57\u7b26\u4e32 str<\/li>\n<li>\u96c6\u5408\u7c7b\u578b&#xff1a;\u5b57\u5178 dict\u3001\u96c6\u5408 set<\/li>\n<li>\u7279\u6b8a\u7684&#xff1a;range() \u751f\u6210\u7684\u6570\u5b57\u5e8f\u5217\u3001\u6587\u4ef6\u5bf9\u8c61<\/li>\n<\/ul>\n<h4>2. \u600e\u4e48\u5224\u65ad\u662f\u4e0d\u662f\u53ef\u8fed\u4ee3\u5bf9\u8c61<\/h4>\n<p>\u53ef\u4ee5\u7528 collections.abc \u6a21\u5757\u91cc\u7684 Iterable \u6765\u5224\u65ad&#xff1a;<\/p>\n<p>from collections.abc import Iterable<\/p>\n<p># \u5217\u8868\u662f\u53ef\u8fed\u4ee3\u5bf9\u8c61<br \/>\nprint(isinstance([1,2,3], Iterable))  # True<br \/>\n# \u5b57\u7b26\u4e32\u662f\u53ef\u8fed\u4ee3\u5bf9\u8c61<br \/>\nprint(isinstance(&#034;hello&#034;, Iterable))  # True<br \/>\n# \u666e\u901a\u6570\u5b57\u4e0d\u662f<br \/>\nprint(isinstance(100, Iterable))      # False<\/p>\n<h4>3. \u53ef\u8fed\u4ee3\u5bf9\u8c61\u7684\u672c\u8d28<\/h4>\n<p>\u53ea\u8981\u4e00\u4e2a\u5bf9\u8c61\u5b9e\u73b0\u4e86 __iter__() \u65b9\u6cd5&#xff0c;\u5b83\u5c31\u662f\u53ef\u8fed\u4ee3\u5bf9\u8c61\u3002for \u5faa\u73af\u904d\u5386\u5b83\u7684\u65f6\u5019&#xff0c;\u4f1a\u5148\u8c03\u7528\u8fd9\u4e2a\u65b9\u6cd5&#xff0c;\u62ff\u5230\u4e00\u4e2a\u300c\u8fed\u4ee3\u5668\u300d&#xff0c;\u518d\u901a\u8fc7\u8fed\u4ee3\u5668\u9010\u4e2a\u53d6\u5143\u7d20\u3002<\/p>\n<hr \/>\n<h3>\u4e8c\u3001\u8fed\u4ee3\u5668&#xff08;Iterator&#xff09;<\/h3>\n<h4>1. \u4ec0\u4e48\u662f\u8fed\u4ee3\u5668&#xff1f;<\/h4>\n<p>\u8fed\u4ee3\u5668\u662f\u4e00\u4e2a\u53ef\u4ee5\u8bb0\u4f4f\u904d\u5386\u4f4d\u7f6e\u3001\u80fd\u4e00\u4e2a\u4e00\u4e2a\u5f80\u5916\u5410\u5143\u7d20\u7684\u5bf9\u8c61\u3002 \u901a\u4fd7\u7406\u89e3&#xff1a;\u53ef\u8fed\u4ee3\u5bf9\u8c61\u662f\u300c\u4e00\u6574\u76d2\u4e52\u4e53\u7403\u300d&#xff0c;\u8fed\u4ee3\u5668\u5c31\u662f\u300c\u5939\u4e52\u4e53\u7403\u7684\u5939\u5b50\u300d\u2014\u2014 \u5b83\u4f1a\u8bb0\u4f4f\u4f60\u5939\u5230\u7b2c\u51e0\u4e2a\u4e86&#xff0c;\u6bcf\u6b21\u8c03\u7528\u5c31\u5939\u51fa\u4e0b\u4e00\u4e2a&#xff0c;\u5939\u5b8c\u5c31\u6ca1\u6709\u4e86\u3002<\/p>\n<p>\u8fed\u4ee3\u5668\u5fc5\u987b\u6ee1\u8db3\u4e24\u4e2a\u6761\u4ef6&#xff1a;<\/p>\n<li>\u6709 __iter__() \u65b9\u6cd5&#xff1a;\u8fd4\u56de\u8fed\u4ee3\u5668\u81ea\u8eab<\/li>\n<li>\u6709 __next__() \u65b9\u6cd5&#xff1a;\u8fd4\u56de\u4e0b\u4e00\u4e2a\u5143\u7d20&#xff0c;\u6ca1\u6709\u5143\u7d20\u4e86\u5c31\u629b\u51fa StopIteration \u5f02\u5e38<\/li>\n<p>&#x1f4a1; \u7ed3\u8bba&#xff1a;\u8fed\u4ee3\u5668\u4e00\u5b9a\u662f\u53ef\u8fed\u4ee3\u5bf9\u8c61&#xff0c;\u4f46\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e0d\u4e00\u5b9a\u662f\u8fed\u4ee3\u5668\u3002\u6bd4\u5982\u5217\u8868\u662f\u53ef\u8fed\u4ee3\u5bf9\u8c61&#xff0c;\u4f46\u4e0d\u662f\u8fed\u4ee3\u5668\u3002<\/p>\n<hr \/>\n<h4>2. \u4e24\u4e2a\u6838\u5fc3\u5185\u7f6e\u51fd\u6570&#xff1a;iter () \u548c next ()<\/h4>\n<p>\u6211\u4eec\u4e0d\u7528\u76f4\u63a5\u8c03\u7528\u9b54\u6cd5\u65b9\u6cd5&#xff0c;Python \u63d0\u4f9b\u4e86\u4e24\u4e2a\u7b80\u5316\u51fd\u6570&#xff1a;<\/p>\n<ul>\n<li>iter(\u53ef\u8fed\u4ee3\u5bf9\u8c61)&#xff1a;\u628a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u8f6c\u6362\u6210\u8fed\u4ee3\u5668<\/li>\n<li>next(\u8fed\u4ee3\u5668)&#xff1a;\u53d6\u51fa\u8fed\u4ee3\u5668\u7684\u4e0b\u4e00\u4e2a\u5143\u7d20<\/li>\n<\/ul>\n<h5>\u5b8c\u6574\u793a\u4f8b&#xff1a;\u624b\u52a8\u904d\u5386\u8fed\u4ee3\u5668<\/h5>\n<p># 1. \u6709\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61&#xff1a;\u5217\u8868<br \/>\nlst &#061; [&#034;\u82f9\u679c&#034;, &#034;\u9999\u8549&#034;, &#034;\u6a59\u5b50&#034;]<\/p>\n<p># 2. \u7528 iter() \u628a\u5b83\u8f6c\u6210\u8fed\u4ee3\u5668<br \/>\nit &#061; iter(lst)<br \/>\nprint(type(it))  # &lt;class &#039;list_iterator&#039;&gt;<\/p>\n<p># 3. \u7528 next() \u4e00\u4e2a\u4e00\u4e2a\u53d6\u5143\u7d20<br \/>\nprint(next(it))  # \u82f9\u679c<br \/>\nprint(next(it))  # \u9999\u8549<br \/>\nprint(next(it))  # \u6a59\u5b50<\/p>\n<p># 4. \u5143\u7d20\u53d6\u5b8c\u4e86&#xff0c;\u518d\u8c03\u7528 next() \u5c31\u4f1a\u62a5\u9519<br \/>\nprint(next(it))  # \u629b\u51fa StopIteration \u5f02\u5e38<\/p>\n<p>\u8fd0\u884c\u5230\u6700\u540e\u4e00\u884c\u4f1a\u62a5\u9519&#xff1a;<\/p>\n<p>StopIteration<\/p>\n<p>\u8fd9\u662f\u6b63\u5e38\u73b0\u8c61 \u2014\u2014 \u8fed\u4ee3\u5668\u904d\u5386\u5b8c\u5c31\u7a7a\u4e86&#xff0c;\u6ca1\u6709\u4e0b\u4e00\u4e2a\u5143\u7d20\u4e86\u3002<\/p>\n<hr \/>\n<h4>3. for \u5faa\u73af\u7684\u5e95\u5c42\u672c\u8d28&#xff08;\u91cd\u70b9\u7406\u89e3&#xff09;<\/h4>\n<p>\u4f60\u5929\u5929\u5199\u7684 for \u5faa\u73af&#xff0c;\u672c\u8d28\u4e0a\u5c31\u662f\u81ea\u52a8\u5e2e\u4f60\u505a\u4e86\u8fd9\u4e09\u4ef6\u4e8b&#xff1a;<\/p>\n<li>\u8c03\u7528 iter(\u53ef\u8fed\u4ee3\u5bf9\u8c61) \u62ff\u5230\u8fed\u4ee3\u5668<\/li>\n<li>\u5faa\u73af\u8c03\u7528 next(\u8fed\u4ee3\u5668) \u53d6\u4e0b\u4e00\u4e2a\u5143\u7d20<\/li>\n<li>\u9047\u5230 StopIteration \u5f02\u5e38&#xff0c;\u81ea\u52a8\u7ed3\u675f\u5faa\u73af<\/li>\n<h5>\u6211\u4eec\u624b\u52a8\u6a21\u62df for \u5faa\u73af&#xff1a;<\/h5>\n<p>lst &#061; [10, 20, 30]<\/p>\n<p># \u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801&#xff0c;\u7b49\u4ef7\u4e8e for num in lst: print(num)<br \/>\nit &#061; iter(lst)<br \/>\nwhile True:<br \/>\n    try:<br \/>\n        num &#061; next(it)<br \/>\n        print(num)<br \/>\n    except StopIteration:<br \/>\n        # \u6ca1\u6709\u5143\u7d20\u4e86&#xff0c;\u8df3\u51fa\u5faa\u73af<br \/>\n        break<\/p>\n<p>\u8fd0\u884c\u7ed3\u679c\u548c\u666e\u901a for \u5faa\u73af\u5b8c\u5168\u4e00\u6837&#xff1a;<\/p>\n<p>10<br \/>\n20<br \/>\n30<\/p>\n<p>\u7406\u89e3\u4e86\u8fd9\u4e2a&#xff0c;\u4f60\u5c31\u5f7b\u5e95\u61c2\u4e86 for \u5faa\u73af\u7684\u5de5\u4f5c\u539f\u7406\u3002<\/p>\n<hr \/>\n<h4>4. \u8fed\u4ee3\u5668\u7684\u6838\u5fc3\u7279\u70b9<\/h4>\n<li>\u60f0\u6027\u8ba1\u7b97&#xff1a;\u4e0d\u4f1a\u4e00\u6b21\u6027\u628a\u6240\u6709\u5143\u7d20\u52a0\u8f7d\u5230\u5185\u5b58\u91cc&#xff0c;\u8981\u4e00\u4e2a\u624d\u751f\u6210\u4e00\u4e2a&#xff0c;\u975e\u5e38\u7701\u5185\u5b58<\/li>\n<li>\u53ea\u80fd\u5f80\u524d&#xff0c;\u4e0d\u80fd\u540e\u9000&#xff1a;\u53d6\u8fc7\u7684\u5143\u7d20\u5c31\u4e0d\u80fd\u518d\u53d6\u4e86&#xff0c;\u8bb0\u4f4f\u4f4d\u7f6e&#xff0c;\u5355\u5411\u904d\u5386<\/li>\n<li>\u53ea\u80fd\u904d\u5386\u4e00\u6b21&#xff1a;\u904d\u5386\u5b8c\u4e4b\u540e&#xff0c;\u8fed\u4ee3\u5668\u5c31\u7a7a\u4e86&#xff0c;\u518d\u53d6\u5c31\u62a5\u9519<\/li>\n<li>\u7701\u5185\u5b58&#xff1a;\u5904\u7406\u767e\u4e07\u7ea7\u3001\u5343\u4e07\u7ea7\u6570\u636e\u65f6&#xff0c;\u4f18\u52bf\u6781\u5176\u660e\u663e<\/li>\n<h5>\u9a8c\u8bc1&#xff1a;\u8fed\u4ee3\u5668\u53ea\u80fd\u904d\u5386\u4e00\u6b21<\/h5>\n<p>it &#061; iter([1,2,3])<br \/>\n# \u7b2c\u4e00\u6b21\u904d\u5386<br \/>\nprint(&#034;\u7b2c\u4e00\u6b21\u904d\u5386&#xff1a;&#034;)<br \/>\nfor i in it:<br \/>\n    print(i)<\/p>\n<p># \u7b2c\u4e8c\u6b21\u904d\u5386&#xff1a;\u4ec0\u4e48\u90fd\u6ca1\u6709&#xff0c;\u56e0\u4e3a\u5df2\u7ecf\u53d6\u5b8c\u4e86<br \/>\nprint(&#034;\u7b2c\u4e8c\u6b21\u904d\u5386&#xff1a;&#034;)<br \/>\nfor i in it:<br \/>\n    print(i)<\/p>\n<p>\u8f93\u51fa&#xff1a;<\/p>\n<p>\u7b2c\u4e00\u6b21\u904d\u5386&#xff1a;<br \/>\n1<br \/>\n2<br \/>\n3<br \/>\n\u7b2c\u4e8c\u6b21\u904d\u5386&#xff1a;<\/p>\n<p>\u7b2c\u4e8c\u6b21\u5faa\u73af\u4ec0\u4e48\u90fd\u6ca1\u6253\u5370&#xff0c;\u56e0\u4e3a\u8fed\u4ee3\u5668\u5df2\u7ecf\u7a7a\u4e86\u3002<\/p>\n<hr \/>\n<h3>\u4e09\u3001\u751f\u6210\u5668&#xff08;Generator&#xff09;\u2014\u2014 \u66f4\u7b80\u5355\u7684\u8fed\u4ee3\u5668<\/h3>\n<p>\u8fed\u4ee3\u5668\u867d\u7136\u597d\u7528&#xff0c;\u4f46\u81ea\u5df1\u5199\u4e00\u4e2a\u8fed\u4ee3\u5668\u8981\u5b9a\u4e49\u7c7b\u3001\u5b9e\u73b0 __iter__ \u548c __next__ \u65b9\u6cd5&#xff0c;\u975e\u5e38\u9ebb\u70e6\u3002 Python \u63d0\u4f9b\u4e86\u66f4\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u521b\u5efa\u8fed\u4ee3\u5668 \u2014\u2014\u751f\u6210\u5668\u3002<\/p>\n<p>\u6838\u5fc3\u7ed3\u8bba&#xff1a;\u751f\u6210\u5668\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u8fed\u4ee3\u5668&#xff0c;\u5b83\u81ea\u5e26\u8fed\u4ee3\u5668\u7684\u6240\u6709\u7279\u6027&#xff08;\u60f0\u6027\u3001\u7701\u5185\u5b58\u3001\u53ea\u80fd\u904d\u5386\u4e00\u6b21&#xff09;&#xff0c;\u4f46\u5199\u8d77\u6765\u6781\u5176\u7b80\u5355\u3002<\/p>\n<p>Python \u6709\u4e24\u79cd\u521b\u5efa\u751f\u6210\u5668\u7684\u65b9\u5f0f&#xff1a;<\/p>\n<li>\u751f\u6210\u5668\u8868\u8fbe\u5f0f&#xff1a;\u7528\u5c0f\u62ec\u53f7&#xff0c;\u5199\u6cd5\u7c7b\u4f3c\u63a8\u5bfc\u5f0f<\/li>\n<li>\u751f\u6210\u5668\u51fd\u6570&#xff1a;\u7528 def &#043; yield \u5173\u952e\u5b57&#xff08;\u6700\u5e38\u7528\u3001\u6700\u5f3a\u5927&#xff09;<\/li>\n<hr \/>\n<h4>\u65b9\u5f0f 1&#xff1a;\u751f\u6210\u5668\u8868\u8fbe\u5f0f<\/h4>\n<p>\u5199\u6cd5\u548c\u5217\u8868\u63a8\u5bfc\u5f0f\u51e0\u4e4e\u4e00\u6a21\u4e00\u6837&#xff0c;\u53ea\u662f\u628a\u65b9\u62ec\u53f7 [] \u6362\u6210\u5c0f\u62ec\u53f7 ()\u3002<\/p>\n<h5>\u8bed\u6cd5\u683c\u5f0f<\/h5>\n<p>(\u8868\u8fbe\u5f0f for \u53d8\u91cf in \u53ef\u8fed\u4ee3\u5bf9\u8c61 [if \u6761\u4ef6])<\/p>\n<h5>\u5bf9\u6bd4&#xff1a;\u5217\u8868\u63a8\u5bfc\u5f0f vs \u751f\u6210\u5668\u8868\u8fbe\u5f0f<\/h5>\n<p># \u5217\u8868\u63a8\u5bfc\u5f0f&#xff1a;\u4e00\u6b21\u6027\u751f\u6210\u6240\u6709\u5143\u7d20&#xff0c;\u5168\u90e8\u653e\u8fdb\u5185\u5b58<br \/>\nlst &#061; [i**2 for i in range(5)]<br \/>\nprint(lst)        # [0, 1, 4, 9, 16]<br \/>\nprint(type(lst))  # &lt;class &#039;list&#039;&gt;<\/p>\n<p># \u751f\u6210\u5668\u8868\u8fbe\u5f0f&#xff1a;\u4e0d\u751f\u6210\u5143\u7d20&#xff0c;\u53ea\u4fdd\u5b58\u751f\u6210\u89c4\u5219<br \/>\ngen &#061; (i**2 for i in range(5))<br \/>\nprint(gen)        # &lt;generator object &lt;genexpr&gt; at 0x&#8230;&gt;<br \/>\nprint(type(gen))  # &lt;class &#039;generator&#039;&gt;<\/p>\n<p>\u5217\u8868\u6253\u5370\u51fa\u6765\u662f\u5b8c\u6574\u7684\u5143\u7d20\u5217\u8868&#xff0c;\u751f\u6210\u5668\u6253\u5370\u51fa\u6765\u662f\u4e00\u4e2a\u5bf9\u8c61 \u2014\u2014 \u5b83\u6ca1\u6709\u628a\u6240\u6709\u5143\u7d20\u7b97\u597d\u5b58\u8d77\u6765&#xff0c;\u53ea\u4fdd\u5b58\u4e86\u751f\u6210\u89c4\u5219&#xff0c;\u4f60\u8981\u4e00\u4e2a\u5b83\u624d\u7b97\u4e00\u4e2a\u3002<\/p>\n<h5>\u751f\u6210\u5668\u7684\u4f7f\u7528<\/h5>\n<p>\u548c\u8fed\u4ee3\u5668\u4e00\u6837&#xff0c;\u7528 next() \u9010\u4e2a\u53d6&#xff0c;\u6216\u8005\u7528 for \u5faa\u73af\u904d\u5386&#xff1a;<\/p>\n<p>gen &#061; (i**2 for i in range(3))<\/p>\n<p># \u624b\u52a8\u53d6<br \/>\nprint(next(gen))  # 0<br \/>\nprint(next(gen))  # 1<br \/>\nprint(next(gen))  # 4<\/p>\n<p># \u66f4\u5e38\u7528\u7684\u662f\u76f4\u63a5 for \u904d\u5386<br \/>\ngen2 &#061; (i*2 for i in range(5))<br \/>\nfor num in gen2:<br \/>\n    print(num)<\/p>\n<h5>\u6838\u5fc3\u4f18\u52bf&#xff1a;\u6781\u81f4\u8282\u7701\u5185\u5b58<\/h5>\n<p>\u5982\u679c\u8981\u751f\u6210 1000 \u4e07\u4e2a\u6570\u5b57&#xff0c;\u5217\u8868\u4f1a\u4e00\u6b21\u6027\u628a 1000 \u4e07\u4e2a\u6570\u5b57\u5168\u90e8\u653e\u8fdb\u5185\u5b58&#xff0c;\u53ef\u80fd\u76f4\u63a5\u5360\u51e0\u767e MB&#xff1b;\u800c\u751f\u6210\u5668\u51e0\u4e4e\u4e0d\u5360\u5185\u5b58&#xff0c;\u56e0\u4e3a\u5b83\u6839\u672c\u6ca1\u751f\u6210\u5143\u7d20&#xff0c;\u53ea\u4fdd\u5b58\u4e86\u89c4\u5219\u3002<\/p>\n<p># \u51e0\u4e4e\u4e0d\u5360\u5185\u5b58<br \/>\ngen &#061; (i for i in range(10000000))<br \/>\n# \u5360\u5927\u91cf\u5185\u5b58<br \/>\nlst &#061; [i for i in range(10000000)]<\/p>\n<p>\u5904\u7406\u8d85\u5927\u6587\u4ef6\u3001\u6d77\u91cf\u6570\u636e\u65f6&#xff0c;\u751f\u6210\u5668\u662f\u5fc5\u5907\u5de5\u5177\u3002<\/p>\n<hr \/>\n<h4>\u65b9\u5f0f 2&#xff1a;\u751f\u6210\u5668\u51fd\u6570&#xff08;yield \u5173\u952e\u5b57&#xff0c;\u91cd\u70b9 &#043; \u96be\u70b9&#xff09;<\/h4>\n<p>\u666e\u901a\u51fd\u6570\u7528 return \u8fd4\u56de\u503c&#xff0c;\u8fd4\u56de\u4e00\u6b21\u51fd\u6570\u5c31\u7ed3\u675f\u4e86&#xff1b; \u751f\u6210\u5668\u51fd\u6570\u7528 yield \u8fd4\u56de\u503c&#xff0c;\u8fd4\u56de\u4e4b\u540e\u51fd\u6570\u4f1a\u6682\u505c\u6267\u884c&#xff0c;\u4e0b\u6b21\u8c03\u7528\u518d\u4ece\u6682\u505c\u7684\u5730\u65b9\u7ee7\u7eed\u5f80\u4e0b\u8d70\u3002<\/p>\n<p>\u8fd9\u662f\u751f\u6210\u5668\u6700\u6838\u5fc3\u7684\u7279\u6027&#xff0c;\u4e5f\u662f\u65b0\u624b\u6700\u96be\u7406\u89e3\u7684\u5730\u65b9&#xff0c;\u6211\u4eec\u4e00\u6b65\u6b65\u62c6\u89e3\u3002<\/p>\n<h5>1. \u57fa\u7840\u8bed\u6cd5<\/h5>\n<p>def \u751f\u6210\u5668\u51fd\u6570\u540d():<br \/>\n    &#8230;<br \/>\n    yield \u503c<br \/>\n    &#8230;<br \/>\n    yield \u503c<\/p>\n<ul>\n<li>\u51fd\u6570\u91cc\u53ea\u8981\u6709 yield \u5173\u952e\u5b57&#xff0c;\u5b83\u5c31\u4e0d\u518d\u662f\u666e\u901a\u51fd\u6570&#xff0c;\u800c\u662f\u751f\u6210\u5668\u51fd\u6570<\/li>\n<li>\u8c03\u7528\u751f\u6210\u5668\u51fd\u6570&#xff0c;\u4e0d\u4f1a\u6267\u884c\u51fd\u6570\u4ee3\u7801&#xff0c;\u53ea\u4f1a\u8fd4\u56de\u4e00\u4e2a\u751f\u6210\u5668\u5bf9\u8c61<\/li>\n<li>\u5fc5\u987b\u8c03\u7528 next() \u624d\u4f1a\u5f00\u59cb\u6267\u884c\u4ee3\u7801&#xff0c;\u9047\u5230 yield \u5c31\u6682\u505c&#xff0c;\u8fd4\u56de\u503c<\/li>\n<\/ul>\n<h5>2. \u6700\u7b80\u5355\u7684\u751f\u6210\u5668\u51fd\u6570 &#043; \u9010\u884c\u6267\u884c\u8fc7\u7a0b<\/h5>\n<p>\u6211\u4eec\u5199\u4e00\u4e2a\u751f\u6210 1\u30012\u30013 \u7684\u751f\u6210\u5668&#xff0c;\u4e00\u6b65\u6b65\u770b\u5b83\u662f\u600e\u4e48\u8fd0\u884c\u7684&#xff1a;<\/p>\n<p>def my_generator():<br \/>\n    print(&#034;&#8212; \u5f00\u59cb\u6267\u884c\u7b2c1\u6bb5\u4ee3\u7801 &#8212;&#034;)<br \/>\n    yield 1  # \u7b2c1\u4e2a\u6682\u505c\u70b9<\/p>\n<p>    print(&#034;&#8212; \u7ee7\u7eed\u6267\u884c\u7b2c2\u6bb5\u4ee3\u7801 &#8212;&#034;)<br \/>\n    yield 2  # \u7b2c2\u4e2a\u6682\u505c\u70b9<\/p>\n<p>    print(&#034;&#8212; \u7ee7\u7eed\u6267\u884c\u7b2c3\u6bb5\u4ee3\u7801 &#8212;&#034;)<br \/>\n    yield 3  # \u7b2c3\u4e2a\u6682\u505c\u70b9<\/p>\n<p># \u8c03\u7528\u51fd\u6570&#xff1a;\u4e0d\u4f1a\u6267\u884c\u4efb\u4f55\u4ee3\u7801&#xff0c;\u53ea\u8fd4\u56de\u751f\u6210\u5668\u5bf9\u8c61<br \/>\ngen &#061; my_generator()<br \/>\nprint(gen)  # &lt;generator object my_generator at 0x&#8230;&gt;<\/p>\n<p>\u73b0\u5728\u6211\u4eec\u8c03\u7528\u4e09\u6b21 next()&#xff0c;\u4e00\u6b65\u6b65\u770b\u6267\u884c\u8fc7\u7a0b&#xff1a;<\/p>\n<h6>\u7b2c\u4e00\u6b21\u8c03\u7528 next (gen)<\/h6>\n<ul>\n<li>\u51fd\u6570\u5f00\u59cb\u6267\u884c&#xff0c;\u6253\u5370 &#8212; \u5f00\u59cb\u6267\u884c\u7b2c1\u6bb5\u4ee3\u7801 &#8212;<\/li>\n<li>\u9047\u5230\u7b2c\u4e00\u4e2a yield 1&#xff0c;\u6682\u505c\u51fd\u6570&#xff0c;\u628a 1 \u8fd4\u56de\u51fa\u53bb<\/li>\n<\/ul>\n<p>print(next(gen))<br \/>\n# \u8f93\u51fa&#xff1a;<br \/>\n# &#8212; \u5f00\u59cb\u6267\u884c\u7b2c1\u6bb5\u4ee3\u7801 &#8212;<br \/>\n# 1<\/p>\n<h6>\u7b2c\u4e8c\u6b21\u8c03\u7528 next (gen)<\/h6>\n<ul>\n<li>\u4ece\u4e0a\u6b21\u6682\u505c\u7684\u5730\u65b9&#xff08;\u7b2c\u4e00\u4e2a yield \u540e\u9762&#xff09;\u7ee7\u7eed\u5f80\u4e0b\u6267\u884c<\/li>\n<li>\u6253\u5370 &#8212; \u7ee7\u7eed\u6267\u884c\u7b2c2\u6bb5\u4ee3\u7801 &#8212;<\/li>\n<li>\u9047\u5230\u7b2c\u4e8c\u4e2a yield 2&#xff0c;\u518d\u6b21\u6682\u505c&#xff0c;\u8fd4\u56de 2<\/li>\n<\/ul>\n<p>print(next(gen))<br \/>\n# \u8f93\u51fa&#xff1a;<br \/>\n# &#8212; \u7ee7\u7eed\u6267\u884c\u7b2c2\u6bb5\u4ee3\u7801 &#8212;<br \/>\n# 2<\/p>\n<h6>\u7b2c\u4e09\u6b21\u8c03\u7528 next (gen)<\/h6>\n<ul>\n<li>\u4ece\u7b2c\u4e8c\u4e2a yield \u540e\u9762\u7ee7\u7eed\u6267\u884c<\/li>\n<li>\u6253\u5370 &#8212; \u7ee7\u7eed\u6267\u884c\u7b2c3\u6bb5\u4ee3\u7801 &#8212;<\/li>\n<li>\u9047\u5230\u7b2c\u4e09\u4e2a yield 3&#xff0c;\u6682\u505c&#xff0c;\u8fd4\u56de 3<\/li>\n<\/ul>\n<p>print(next(gen))<br \/>\n# \u8f93\u51fa&#xff1a;<br \/>\n# &#8212; \u7ee7\u7eed\u6267\u884c\u7b2c3\u6bb5\u4ee3\u7801 &#8212;<br \/>\n# 3<\/p>\n<h6>\u7b2c\u56db\u6b21\u8c03\u7528 next (gen)<\/h6>\n<ul>\n<li>\u4ece\u7b2c\u4e09\u4e2a yield \u540e\u9762\u7ee7\u7eed\u6267\u884c&#xff0c;\u51fd\u6570\u91cc\u6ca1\u6709\u4ee3\u7801\u4e86<\/li>\n<li>\u629b\u51fa StopIteration \u5f02\u5e38<\/li>\n<\/ul>\n<p>print(next(gen))  # StopIteration<\/p>\n<p>&#x1f511; \u6838\u5fc3\u7ed3\u8bba&#xff1a;yield \u7684\u4f5c\u7528\u662f\u300c\u8fd4\u56de\u503c &#043; \u6682\u505c\u51fd\u6570\u300d&#xff0c;\u4e0b\u6b21 next() \u4ece\u6682\u505c\u4f4d\u7f6e\u7ee7\u7eed\u6267\u884c\u3002\u8fd9\u662f\u5b83\u548c return \u6700\u672c\u8d28\u7684\u533a\u522b\u3002<\/p>\n<hr \/>\n<h5>3. yield vs return \u6838\u5fc3\u533a\u522b<\/h5>\n<table>\n<tr>\u5bf9\u6bd4\u9879returnyield<\/tr>\n<tbody>\n<tr>\n<td>\u4f5c\u7528<\/td>\n<td>\u7ed3\u675f\u6574\u4e2a\u51fd\u6570&#xff0c;\u8fd4\u56de\u503c<\/td>\n<td>\u6682\u505c\u51fd\u6570&#xff0c;\u8fd4\u56de\u503c&#xff0c;\u4e0b\u6b21\u7ee7\u7eed<\/td>\n<\/tr>\n<tr>\n<td>\u6b21\u6570<\/td>\n<td>\u4e00\u4e2a\u51fd\u6570\u53ea\u80fd\u6267\u884c\u4e00\u6b21 return<\/td>\n<td>\u53ef\u4ee5\u6709\u591a\u4e2a yield&#xff0c;\u591a\u6b21\u6682\u505c\u8fd4\u56de<\/td>\n<\/tr>\n<tr>\n<td>\u51fd\u6570\u72b6\u6001<\/td>\n<td>\u6267\u884c\u5b8c\u5c31\u9500\u6bc1\u6240\u6709\u5c40\u90e8\u53d8\u91cf<\/td>\n<td>\u6682\u505c\u65f6\u4fdd\u7559\u6240\u6709\u5c40\u90e8\u53d8\u91cf\u7684\u72b6\u6001<\/td>\n<\/tr>\n<tr>\n<td>\u6240\u5c5e<\/td>\n<td>\u666e\u901a\u51fd\u6570<\/td>\n<td>\u751f\u6210\u5668\u51fd\u6570<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr \/>\n<h5>4. \u7ecf\u5178\u5b9e\u6218\u4f8b\u5b50&#xff1a;\u751f\u6210\u6590\u6ce2\u90a3\u5951\u6570\u5217<\/h5>\n<p>\u6590\u6ce2\u90a3\u5951\u6570\u5217&#xff1a;1, 1, 2, 3, 5, 8, 13&#8230; \u540e\u4e00\u4e2a\u6570\u7b49\u4e8e\u524d\u4e24\u4e2a\u6570\u4e4b\u548c\u3002<\/p>\n<p>\u5982\u679c\u7528\u666e\u901a\u5217\u8868\u5b9e\u73b0&#xff0c;\u751f\u6210\u524d N \u4e2a\u6570&#xff0c;\u4f1a\u628a\u6240\u6709\u6570\u90fd\u5b58\u5728\u5185\u5b58\u91cc&#xff0c;N \u5f88\u5927\u65f6\u975e\u5e38\u5360\u5185\u5b58&#xff1a;<\/p>\n<p>def fib_list(n):<br \/>\n    a, b &#061; 0, 1<br \/>\n    res &#061; []<br \/>\n    for _ in range(n):<br \/>\n        res.append(b)<br \/>\n        a, b &#061; b, a&#043;b<br \/>\n    return res<\/p>\n<p>print(fib_list(10))<br \/>\n# [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]<\/p>\n<p>\u7528\u751f\u6210\u5668\u5b9e\u73b0&#xff0c;\u60f0\u6027\u8ba1\u7b97&#xff0c;\u8981\u4e00\u4e2a\u7b97\u4e00\u4e2a&#xff0c;\u51e0\u4e4e\u4e0d\u5360\u5185\u5b58&#xff1a;<\/p>\n<p>def fib_gen(n):<br \/>\n    a, b &#061; 0, 1<br \/>\n    for _ in range(n):<br \/>\n        yield b  # \u9047\u5230yield\u5c31\u8fd4\u56de\u5f53\u524d\u503c&#xff0c;\u6682\u505c<br \/>\n        a, b &#061; b, a &#043; b<\/p>\n<p># \u904d\u5386\u751f\u6210\u5668<br \/>\nfor num in fib_gen(10):<br \/>\n    print(num, end&#061;&#034; &#034;)<br \/>\n# \u8f93\u51fa&#xff1a;1 1 2 3 5 8 13 21 34 55<\/p>\n<p>\u54ea\u6015\u751f\u6210 100 \u4e07\u4e2a\u6590\u6ce2\u90a3\u5951\u6570&#xff0c;\u751f\u6210\u5668\u7248\u672c\u7684\u5185\u5b58\u5360\u7528\u4e5f\u51e0\u4e4e\u4e0d\u53d8&#xff0c;\u8fd9\u5c31\u662f\u5b83\u6700\u5927\u7684\u4ef7\u503c\u3002<\/p>\n<hr \/>\n<h5>5. \u751f\u6210\u5668\u7684\u8fdb\u9636\u65b9\u6cd5&#xff08;\u4e86\u89e3\u5373\u53ef&#xff09;<\/h5>\n<p>\u751f\u6210\u5668\u9664\u4e86 next()&#xff0c;\u8fd8\u6709\u4e24\u4e2a\u5e38\u7528\u65b9\u6cd5&#xff1a;<\/p>\n<ul>\n<li>send(\u503c)&#xff1a;\u7ed9\u751f\u6210\u5668\u5185\u90e8\u4f20\u503c&#xff0c;\u540c\u65f6\u5524\u9192\u6682\u505c\u7684\u751f\u6210\u5668<\/li>\n<li>close()&#xff1a;\u624b\u52a8\u5173\u95ed\u751f\u6210\u5668<\/li>\n<\/ul>\n<h6>send () \u7b80\u5355\u793a\u4f8b<\/h6>\n<p>def gen():<br \/>\n    print(&#034;\u542f\u52a8&#034;)<br \/>\n    x &#061; yield 1<br \/>\n    print(f&#034;\u6536\u5230\u4e86&#xff1a;{x}&#034;)<br \/>\n    y &#061; yield 2<br \/>\n    print(f&#034;\u6536\u5230\u4e86&#xff1a;{y}&#034;)<\/p>\n<p>g &#061; gen()<br \/>\n# \u7b2c\u4e00\u6b21\u5fc5\u987b\u7528 next() \u542f\u52a8&#xff0c;\u6216\u8005 send(None)<br \/>\nprint(next(g))  # \u542f\u52a8&#xff0c;\u6253\u5370&#034;\u542f\u52a8&#034;&#xff0c;\u8fd4\u56de1<\/p>\n<p># send\u4f20\u503c\u8fdb\u53bb&#xff0c;\u8d4b\u503c\u7ed9x&#xff0c;\u7ee7\u7eed\u6267\u884c\u5230\u4e0b\u4e00\u4e2ayield<br \/>\nprint(g.send(&#034;hello&#034;))  # \u6253\u5370&#034;\u6536\u5230\u4e86&#xff1a;hello&#034;&#xff0c;\u8fd4\u56de2<\/p>\n<p>print(g.send(&#034;world&#034;))  # \u6253\u5370&#034;\u6536\u5230\u4e86&#xff1a;world&#034;&#xff0c;\u6ca1\u6709yield\u4e86&#xff0c;\u629b\u5f02\u5e38<\/p>\n<p>&#x1f4a1; \u65b0\u624b\u63d0\u793a&#xff1a;\u524d\u671f\u5148\u638c\u63e1 next() \u548c for \u904d\u5386\u5c31\u884c&#xff0c;send() \u5c5e\u4e8e\u8fdb\u9636\u7528\u6cd5&#xff0c;\u540e\u671f\u518d\u6df1\u5165\u3002<\/p>\n<hr \/>\n<h3>\u56db\u3001\u4e09\u8005\u5173\u7cfb\u603b\u7ed3<\/h3>\n<p>\u53ef\u8fed\u4ee3\u5bf9\u8c61&#xff08;\u8303\u56f4\u6700\u5927&#xff09;<br \/>\n    \u2514\u2500\u2500 \u8fed\u4ee3\u5668<br \/>\n          \u2514\u2500\u2500 \u751f\u6210\u5668&#xff08;\u7279\u6b8a\u7684\u8fed\u4ee3\u5668&#xff09;<\/p>\n<li>\u53ef\u8fed\u4ee3\u5bf9\u8c61&#xff1a;\u5b9e\u73b0\u4e86 __iter__ \u65b9\u6cd5&#xff0c;\u80fd\u88ab for \u904d\u5386<\/li>\n<li>\u8fed\u4ee3\u5668&#xff1a;\u540c\u65f6\u5b9e\u73b0\u4e86 __iter__ \u548c __next__ \u65b9\u6cd5&#xff0c;\u60f0\u6027\u53d6\u503c&#xff0c;\u53ea\u80fd\u904d\u5386\u4e00\u6b21<\/li>\n<li>\u751f\u6210\u5668&#xff1a;\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u6216 yield \u51fd\u6570\u521b\u5efa\u7684\u8fed\u4ee3\u5668&#xff0c;\u5199\u6cd5\u66f4\u7b80\u6d01<\/li>\n<hr \/>\n<h3>\u4e94\u3001\u65b0\u624b\u9ad8\u9891\u6613\u9519\u70b9\u603b\u7ed3<\/h3>\n<li>\u53ef\u8fed\u4ee3\u5bf9\u8c61 \u2260 \u8fed\u4ee3\u5668 \u5217\u8868\u3001\u5b57\u7b26\u4e32\u662f\u53ef\u8fed\u4ee3\u5bf9\u8c61&#xff0c;\u4f46\u4e0d\u662f\u8fed\u4ee3\u5668&#xff1b;\u7528 iter() \u8f6c\u6362\u540e\u624d\u662f\u8fed\u4ee3\u5668\u3002<\/li>\n<li>\u8fed\u4ee3\u5668 \/ \u751f\u6210\u5668\u53ea\u80fd\u904d\u5386\u4e00\u6b21 \u904d\u5386\u5b8c\u5c31\u7a7a\u4e86&#xff0c;\u60f3\u518d\u6b21\u904d\u5386\u5fc5\u987b\u91cd\u65b0\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u8fed\u4ee3\u5668 \/ \u751f\u6210\u5668\u3002<\/li>\n<li>\u8c03\u7528\u751f\u6210\u5668\u51fd\u6570\u4e0d\u4f1a\u6267\u884c\u4ee3\u7801 \u53ea\u662f\u8fd4\u56de\u751f\u6210\u5668\u5bf9\u8c61&#xff0c;\u5fc5\u987b\u8c03\u7528 next() \u624d\u4f1a\u771f\u6b63\u5f00\u59cb\u6267\u884c\u51fd\u6570\u4ee3\u7801\u3002<\/li>\n<li>yield \u548c return \u641e\u6df7 return \u662f\u7ed3\u675f\u51fd\u6570&#xff1b;yield \u662f\u6682\u505c\u51fd\u6570&#xff0c;\u4e0b\u6b21\u7ee7\u7eed\u3002\u4e00\u4e2a\u751f\u6210\u5668\u53ef\u4ee5\u6709\u591a\u4e2a yield\u3002<\/li>\n<li>\u5c0f\u62ec\u53f7\u4e0d\u662f\u5143\u7ec4\u63a8\u5bfc\u5f0f(i for i in range(5)) \u662f\u751f\u6210\u5668\u8868\u8fbe\u5f0f&#xff0c;\u4e0d\u662f\u5143\u7ec4\u63a8\u5bfc\u5f0f&#xff0c;Python \u6ca1\u6709\u5143\u7ec4\u63a8\u5bfc\u5f0f\u3002<\/li>\n<li>\u4ec0\u4e48\u65f6\u5019\u7528\u751f\u6210\u5668&#xff1f;\n<ul>\n<li>\u6570\u636e\u91cf\u7279\u522b\u5927&#xff0c;\u4e0d\u60f3\u4e00\u6b21\u6027\u5360\u6ee1\u5185\u5b58<\/li>\n<li>\u4e0d\u9700\u8981\u4e00\u6b21\u6027\u62ff\u5230\u6240\u6709\u6570\u636e&#xff0c;\u7528\u4e00\u4e2a\u53d6\u4e00\u4e2a<\/li>\n<li>\u6570\u636e\u6d41\u5904\u7406\u3001\u5927\u6587\u4ef6\u8bfb\u53d6\u3001\u65e0\u9650\u5e8f\u5217\u751f\u6210<\/li>\n<\/ul>\n<\/li>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u4e24\u4e2a\u6982\u5ff5\u662f Python \u8fdb\u9636\u7684\u6838\u5fc3\u77e5\u8bc6\u70b9&#xff0c;\u4e5f\u662f\u65b0\u624b\u6700\u5bb9\u6613\u61f5\u7684\u5730\u65b9\u3002\u5168\u7a0b\u7528\u767d\u8bdd  \u53ef\u8fd0\u884c\u793a\u4f8b&#xff0c;\u4ece\u6700\u57fa\u7840\u7684\u300c\u53ef\u8fed\u4ee3\u5bf9\u8c61\u300d\u8bb2\u8d77&#xff0c;\u4e00\u6b65\u6b65\u62c6\u89e3\u539f\u7406\u3001\u7528\u6cd5\u548c\u533a\u522b&#xff0c;\u4f60\u53ef\u4ee5\u628a\u4ee3\u7801\u590d\u5236\u5230 Python \u91cc\u8fb9\u8fd0\u884c\u8fb9\u7406\u89e3\u3002\u4e00\u3001\u524d\u7f6e\u94fa\u57ab&#xff1a;\u5148\u641e\u61c2\u300c\u53ef\u8fed\u4ee3\u5bf9\u8c61\u300d\u6211\u4eec\u5e73\u65f6\u7528 for \u5faa\u73af\u904d\u5386\u5217\u8868\u3001\u5b57\u7b26\u4e32\u3001\u5b57\u5178\u3001\u5143\u7ec4&#xff0c;\u8fd9\u4e9b\u80fd\u88ab for \u5faa\u73af\u9010\u4e2a\u53d6\u51fa\u5143\u7d20\u7684\u4e1c\u897f&#xff0c;\u90fd\u53eb\u300c\u53ef\u8fed\u4ee3\u5bf9\u8c61&#xff08;Iterable<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[81,190],"topic":[],"class_list":["post-85259","post","type-post","status-publish","format-standard","hentry","category-server","tag-python","tag-190"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python\u8bed\u8a00\u57fa\u7840\uff1a17_\u8fed\u4ee3\u5668\u4e0e\u751f\u6210\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/85259.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python\u8bed\u8a00\u57fa\u7840\uff1a17_\u8fed\u4ee3\u5668\u4e0e\u751f\u6210\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u8fd9\u4e24\u4e2a\u6982\u5ff5\u662f Python \u8fdb\u9636\u7684\u6838\u5fc3\u77e5\u8bc6\u70b9&#xff0c;\u4e5f\u662f\u65b0\u624b\u6700\u5bb9\u6613\u61f5\u7684\u5730\u65b9\u3002\u5168\u7a0b\u7528\u767d\u8bdd \u53ef\u8fd0\u884c\u793a\u4f8b&#xff0c;\u4ece\u6700\u57fa\u7840\u7684\u300c\u53ef\u8fed\u4ee3\u5bf9\u8c61\u300d\u8bb2\u8d77&#xff0c;\u4e00\u6b65\u6b65\u62c6\u89e3\u539f\u7406\u3001\u7528\u6cd5\u548c\u533a\u522b&#xff0c;\u4f60\u53ef\u4ee5\u628a\u4ee3\u7801\u590d\u5236\u5230 Python \u91cc\u8fb9\u8fd0\u884c\u8fb9\u7406\u89e3\u3002\u4e00\u3001\u524d\u7f6e\u94fa\u57ab&#xff1a;\u5148\u641e\u61c2\u300c\u53ef\u8fed\u4ee3\u5bf9\u8c61\u300d\u6211\u4eec\u5e73\u65f6\u7528 for \u5faa\u73af\u904d\u5386\u5217\u8868\u3001\u5b57\u7b26\u4e32\u3001\u5b57\u5178\u3001\u5143\u7ec4&#xff0c;\u8fd9\u4e9b\u80fd\u88ab for \u5faa\u73af\u9010\u4e2a\u53d6\u51fa\u5143\u7d20\u7684\u4e1c\u897f&#xff0c;\u90fd\u53eb\u300c\u53ef\u8fed\u4ee3\u5bf9\u8c61&#xff08;Iterable\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/85259.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-07-27T00:24:53+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/85259.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/85259.html\",\"name\":\"Python\u8bed\u8a00\u57fa\u7840\uff1a17_\u8fed\u4ee3\u5668\u4e0e\u751f\u6210\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-07-27T00:24:53+00:00\",\"dateModified\":\"2026-07-27T00:24:53+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/85259.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/85259.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/85259.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python\u8bed\u8a00\u57fa\u7840\uff1a17_\u8fed\u4ee3\u5668\u4e0e\u751f\u6210\u5668\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python\u8bed\u8a00\u57fa\u7840\uff1a17_\u8fed\u4ee3\u5668\u4e0e\u751f\u6210\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/85259.html","og_locale":"zh_CN","og_type":"article","og_title":"Python\u8bed\u8a00\u57fa\u7840\uff1a17_\u8fed\u4ee3\u5668\u4e0e\u751f\u6210\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u8fd9\u4e24\u4e2a\u6982\u5ff5\u662f Python \u8fdb\u9636\u7684\u6838\u5fc3\u77e5\u8bc6\u70b9&#xff0c;\u4e5f\u662f\u65b0\u624b\u6700\u5bb9\u6613\u61f5\u7684\u5730\u65b9\u3002\u5168\u7a0b\u7528\u767d\u8bdd \u53ef\u8fd0\u884c\u793a\u4f8b&#xff0c;\u4ece\u6700\u57fa\u7840\u7684\u300c\u53ef\u8fed\u4ee3\u5bf9\u8c61\u300d\u8bb2\u8d77&#xff0c;\u4e00\u6b65\u6b65\u62c6\u89e3\u539f\u7406\u3001\u7528\u6cd5\u548c\u533a\u522b&#xff0c;\u4f60\u53ef\u4ee5\u628a\u4ee3\u7801\u590d\u5236\u5230 Python \u91cc\u8fb9\u8fd0\u884c\u8fb9\u7406\u89e3\u3002\u4e00\u3001\u524d\u7f6e\u94fa\u57ab&#xff1a;\u5148\u641e\u61c2\u300c\u53ef\u8fed\u4ee3\u5bf9\u8c61\u300d\u6211\u4eec\u5e73\u65f6\u7528 for \u5faa\u73af\u904d\u5386\u5217\u8868\u3001\u5b57\u7b26\u4e32\u3001\u5b57\u5178\u3001\u5143\u7ec4&#xff0c;\u8fd9\u4e9b\u80fd\u88ab for \u5faa\u73af\u9010\u4e2a\u53d6\u51fa\u5143\u7d20\u7684\u4e1c\u897f&#xff0c;\u90fd\u53eb\u300c\u53ef\u8fed\u4ee3\u5bf9\u8c61&#xff08;Iterable","og_url":"https:\/\/www.wsisp.com\/helps\/85259.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-07-27T00:24:53+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"4 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/85259.html","url":"https:\/\/www.wsisp.com\/helps\/85259.html","name":"Python\u8bed\u8a00\u57fa\u7840\uff1a17_\u8fed\u4ee3\u5668\u4e0e\u751f\u6210\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-07-27T00:24:53+00:00","dateModified":"2026-07-27T00:24:53+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/85259.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/85259.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/85259.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Python\u8bed\u8a00\u57fa\u7840\uff1a17_\u8fed\u4ee3\u5668\u4e0e\u751f\u6210\u5668"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/85259","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=85259"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/85259\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=85259"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=85259"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=85259"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=85259"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}