V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
kayseen
V2EX  ›  Python

根据 Python 生成器中的某个值, 获取下一个值

  •  
  •   kayseen · 2020-01-14 17:23:59 +08:00 · 2222 次点击
    这是一个创建于 1556 天前的主题,其中的信息可能已经有所发展或是发生改变。

    如题, 我现在有一个生成器, 通过 for i in range(10) 生成 10 个随机数, 然后退出了这个循环, 然后可以根据生成的第 3 个随机数去获取第四个随机数吗?

    import itertools
    
    mylist = ("".join(x) for x in itertools.product("0123456789", repeat=5))
    for i in range(10):
        print(next(mylist))
    
    

    然后输出:

    00000
    00001
    00002
    00003
    00004
    00005
    00006
    00007
    00008
    00009
    

    已经退出该循环, 我现在拿着 00002 怎么样才可以获取 00004 呢?

    3 条回复    2020-01-15 10:19:11 +08:00
    guyeu
        1
    guyeu  
       2020-01-14 18:08:37 +08:00
    不可以,此题完结。
    renmu
        2
    renmu  
       2020-01-14 18:59:25 +08:00 via Android
    一旦消耗就没有了,可以弄成列表
    ferstar
        3
    ferstar  
       2020-01-15 10:19:11 +08:00
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3278 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 12:17 · PVG 20:17 · LAX 05:17 · JFK 08:17
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.