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
Amone
V2EX  ›  Python

问个 Python 遍历的问题

  •  
  •   Amone · 2018-11-06 13:14:50 +08:00 · 3707 次点击
    这是一个创建于 1996 天前的主题,其中的信息可能已经有所发展或是发生改变。

    xz2cs4641-n231-x512-9sv5-bs1s41o2853x

    上面 37 个字符 每个字符从 0-9,a-z 每个字符都组一遍

    一开始用 for 嵌套遍历弄,然而 python 嵌套限制 20 个。


    嵌套到最后面运行的时候,就提示: SyntaxError: too many statically nested blocks 太多静态嵌套的块 python 限制 20 个嵌套


    有什么方式能解决么 QAQ

    26 条回复    2018-11-07 09:05:53 +08:00
    gstqc
        1
    gstqc  
       2018-11-06 13:20:19 +08:00 via Android
    是我没看明白题目
    这不是两个 for 搞定了吗?
    Amone
        2
    Amone  
    OP
       2018-11-06 13:22:19 +08:00
    @gstqc 能说下代码么 QAQ 或者加个 QQ 向前辈学习下:1806241622
    ClutchBear
        5
    ClutchBear  
       2018-11-06 13:30:34 +08:00
    搜索 python
    uuid
    dapengzhao
        6
    dapengzhao  
       2018-11-06 13:32:03 +08:00
    楼下把题目解释一下吧
    rabbbit
        7
    rabbbit  
       2018-11-06 13:33:26 +08:00
    arr = [str(i) for i in range(0, 10)] + [chr(i) for i in range(ord('a'), ord('z') + 1)]

    def gen(value):
    if len(value) == 37:
    print(value)
    return
    for i in arr:
    gen(value + i)

    for i in arr:
    gen(i)
    WEIHUANJIHE
        8
    WEIHUANJIHE  
       2018-11-06 13:33:54 +08:00
    没看懂


    s = 'xz2cs4641-n231-x512-9sv5-bs1s41o2853x'
    ss = sorted(s)

    def _filter_func(item):
    return ('0' <= item <= 'z')



    def combine(s):
    return ''.join(sorted(filter(_filter_func, s)))

    print(combine(s))
    Ken999
        9
    Ken999  
       2018-11-06 13:34:21 +08:00
    你的意思是,26 个字母加 10 个数字有多少种排列组合?
    rabbbit
        10
    rabbbit  
       2018-11-06 13:34:49 +08:00
    recall704
        11
    recall704  
       2018-11-06 13:45:13 +08:00
    如果要生成这样的字符串,可以用 uuid,
    如果要判断字符串是否满足这样的规则,可以用正则表达式。
    necomancer
        12
    necomancer  
       2018-11-06 15:11:17 +08:00
    import uuid
    print('%s' % (uuid.uuid4()))

    试试。有其他要求请看
    help(uuid.uuid1)
    help(uuid.uuid3)
    help(uuid.uuid4)
    help(uuid.uuid5)
    rocketman13
        13
    rocketman13  
       2018-11-06 18:35:39 +08:00
    你不会写了 37 个 for 循环嵌套在一起吧。。。。
    Sanko
        14
    Sanko  
       2018-11-06 20:24:08 +08:00 via Android
    37 层 for 循环怕不怕要被打死
    ruoruodetouzizhe
        15
    ruoruodetouzizhe  
       2018-11-06 21:02:32 +08:00   ❤️ 1
    看样子是准备写 37 层嵌套,结果 python 只让写 20 个
    lihongjie0209
        16
    lihongjie0209  
       2018-11-06 21:13:54 +08:00
    佩服佩服
    gstqc
        17
    gstqc  
       2018-11-06 21:20:04 +08:00 via Android
    37 个嵌套,每次循环 36 个元素?
    一共跑 36 ** 37 次……
    tuding
        18
    tuding  
       2018-11-06 21:23:59 +08:00
    楼下好心人帮忙解释一下, 没有读懂
    duoguo
        19
    duoguo  
       2018-11-06 22:30:54 +08:00 via Android
    我对不起我的语文老师。。。
    JCZ2MkKb5S8ZX9pq
        20
    JCZ2MkKb5S8ZX9pq  
       2018-11-06 23:04:51 +08:00
    itertools 好像带排列组合的
    ankelo
        21
    ankelo  
       2018-11-06 23:32:37 +08:00
    我只能说,这个属于新手问题
    AX5N
        22
    AX5N  
       2018-11-06 23:44:36 +08:00
    高中知识,排列组合....
    freakxx
        23
    freakxx  
       2018-11-07 00:01:00 +08:00
    @tuding
    每个位的生成都 for 一遍 string+digit,然后 再拼接 return 回去;

    @Amone
    import string
    def random_generator(size=6, chars=string.ascii_letters + string.digits):
    return "".join(random.choice(chars) for _ in range(size))
    wizardoz
        24
    wizardoz  
       2018-11-07 00:21:21 +08:00
    递归或栈
    wizardoz
        25
    wizardoz  
       2018-11-07 00:22:11 +08:00
    另外,任何一种语言都不要手写 37 层嵌套循环
    epleone
        26
    epleone  
       2018-11-07 09:05:53 +08:00
    ```python
    from itertools import permutations

    _str = 'xz2cs4641-n231-x512-9sv5-bs1s41o2853x'

    for i in permutations(_str , len(_str )):
    print(i)

    ```
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5044 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 09:40 · PVG 17:40 · LAX 02:40 · JFK 05:40
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.