{"id":701,"date":"2026-03-17T17:02:00","date_gmt":"2026-03-17T09:02:00","guid":{"rendered":"https:\/\/mitongxue.cn\/?p=701"},"modified":"2026-07-15T17:13:24","modified_gmt":"2026-07-15T09:13:24","slug":"itertools-%e7%ae%80%e4%bb%8b","status":"publish","type":"post","link":"https:\/\/mitongxue.cn\/index.php\/2026\/03\/17\/itertools-%e7%ae%80%e4%bb%8b\/","title":{"rendered":"itertools \u7b80\u4ecb"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><code>itertools<\/code> \u6a21\u5757\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u6548\u7684\u8fed\u4ee3\u5668\u5de5\u5177\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import itertools<br>\u200b<br># count(start, step)\uff1a\u65e0\u9650\u8ba1\u6570<br>counter = itertools.count(10, 2)<br>print(next(counter)) &nbsp;# 10<br>print(next(counter)) &nbsp;# 12<br>print(next(counter)) &nbsp;# 14<br>\u200b<br># cycle(iterable)\uff1a\u65e0\u9650\u5faa\u73af<br>cy = itertools.cycle(&#91;\"A\", \"B\", \"C\"])<br>print(next(cy)) &nbsp;# A<br>print(next(cy)) &nbsp;# B<br>print(next(cy)) &nbsp;# C<br>print(next(cy)) &nbsp;# A\uff08\u91cd\u65b0\u5f00\u59cb\uff09<br>\u200b<br># repeat(value, times)\uff1a\u91cd\u590d\u503c<br>times_three = list(itertools.repeat(\"x\", 3))<br>print(times_three) &nbsp;# &#91;'x', 'x', 'x']<br>\u200b<br># chain(*iterables)\uff1a\u8fde\u63a5\u591a\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61<br>combined = list(itertools.chain(&#91;1, 2], &#91;3, 4], &#91;5, 6]))<br>print(combined) &nbsp;# &#91;1, 2, 3, 4, 5, 6]<br>\u200b<br># islice(iterable, start, stop, step)\uff1a\u5207\u7247\uff08\u652f\u6301\u65e0\u9650\u8fed\u4ee3\u5668\uff09<br>first_five = list(itertools.islice(itertools.count(), 5))<br>print(first_five) &nbsp;# &#91;0, 1, 2, 3, 4]<br>\u200b<br># permutations(iterable, r)\uff1a\u6392\u5217<br>perms = list(itertools.permutations(&#91;1, 2, 3], 2))<br>print(perms) &nbsp;# &#91;(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)]<br>\u200b<br># combinations(iterable, r)\uff1a\u7ec4\u5408<br>combs = list(itertools.combinations(&#91;1, 2, 3, 4], 2))<br>print(combs) &nbsp;# &#91;(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>itertools \u6a21\u5757\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u6548\u7684\u8fed\u4ee3\u5668\u5de5\u5177..<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[52,14],"tags":[],"class_list":["post-701","post","type-post","status-publish","format-standard","hentry","category-python","category-back"],"featured_image_urls":{"full":"","thumbnail":"","medium":"","medium_large":"","large":"","1536x1536":"","2048x2048":""},"author_info":{"info":["\u9648 \u67d0\u4eba"]},"category_info":"<a href=\"https:\/\/mitongxue.cn\/index.php\/category\/back\/python\/\" rel=\"category tag\">Python<\/a> <a href=\"https:\/\/mitongxue.cn\/index.php\/category\/back\/\" rel=\"category tag\">\u540e\u7aef<\/a>","tag_info":"\u540e\u7aef","comment_count":"0","_links":{"self":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/701","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/comments?post=701"}],"version-history":[{"count":1,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/701\/revisions"}],"predecessor-version":[{"id":703,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/701\/revisions\/703"}],"wp:attachment":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/media?parent=701"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/categories?post=701"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/tags?post=701"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}