【发布时间】:2017-10-26 22:55:45
【问题描述】:
标题有点奇怪,但我不知道具体是怎么称呼的,所以请原谅我用抽象的标题......
我在网上找到了这样的代码:
def lcs(xstr, ystr):
"""
>>> lcs('thisisatest', 'testing123testing')
'tsitest'
"""
if not xstr or not ystr:
return ""
x, xs, y, ys = xstr[0], xstr[1:], ystr[0], ystr[1:]
if x == y:
return x + lcs(xs, ys)
else:
return max(lcs(xstr, ys), lcs(xs, ystr), key=len)
我是python新手,不明白怎么调用lcs(xs, ys) in
return x + lcs(xs, ys)
据我了解,lcs() 尚未完全定义,我很困惑如何在自身内部调用自身的函数....
另外,我不知道 key = len 在做什么
max(lcs(xstr, ys), lcs(xs, ystr), key=len)
我知道 max( 1st, 2nd ) 是如何工作的,但我不知道第三个参数在做什么。 “key”是什么意思,为什么用“len”作为“key”的值?
【问题讨论】:
-
你是正确的,当你定义函数时,函数本身没有被定义。但是,函数在调用时已定义,因此不会有
NameError。
标签: python python-3.x recursion key lcs