【发布时间】:2016-03-10 18:33:04
【问题描述】:
这棵树是非二元的,因为它可以有两个以上的孩子。每个节点都有一个子节点列表。在这里,我正在编写list_if,它从树中返回满足谓词p 的值列表。当我测试我的代码时,它显示'int' object is not iterable。有人知道我怎么写代码吗?
def list_if(t, p):
"""
Return a list of values in Tree t that satisfy predicate p(value).
Assume p is defined on all of t's values.
@param Tree t: tree to list values that satisfy predicate p
@param (object)->bool p: predicate to check values with
@rtype: list[object]
>>> def p(v): return v > 4
>>> t = descendants_from_list(Tree(0), [1, 2, 3, 4, 5, 6, 7, 8], 3)
>>> list_ = list_if(t, p)
>>> list_.sort()
>>> list_
[5, 6, 7, 8]
>>> def p(v): return v % 2 == 0
>>> list_ = list_if(t, p)
>>> list_.sort()
>>> list_
[0, 2, 4, 6, 8]
""" ###gather_lists() eliminates lists in list
return gather_lists([t.value if p(t.value) else []] +[list_if(x, p) for x in t.children])
【问题讨论】:
-
在哪里显示?
标签: python list python-3.x tree