社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

python中的最后一个迭代器数

Carly • 4 年前 • 1806 次点击  

如何编辑迭代器,同时给出序列中的最后一个数字?我的意思是,总的来说,不是为了这么简单的序列。使用<而不是==不是一个选项。

class P():
    def __init__(self, n0):
        self.n = n0

    def __iter__(self):
        return self
    
    def __next__(self):
        if self.n == 1:
            raise StopIteration
        num = self.n
        self.n = self.n // 2 if self.n % 2 == 0 else 3 * self.n + 1
        return num

nmax = 1000
PP = P(nmax)

PPP = []

for j in PP:
    PPP.append(j) 

print(PPP)

电流输出:

[10, 5, 16, 8, 4, 2]

期望输出:

[10, 5, 16, 8, 4, 2, 1]

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/133822
文章 [ 1 ]  |  最新文章 4 年前