其他分享
首页 > 其他分享> > 偶遇递归树

偶遇递归树

作者:互联网

递归树

st = "aaa-kkk-(bbbb#llll-ewwww#ccc-hhhh#yyyy-(ddd-ggg#eee))"

class AAAA:
    def str_dict(self,data):
        # 数据处理
        data_dict = {}
        if type(data) == str:
            data_dict['ddt'] = data
            return data_dict
        else:
            data_list = []
            for i in data:
                data_dict['ddt'] =i
                data_list.append(data_dict)
                data_dict={}
            return data_list
    def fenge(self, lis1, l):
        # 递归树的深层递归方法
        print(lis1)
        in1 = lis1.index("#")
        in2 = lis1.index("(")
        if in1 < in2:
            lis2 = lis1.split("#", 1)
            if "-" in lis2[0]:
                lis3 = lis2[0].split("-")
                dd = self.str_dict(lis3)
                l.append(dd)
            else:
                dd = self.str_dict([lis2[0]])
                l.append(dd)

            self.fenge(lis2[1], l)
        else:
            l.append(self.dd(lis1, []))

    def dd(self, st, l):
        # 平层次递归方法
        if "(" not in st:
            if "#" not in st:
                lis3 = st.split("-")
                return lis3
            else:
                liswww = st.split("#")
                for item in liswww:
                    dd = self.str_dict(item.split('-'))
                    l.append(dd)
        else:
            lis = st.split("(", 1)
            #  lis= "bbbb-sss#ccc-hhhh#kkk-(ddd-ggg#eee))"
            ll = self.str_dict(item for item in lis[0].split("-"))
            for i in ll:

                l.append(i)
            print(lis)
            if '#' in lis[1]:
                if "(" in lis[1]:
                    self.fenge(lis[1], l)
                else:
                    liswww = lis[1].split("#")
                    for item in liswww:
                        dd = self.str_dict(item.split('-'))
                        l.append(dd)
            else:
                l.append(lis[1].split("-"))
            return l


a = AAAA()
d = a.dd(st, [])
print(d)

标签:递归,偶遇,self,dict,split,lis,data,dd
来源: https://www.cnblogs.com/mqhpy/p/13365938.html