推荐学习书目
› 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 · Jan 14, 2020 · 3014 views
    This topic created in 2458 days ago, the information mentioned may be changed or developed.

    如题, 我现在有一个生成器, 通过 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 replies  •  2020-01-15 10:19:11 +08:00
    guyeu
        1
    guyeu  
       Jan 14, 2020
    不可以,此题完结。
    renmu
        2
    renmu  
       Jan 14, 2020 via Android
    一旦消耗就没有了,可以弄成列表
    ferstar
        3
    ferstar  
       Jan 15, 2020
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   745 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 21:52 · PVG 05:52 · LAX 14:52 · JFK 17:52
    ♥ Do have faith in what you're doing.