{"id":3024,"date":"2025-06-03T16:57:12","date_gmt":"2025-06-04T09:08:11","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3024"},"modified":"2025-06-04T16:57:12","modified_gmt":"2025-06-04T09:08:11","slug":"%e4%ba%86%e8%a7%a3python%e7%9a%84iter%e5%87%bd%e6%95%b8","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%ba%86%e8%a7%a3python%e7%9a%84iter%e5%87%bd%e6%95%b8\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3 Python \u7684 `iter()` \u51fd\u6578\uff1a2025 \u6700\u65b0\u7528\u6cd5\u8207\u5be6\u4f5c\u7bc4\u4f8b"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, iter(), Python \u6559\u5b78, Python \u8fed\u4ee3\"><\/p>\n<h1>\u6df1\u5165\u4e86\u89e3 Python \u7684 `iter()` \u51fd\u6578<\/h1>\n<p>\u5728 Python \u4e2d\uff0c`iter()` \u51fd\u6578\u662f\u4e00\u500b\u975e\u5e38\u91cd\u8981\u7684\u5de5\u5177\uff0c\u7528\u65bc\u5275\u5efa\u53ef\u91cd\u8907\u4f7f\u7528\u7684\u8fed\u4ee3\u5668\u3002\u9019\u4f7f\u6211\u5011\u80fd\u5920\u8f15\u9b06\u5730\u904d\u6b77\u5404\u7a2e\u53ef\u8fed\u4ee3\u7269\u4ef6\uff0c\u5982\u5b57\u4e32\u3001\u5217\u8868\u3001\u5143\u7d44\u548c\u5b57\u5178\u7b49\u3002\u672c\u6587\u5c07\u63d0\u4f9b2025\u5e74\u7684\u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10\uff0c\u4e26\u88dc\u5145\u5be6\u4f5c\u7bc4\u4f8b\u3001\u932f\u8aa4\u6392\u9664\u548c\u5ef6\u4f38\u61c9\u7528\u3002<\/p>\n<h2>\u4f7f\u7528 `iter()` \u51fd\u6578<\/h2>\n<p>\u8981\u4f7f\u7528 `iter()` \u51fd\u6578\uff0c\u60a8\u53ea\u9700\u5c07\u4e00\u500b\u53ef\u8fed\u4ee3\u7269\u4ef6\u4f5c\u70ba\u53c3\u6578\u50b3\u5165\uff0c\u9019\u6a23\u5c31\u80fd\u5275\u5efa\u4e00\u500b\u8fed\u4ee3\u5668\u3002\u4f8b\u5982\uff1a<\/p>\n<p>&#8220;`python<br \/>\n# \u5efa\u7acb\u4e00\u500b\u53ef\u4ee5\u91cd\u8907\u4f7f\u7528\u7684\u7269\u4ef6<br \/>\nmy_iter = iter([&#8216;apple&#8217;, &#8216;banana&#8217;, &#8216;orange&#8217;])<\/p>\n<p># \u4f7f\u7528 next() \u51fd\u6578\u4f86\u53d6\u5f97\u4e0b\u4e00\u500b\u5143\u7d20<br \/>\nprint(next(my_iter))  # apple<br \/>\nprint(next(my_iter))  # banana<br \/>\nprint(next(my_iter))  # orange<br \/>\n&#8220;`<\/p>\n<p>\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\u5148\u5275\u5efa\u4e86\u4e00\u500b\u5305\u542b\u4e09\u500b\u6c34\u679c\u7684\u5217\u8868\uff0c\u7136\u5f8c\u4f7f\u7528 `next()` \u51fd\u6578\u4f86\u9010\u500b\u53d6\u5f97\u5143\u7d20\u3002\u9019\u7a2e\u65b9\u6cd5\u9069\u5408\u5728\u60a8\u9700\u8981\u9010\u6b65\u8655\u7406\u5143\u7d20\u6642\u4f7f\u7528\u3002<\/p>\n<h2>\u4f7f\u7528 `for` \u8ff4\u5708\u4f86\u8fed\u4ee3<\/h2>\n<p>\u9664\u4e86\u4f7f\u7528 `next()` \u51fd\u6578\uff0c\u6211\u5011\u9084\u53ef\u4ee5\u5229\u7528 `for` \u8ff4\u5708\u4f86\u904d\u6b77\u53ef\u8fed\u4ee3\u7269\u4ef6\uff0c\u9019\u6a23\u7684\u5beb\u6cd5\u66f4\u52a0\u7c21\u6f54\u8207\u76f4\u89c0\uff1a<\/p>\n<p>&#8220;`python<br \/>\n# \u5efa\u7acb\u4e00\u500b\u53ef\u4ee5\u91cd\u8907\u4f7f\u7528\u7684\u7269\u4ef6<br \/>\nmy_iter = iter([&#8216;apple&#8217;, &#8216;banana&#8217;, &#8216;orange&#8217;])<\/p>\n<p># \u4f7f\u7528 for \u8ff4\u5708\u4f86\u8fed\u4ee3<br \/>\nfor item in my_iter:<br \/>\n    print(item)<\/p>\n<p># apple<br \/>\n# banana<br \/>\n# orange<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6bb5\u7a0b\u5f0f\u78bc\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 `for` \u8ff4\u5708\u81ea\u52d5\u8fed\u4ee3\u6240\u6709\u9805\u76ee\u3002\u9019\u662f\u8655\u7406\u8fed\u4ee3\u4efb\u52d9\u7684\u63a8\u85a6\u65b9\u5f0f\uff0c\u56e0\u70ba\u5b83\u66f4\u5177\u53ef\u8b80\u6027\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u4f7f\u7528 `iter()` \u51fd\u6578\u6642\uff0c\u60a8\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u5e38\u898b\u7684\u932f\u8aa4\u3002\u4f8b\u5982\uff0c\u5c0d\u65bc\u975e\u53ef\u8fed\u4ee3\u7269\u4ef6\u8abf\u7528 `iter()` \u6703\u5f15\u767c `TypeError`\u3002\u8acb\u78ba\u4fdd\u60a8\u50b3\u5165\u7684\u7269\u4ef6\u662f\u53ef\u8fed\u4ee3\u7684\uff0c\u5982\u5217\u8868\u3001\u5b57\u4e32\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u932f\u8aa4\u793a\u7bc4\uff1a<\/p>\n<p>&#8220;`python<br \/>\n# \u5617\u8a66\u4f7f\u7528\u4e0d\u53ef\u8fed\u4ee3\u7684\u6574\u6578<br \/>\ntry:<br \/>\n    my_iter = iter(123)  # \u9019\u5c07\u5f15\u767c TypeError<br \/>\nexcept TypeError as e:<br \/>\n    print(f&#8221;\u932f\u8aa4\uff1a{e}&#8221;)<br \/>\n&#8220;`<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>`iter()` \u51fd\u6578\u4e0d\u50c5\u9650\u65bc\u57fa\u672c\u7684\u5217\u8868\u6216\u5b57\u4e32\u3002\u60a8\u9084\u53ef\u4ee5\u7528\u5b83\u4f86\u8655\u7406\u66f4\u8907\u96dc\u7684\u6578\u64da\u7d50\u69cb\uff0c\u5982\u5b57\u5178\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u793a\u7bc4\uff1a<\/p>\n<p>&#8220;`python<br \/>\n# \u4f7f\u7528 iter() \u904d\u6b77\u5b57\u5178\u7684\u9375<br \/>\nmy_dict = {&#8216;name&#8217;: &#8216;Alice&#8217;, &#8216;age&#8217;: 30, &#8216;city&#8217;: &#8216;New York&#8217;}<br \/>\ndict_iter = iter(my_dict)<\/p>\n<p>for key in dict_iter:<br \/>\n    print(key, my_dict[key])<\/p>\n<p># name Alice<br \/>\n# age 30<br \/>\n# city New York<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6bb5\u7a0b\u5f0f\u78bc\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 `iter()` \u4f86\u904d\u6b77\u5b57\u5178\u7684\u9375\u4e26\u7372\u53d6\u5176\u5c0d\u61c9\u7684\u503c\u3002<\/p>\n<p>\u5728\u9019\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5011\u63a2\u8a0e\u4e86 `iter()` \u51fd\u6578\u7684\u57fa\u672c\u7528\u6cd5\u548c\u5be6\u969b\u61c9\u7528\uff0c\u9019\u5c07\u5e6b\u52a9\u60a8\u5728 Python \u958b\u767c\u4e2d\u66f4\u6709\u6548\u7387\u5730\u8655\u7406\u8fed\u4ee3\u4efb\u52d9\u3002<\/p>\n<p>\u5982\u679c\u60a8\u60f3\u9032\u4e00\u6b65\u4e86\u89e3 Python \u7684\u5176\u4ed6\u529f\u80fd\uff0c\u5efa\u8b70\u67e5\u770b [vocus.cc Python \u6559\u5b78](https:\/\/vocus.cc) \u6216 [miner.tw \u7684\u8cc7\u6e90](https:\/\/miner.tw)\u3002<\/p>\n<h2>\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff08Q&#038;A\uff09<\/h2>\n<h3>Q1: `iter()` \u51fd\u6578\u8207 `next()` \u51fd\u6578\u7684\u95dc\u4fc2\u662f\u4ec0\u9ebc\uff1f<\/h3>\n<p>A1: `iter()` \u51fd\u6578\u7528\u65bc\u5275\u5efa\u4e00\u500b\u8fed\u4ee3\u5668\uff0c\u800c `next()` \u51fd\u6578\u7528\u65bc\u5f9e\u8fed\u4ee3\u5668\u4e2d\u7372\u53d6\u4e0b\u4e00\u500b\u5143\u7d20\u3002\u9019\u5169\u8005\u901a\u5e38\u4e00\u8d77\u4f7f\u7528\u4f86\u904d\u6b77\u53ef\u8fed\u4ee3\u7269\u4ef6\u3002<\/p>\n<h3>Q2: \u5982\u4f55\u77e5\u9053\u4e00\u500b\u7269\u4ef6\u662f\u5426\u53ef\u8fed\u4ee3\uff1f<\/h3>\n<p>A2: \u60a8\u53ef\u4ee5\u4f7f\u7528 `isinstance()` \u51fd\u6578\u4f86\u6aa2\u67e5\u4e00\u500b\u7269\u4ef6\u662f\u5426\u662f\u53ef\u8fed\u4ee3\u7684\uff0c\u901a\u5e38\u53ef\u8fed\u4ee3\u7269\u4ef6\u5305\u62ec\u5217\u8868\u3001\u5143\u7d44\u3001\u5b57\u5178\u548c\u5b57\u4e32\u3002<\/p>\n<h3>Q3: \u5728\u4f7f\u7528\u8fed\u4ee3\u5668\u6642\uff0c\u5982\u4f55\u907f\u514d `StopIteration` \u932f\u8aa4\uff1f<\/h3>\n<p>A3: \u5728\u4f7f\u7528 `next()` \u51fd\u6578\u6642\uff0c\u53ef\u4ee5\u63d0\u4f9b\u4e00\u500b\u9810\u8a2d\u503c\u4f5c\u70ba\u7b2c\u4e8c\u500b\u53c3\u6578\uff0c\u9019\u6a23\u7576\u8fed\u4ee3\u5668\u8017\u76e1\u6642\u5c07\u8fd4\u56de\u9019\u500b\u503c\uff0c\u800c\u4e0d\u662f\u5f15\u767c `StopIteration` \u932f\u8aa4\u3002\u4f8b\u5982\uff1a`next(my_iter, &#8216;\u7d50\u675f&#8217;)`\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3Python\u7684iter()\u51fd\u6578\uff0c\u5b83\u53ef\u4ee5\u8b93\u4f60\u5c07\u53ef\u8fed\u4ee3\u7684\u5c0d\u8c61\u8f49\u63db\u70ba\u8fed\u4ee3\u5668\uff0c\u4ee5\u4fbf\u53ef\u4ee5\u4f7f\u7528next()\u51fd\u6578\u4f86\u7372\u53d6\u5176\u4e2d\u7684\u5143\u7d20\u3002\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39iter()\u51fd\u6578\u7684\u7528\u6cd5\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u793a\u4f8b\u4f86\u89e3\u91cb\u5b83\u7684\u529f\u80fd\u3002<\/p>\n","protected":false},"author":1,"featured_media":2518,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[18],"tags":[15],"class_list":["post-3024","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2022\/12\/DALL\u00b7E-2022-12-28-14.25.55-\u62f7\u8c9d2.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-MM","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3024","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=3024"}],"version-history":[{"count":3,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3024\/revisions"}],"predecessor-version":[{"id":12957,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3024\/revisions\/12957"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/2518"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=3024"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3024"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3024"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}