我是编程新手,现在正在学习python。我正在构建一个系统,其中每个对象由单个元素或包含单个元素或二进制集的二进制集(包含集合......)组成,即递归结构。在这些对象中,我定义了一个函数,它给了我立即包含,以及一个递归函数,它递归地回答了元素是否包含在任何集合成员中的问题。二进制集和单例是元类“语法对象”的子类。每个对象都有一个唯一的标识符idx,每个对象原则上可以出现在多个地方,所以我们可以有一个如下的集合,idx作为-idx附加到对象
{{A-0,C-2} -4,{B-1,{C-2,{D-3,{A-0,C-2} -4} -5} -6} - 7
class SOMeta(object):
"""Abstract class, never instantiated. Has two subtypes, base case, recursive case"""
def __init__(self, idx: int):
self.idx = idx
def i_contains(self, a):
"""self immediately contains a if a is an element of self"""
if type(self) is Binary:
return a in self.syntactic_object_set
else:
return False
def contains(self, a):
"""self contains a if self immediately contains a, or some daughter of self contains a"""
if type(self) is Binary:
if self.i_contains(a):
return True
else:
to_return = False
for daughter in self.syntactic_object_set:
if type(daughter) is Binary:
to_return = to_return or daughter.contains(a)
return to_return
else:
return False
class Binary(SOMeta):
def __init__(self, the_set: Set[SOMeta], idx: int):
super(Binary, self).__init__(idx)
self.syntactic_object_set: Set[SOMeta] = the_set
def __str__(self):
set_strings = {str(obj) for obj in self.syntactic_object_set}
rep = "< " + str(self.idx) + ": " + ", ".join(set_strings) + " >"
return rep
class Singleton(SOMeta):
def __init__(self, lexical_item: string, idx: int):
super(Singleton, self).__init__(idx)
self.lexical_item = lexical_item
def __str__(self):
rep = "< " + str(self.idx) + ": " + str(self.lexical_item) + " >"
return rep
现在,基于此,我正在尝试定义一个函数,该函数返回对象中对象的一组“路径”,从顶部开始,其中路径定义如下: 路径是一系列对象&lt; O_1,O_2,...,O_n&gt;所有0&lt;我&lt; n,O_ {i + 1}在O_i中。
因此,例如,如果有问题的集合是{A-0,{B-1,A-0} -2} -3,我希望此函数返回以下列表集: {[{A-0,{B-1,A-0} -2} -3,A-0],[{A-0,{B-1,A-0} -2} -3,{B -1,A-0} -2,A-0]}
我尝试了以下递归函数,它在基本情况下运行良好,以及上述情况,但似乎遇到了较大集的问题。我怀疑我在尝试传递函数中的path_set的方式上做错了什么?
def paths(self, so):
"""
:return: all paths to self starting at so
"""
def paths_recursive(self, other, path, path_set = set()):
""" Helper Function """
for x in so.syntactic_object_set:
if x.idx == self.idx:
path += [self]
path_set.update([tuple(path)])
elif type(x) != Singleton:
if x.contains(self):
path1 = path.copy() # Need to make a copy in case both daughters contain self
path1 += [x]
path_set.update(paths_recursive(self, x, path1, path_set))
return path_set
if so.contains(self) == False:
return {}
elif so.idx == self.idx:
return {self}
else:
return paths_recursive(self, so, list([so]))