zoukankan      html  css  js  c++  java
  • 迭代器

    迭代器能减少内存空间实现循环。

    方法1:

     1 import time
     2 from collections import Iterable
     3 from collections import Iterator
     4 
     5 
     6 class Classmate(object):
     7 
     8 
     9     def __init__(self):
    10         self.names = list()
    11 
    12 
    13     def add(self,name):
    14         self.names.append(name)
    15 
    16 
    17     def __iter__(self):
    18         """如果想要一个对象变成一个可以迭代的对象,即可以使用for,那么必须实现__iter__方法"""
    19         return Classmator(self);  # 调用Classmator 然后把自己传过去
    20 
    21 
    22 class Classmator(object):
    23     def __init__(self,obj):
    24         self.obj = obj
    25         # 定义索引值
    26         self.current_num = 0
    27 
    28     def __iter__(self):
    29         pass
    30 
    31 
    32     def __next__(self):
    33         # 判断索引值是否超出列表个数范围
    34         if self.current_num < len(self.obj.names):
    35             ret = self.obj.names[self.current_num]
    36             # 索引值+1
    37             self.current_num += 1
    38             return ret
    39         else:
    40             # 抛出异常,终止for循环
    41             raise StopIteration
    42 
    43 classmate = Classmate()
    44 
    45 classmate.add("张三")
    46 classmate.add("张二")
    47 classmate.add("李四")
    48 
    49 # 判断一个类型是否可以迭代   isinstance(要判断的对象,Iterable)
    50 print("判断classmate是否是可以迭代的对象:", isinstance(classmate,Iterable))
    51 # 迭代器
    52 classmate_iterator = iter(classmate)
    53 # 判断classmate_iterator是否是迭代器
    54 print("判断classmate_iterator是否是迭代器:", isinstance(classmate_iterator,Iterator))
    55 # print(next(classmate_iterator))
    56 
    57 for name in classmate:
    58     print(name)
    59     time.sleep(1)

    两个类可以改善成一个,只要有__iter__方法和__next__方法:

     1 import time
     2 from collections import Iterable
     3 from collections import Iterator
     4 
     5 
     6 
     7 class Classmate(object):
     8     def __init__(self):
     9         self.names = list()
    10         self.current_num = 0
    11 
    12     def add(self,name):
    13         self.names.append(name)
    14 
    15 
    16     def __iter__(self):
    17         return self
    18 
    19 
    20     def __next__(self):
    21         if self.current_num < len(self.names):
    22             ret = self.names[self.current_num]
    23             self.current_num += 1
    24             return ret
    25         else:
    26             raise StopIteration
    27 
    28 
    29 classmate = Classmate()
    30 classmate.add("张三")
    31 classmate.add("李四")
    32 classmate.add("王五")
    33 
    34 # 判断一个类型是否可以迭代   isinstance(要判断的对象,Iterable)
    35 print("判断classmate是否是可以迭代的对象:", isinstance(classmate,Iterable))
    36 # 迭代器
    37 classmate_iterator = iter(classmate)
    38 # 判断classmate_iterator是否是迭代器
    39 print("判断classmate_iterator是否是迭代器:", isinstance(classmate_iterator,Iterator))
    40 # print(next(classmate_iterator))
    41 
    42 for name in classmate:
    43     print(name)
    44     time.sleep(1)
  • 相关阅读:
    HDU 1010 Tempter of the Bone
    HDU 4421 Bit Magic(奇葩式解法)
    HDU 2614 Beat 深搜DFS
    HDU 1495 非常可乐 BFS 搜索
    Road to Cinema
    Sea Battle
    Interview with Oleg
    Spotlights
    Substring
    Dominating Patterns
  • 原文地址:https://www.cnblogs.com/yifengs/p/11392503.html
Copyright © 2011-2022 走看看