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

一段 python 代码里的迭代看不懂,求解

  •  
  •   DannyVim · 2015-08-03 08:59:16 +08:00 · 2992 次点击
    这是一个创建于 3190 天前的主题,其中的信息可能已经有所发展或是发生改变。
    def is_reducible(word, word_dict):
        if word in memo:
            return memo[word]
        res=[]
        for child in children(word, word_dict):        
            t=is_reducible(child, word_dict) 
            #这里的迭代什么时候结束啊,传给t的到底是哪些值呢?
            if t:
            #这里是指t不是空集吗?
                res.append(child)
        memo[word]=res                                            
        return res
    
    def children(word, word_dict):
        res=[]
        for i in range(len(word)):
            child=word[:i]+word[i+1:]
            if child in word_dict:
                res.append(child)
        return res
    

    如果可以的话,能不能用一个单词举例子呢?

    9 条回复    2015-08-03 14:10:52 +08:00
    monkeylyf
        1
    monkeylyf  
       2015-08-03 09:15:02 +08:00
    where is var 'memo' defined?
    zerh925
        2
    zerh925  
       2015-08-03 09:23:01 +08:00
    child = word[:i] + word[i+1:]
    这一段意思不是从word中去除一个字母吗?
    那什么情况下去除了一个字母反而会变成一个单词?
    加入word = congratulation, 无论i=多少,child都不是一个正确的单词吧?
    根据你函数名称,是要找长单词中的最小有意义的短单词?
    比如,uncomfortable中找comfort?
    sinux
        3
    sinux  
       2015-08-03 09:37:49 +08:00
    不是递归了一下嘛
    tomwen
        4
    tomwen  
       2015-08-03 09:56:52 +08:00   ❤️ 1
    ie :
    children('bread') => ['read', 'brad','bred']
    -->children('read') => ['red',]
    ---->children('red') =>[] 迭代停止

    -->children('brad')=> ['bad',]
    ...

    -->children('bred')=> ['red']
    ....
    DannyVim
        5
    DannyVim  
    OP
       2015-08-03 11:22:16 +08:00
    @tomwen 传给res的值是当t为空集时的child吗?
    DannyVim
        6
    DannyVim  
    OP
       2015-08-03 11:24:43 +08:00
    @monkeylyf 这里不必管它了。
    @zerh925 是要找出所有更短的单词。
    tomwen
        7
    tomwen  
       2015-08-03 11:32:36 +08:00
    res只是一个局部变量,在每个函数中间都是先初始化为一个空列表,如果有符合条件的就放进去,如果没有结果就是空列表,最后遍历结束后返回res。
    只是碰巧在两个函数中间都使用了同样的名字局部变量res而已,他们之间没有关系。
    DannyVim
        8
    DannyVim  
    OP
       2015-08-03 13:45:53 +08:00
    @tomwen 在‘is_reducible’这个函数里面,如果t为空集,则把此时的‘child’放进‘res’吗?例如是把‘red’放进去,而‘read’并不会被放进去吗?
    tomwen
        9
    tomwen  
       2015-08-03 14:10:52 +08:00 via iPhone
    Just try it.
    Insert "print word,res" after the 4th line, and run it.
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   873 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 20:18 · PVG 04:18 · LAX 13:18 · JFK 16:18
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.