如何将第一个数字包含到 python 中的迭代器 class?
How to include first number to iterator class in python?
如何编辑这个class以获得PPP中的n0?我没有获得序列中的初始编号。谢谢
class P():
def __init__(self, n0):
self.n = n0
def __iter__(self):
return self
def __next__(self):
if self.n == 1:
raise StopIteration
self.n = self.n - 1
return self.n
nmax = 10
PP = P(nmax)
PPP = []
for j in PP:
PPP.append(j)
print(PPP)
当前输出:
[9, 8, 7, 6, 5, 4, 3, 2, 1]
期望的输出:
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
在递减之前存储该值
def __next__(self):
if self.n == 1:
raise StopIteration
num = self.n
self.n = self.n - 1
return num
#[10, 9, 8, 7, 6, 5, 4, 3, 2]
如何编辑这个class以获得PPP中的n0?我没有获得序列中的初始编号。谢谢
class P():
def __init__(self, n0):
self.n = n0
def __iter__(self):
return self
def __next__(self):
if self.n == 1:
raise StopIteration
self.n = self.n - 1
return self.n
nmax = 10
PP = P(nmax)
PPP = []
for j in PP:
PPP.append(j)
print(PPP)
当前输出:
[9, 8, 7, 6, 5, 4, 3, 2, 1]
期望的输出:
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
在递减之前存储该值
def __next__(self):
if self.n == 1:
raise StopIteration
num = self.n
self.n = self.n - 1
return num
#[10, 9, 8, 7, 6, 5, 4, 3, 2]