{"id":7018,"date":"2020-06-29T18:04:13","date_gmt":"2020-06-30T01:04:13","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7018"},"modified":"2020-06-29T18:04:15","modified_gmt":"2020-06-30T01:04:15","slug":"iterables-in-python","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/python\/iterables-in-python\/","title":{"rendered":"Iterables in Python"},"content":{"rendered":"\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u53ef\u8fed\u4ee3\u7684 Iterables - Python Weekly EP4\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/-PklUOOz4n8?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Example Code:<\/strong><\/h2>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">Iterables in Python<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">\n# Author: Huahua\nclass MyListIterator:\n  def __init__(self, my_list, index=0):\n    self.my_list = my_list\n    self.index = index\n\n  def __next__(self):\n    if self.index < len(self.my_list.data):\n      val = self.my_list.data[self.index]\n      self.index += 1\n      return val\n    else:\n      raise StopIteration()\n\n  def __iter__(self):\n    return MyListIterator(self.my_list, self.index)\n    # return self\n\nclass MyList:\n  def __init__(self, data):\n    self.data = list(data)\n\n  def __getitem__(self, index):\n    return self.data[index]\n\n  def __len__(self):\n    return len(self.data)\n\n  def __iter__(self):\n    return MyListIterator(self)\n\nmy_list = MyList([1,2,3,4,5])\nit = iter(my_list)\nnext(it)\nit2 = iter(it)\nnext(it)\nprint('--- it ---')\nfor x in it:\n  print(x) # 3, 4, 5\nprint('--- it2 ---')\nfor x in it2:\n  print(x) # 2, 3, 4, 5\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Example Code: # Author: Huahua class MyListIterator: def __init__(self, my_list, index=0): self.my_list = my_list self.index = index def __next__(self): if self.index < len(self.my_list.data): val =&#8230;\n<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[605],"tags":[626,624,625,517],"class_list":["post-7018","post","type-post","status-publish","format-standard","hentry","category-python","tag-expcetion","tag-iterables","tag-iterator","tag-python","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7018","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=7018"}],"version-history":[{"count":1,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7018\/revisions"}],"predecessor-version":[{"id":7019,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7018\/revisions\/7019"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7018"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7018"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7018"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}