根据这个问题修改下面的代码:Traceback (most recent call last): File "/var/ws/tmp/test/private_test.py", line 89, in test4 sorted([f(v) for f in lookups(k, 2)]), File "/var/ws/tmp/test/private_test.py", line 89, in <listcomp> sorted([f(v) for f in lookups(k, 2)]), TypeError: g() missing 1 required positional argument: 'b' ----------------------------------------------------------------------要修改的代码如下:def lookups(k, key): """Yield one lookup function for each node of k that has the label key. >>> k = tree(5, [tree(7, [tree(2)]), tree(8, [tree(3), tree(4)]), tree(5, [tree(4), tree(2)])]) >>> v = tree('Go', [tree('C', [tree('C')]), tree('A', [tree('S'), tree(6)]), tree('L', [tree(1), tree('A')])]) >>> type(lookups(k, 4)) <class 'generator'> >>> sorted([f(v) for f in lookups(k, 2)]) ['A', 'C'] >>> sorted([f(v) for f in lookups(k, 3)]) ['S'] >>> [f(v) for f in lookups(k, 6)] [] """ if label(k) == key: yield lambda v, b: label(v) for i in range(len(branches(k))): for lookup in lookups(branches(k)[i], key): yield new_lookup(i, lookup) def new_lookup(i, f): def g(v, b): return f(v[b][i]) return g
时间: 2024-02-07 09:03:29 浏览: 33
根据错误提示 "g() missing 1 required positional argument: 'b'",我们可以看出在函数`g`中缺少一个参数'b'。因此,我们需要将'b'作为参数添加到函数`g`中。
修改后的代码如下:
```python
def lookups(k, key):
"""
Yield one lookup function for each node of k that has the label key.
>>> k = tree(5, [tree(7, [tree(2)]), tree(8, [tree(3), tree(4)]), tree(5, [tree(4), tree(2)])])
>>> v = tree('Go', [tree('C', [tree('C')]), tree('A', [tree('S'), tree(6)]), tree('L', [tree(1), tree('A')])])
>>> type(lookups(k, 4))
<class 'generator'>
>>> sorted([f(v, 0) for f in lookups(k, 2)])
['A', 'C']
>>> sorted([f(v, 0) for f in lookups(k, 3)])
['S']
>>> [f(v, 0) for f in lookups(k, 6)]
[]
"""
if label(k) == key:
yield lambda v, b: label(v[b])
for i in range(len(branches(k))):
for lookup in lookups(branches(k)[i], key):
yield new_lookup(i, lookup)
def new_lookup(i, f):
def g(v, b):
return f(v[b][i])
return g
```
现在函数`g`有两个参数,即`v`和`b`,并且在调用`f`时需要传入`v[b][i]`作为参数。同时,在测试函数中,我们需要调用`f`时传入一个参数`b`,表示在访问`v`时应该使用哪个分支。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)