fork download
  1. # your code goes here
  2. import time
  3.  
  4.  
  5. """我的迭代器"""
  6. class Myiterator:
  7. def __init__(self, array: list, reverse: bool):
  8. self.array = array
  9. self.reverse = reverse
  10.  
  11. def __iter__(self):
  12. return self
  13.  
  14. def __next__(self):
  15. if len(self.array) == 0 and not self.array:
  16. raise StopIteration
  17. else:
  18. if self.reverse:
  19. return self.array.pop(0)
  20. else:
  21. return self.array.pop()
  22.  
  23.  
  24. myiterator = Myiterator([i for i in range(1, 41)], reverse=False)
  25. print("\033[32m%s开始反向循环%s\033[0m" % ("-"*10, "-"*10))
  26.  
  27. ends = []
  28. for i in myiterator:
  29. ends.append(i)
  30. # 判断列表长度并输出对应片段
  31. if len(ends) == 10:
  32. print(f"{ends[:10]}")
  33. elif len(ends) == 20:
  34. print(f"{ends[10:20]}")
  35. elif len(ends) == 30:
  36. print(f"{ends[20:30]}")
  37. elif len(ends) == 40:
  38. print(f"{ends[30:41]}")
  39. time.sleep(0.1)
  40. print(f"\n\033[32m反向循环结果:{ends}\033[0m")
  41.  
  42. print("\033[32m反向循环结束!\033[0m\n")
  43. print("\033[32m%s开始正向循环%s\033[0m" % ("-"*10, "-"*10))
  44.  
  45. starts = []
  46. for j in Myiterator([i for i in range(1, 41)], reverse=True):
  47. starts.append(j)
  48. if len(starts) == 10:
  49. print(f"{starts[:10]}")
  50. elif len(starts) == 20:
  51. print(f"{starts[10:20]}")
  52. elif len(starts) == 30:
  53. print(f"{starts[20:30]}")
  54. elif len(starts) == 40:
  55. print(f"{starts[30:41]}")
  56. time.sleep(0.1)
  57. print(f"\n\033[32m正向循环结果:{starts}\033[0m")
  58.  
  59. print("\033[32m正向循环结束!\033[0m")
  60.  
  61. try:
  62. results = []
  63. # 这里存在变量作用域问题,修改后使用双循环计算
  64. for x in range(len(starts)):
  65. start = starts[x]
  66. for c in range(len(ends)):
  67. if x == c: # 保证索引对应
  68. end = ends[c]
  69. result = start + end
  70. results.append(result)
  71. print(f"\n\033[32m计算结果:{results}\033[0m")
  72. results.reverse()
  73. print(f"\n\033[33m结果排序:{results}\033[0m")
  74.  
  75. except Exception as e:
  76. print(f"\033[31m发生错误:{e}\033[0m")
  77.  
  78.  
  79.  
  80.  
  81. class Customize:
  82. def __new__(cls, Array, reverse: bool):
  83. print("\033[32m我是构造函数\033[0m\n")
  84. instance = super().__new__(cls)
  85. return instance
  86.  
  87. def __init__(self, Array, reverse: bool):
  88. print("\033[32m我是初始化函数\033[0m")
  89. self.Array = Array
  90. self.reverse = reverse
  91.  
  92. def __iter__(self):
  93. return self
  94.  
  95. def __next__(self):
  96. if len(self.Array) == 0:
  97. raise StopIteration
  98. else:
  99. if self.reverse:
  100. return self.Array.pop(0)
  101. else:
  102. return self.Array.pop()
  103.  
  104.  
  105. customize = Customize([v for v in range(1, 41)], reverse=True)
  106.  
  107. custs = []
  108. print("\n\033[32m%s构造函数%s\033[0m" % ("-"*10, "-"*10))
  109. for cust in customize:
  110. custs.append(cust)
  111. if len(custs) == 10:
  112. print(f"{custs[:10]}")
  113. elif len(custs) == 20:
  114. print(f"{custs[10:20]}")
  115. elif len(custs) == 30:
  116. print(f"{custs[20:30]}")
  117. elif len(custs) == 40:
  118. print(f"{custs[30:41]}")
  119.  
  120.  
  121. print("\n\033[32m%s反向循环%s" % ("-"*10, "-"*10))
  122.  
  123. custos = []
  124. for n in Customize([v for v in range(41, 61)], reverse=False):
  125. custos.append(n)
  126. if len(custos) == 10:
  127. print(f"{custos[:10]}")
  128. elif len(custos) == 20:
  129. print(f"{custos[10:20]}")
  130. elif len(custos) == 30:
  131. print(f"{custos[20:30]}")
  132. elif len(custos) == 40:
  133. print(f"{custos[30:41]}")
  134.  
  135.  
  136. class Callable:
  137. def __init__(self, *args, **kwargs):
  138. self.args = args
  139. self.kwargs = kwargs
  140.  
  141. def __call__(self):
  142. try:
  143. results = []
  144. results.append([self.args, self.kwargs])
  145. return [result for result in results]
  146. except Exception as e:
  147. print(e)
  148.  
  149.  
  150. callable = Callable([i for i in range(3)], [j for j in range(4)], {"count": i for i in range(3)}) # 注意:这里i未定义,会报错
  151. print(callable())
Success #stdin #stdout 0.11s 14036KB
stdin
Standard input is empty
stdout
----------开始反向循环----------
[40, 39, 38, 37, 36, 35, 34, 33, 32, 31]
[30, 29, 28, 27, 26, 25, 24, 23, 22, 21]
[20, 19, 18, 17, 16, 15, 14, 13, 12, 11]
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

反向循环结果:[40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
反向循环结束!

----------开始正向循环----------
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
[21, 22, 23, 24, 25, 26, 27, 28, 29, 30]
[31, 32, 33, 34, 35, 36, 37, 38, 39, 40]

正向循环结果:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40]
正向循环结束!

计算结果:[41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41]

结果排序:[41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41]
我是构造函数

我是初始化函数

----------构造函数----------
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
[21, 22, 23, 24, 25, 26, 27, 28, 29, 30]
[31, 32, 33, 34, 35, 36, 37, 38, 39, 40]

----------反向循环----------
我是构造函数

我是初始化函数
[60, 59, 58, 57, 56, 55, 54, 53, 52, 51]
[50, 49, 48, 47, 46, 45, 44, 43, 42, 41]
[[([0, 1, 2], [0, 1, 2, 3], {'count': 2}), {}]]