2006年07月11日 星期二 22:14
用如下代码, >>> t=BinaryTree('Capricorn') >>> t.addNode('Aquarius') >>> t.addNode('Pices') >>> t.addNode('Cancer') >>> t.printTree() Capricorn Aquarius Cancer Pices 有什么办法把 printTree 里的print 转化为list吗? 也就是:['Capricorn','Aquarius','Cancer','Pices'] class BinaryTree: def __init__(self, key, left=None, right=None, parent=None): self.key = key self.left = left self.right = right self.parent = parent def addNode(self,key): """Add a node in the proper location.""" if key < self.key: if self.left: self.left.addNode(key) else: self.left = BinaryTree(key, parent=self) elif key > self.key: if self.right: self.right.addNode(key) else: self.right = BinaryTree(key, parent=self) def printTree(self): print self.key if self.left: self.left.printTree() if self.right: self.right.printTree()
2006年07月11日 星期二 22:22
On 7/11/06, linda. s <samrobertsmith at gmail.com> wrote: > 用如下代码, > >>> t=BinaryTree('Capricorn') > >>> t.addNode('Aquarius') > >>> t.addNode('Pices') > >>> t.addNode('Cancer') > >>> t.printTree() > Capricorn > Aquarius > Cancer > Pices > > 有什么办法把 printTree 里的print 转化为list吗? > 也就是:['Capricorn','Aquarius','Cancer','Pices'] > > class BinaryTree: > > def __init__(self, key, left=None, right=None, parent=None): > self.key = key > self.left = left > self.right = right > self.parent = parent > > def addNode(self,key): > """Add a node in the proper location.""" > if key < self.key: > if self.left: > self.left.addNode(key) > else: > self.left = BinaryTree(key, parent=self) > elif key > self.key: > if self.right: > self.right.addNode(key) > else: > self.right = BinaryTree(key, parent=self) > > def printTree(self): > print self.key > if self.left: > self.left.printTree() > if self.right: > self.right.printTree() > 把printTree改造一下不就行了嘛,既然可以打印,那就可以保存啊。 -- I like python! My Blog: http://www.donews.net/limodou My Django Site: http://www.djangocn.org NewEdit Maillist: http://groups.google.com/group/NewEdit
2006年07月11日 星期二 22:44
On 7/11/06, limodou <limodou at gmail.com> wrote: > On 7/11/06, linda. s <samrobertsmith at gmail.com> wrote: > > 用如下代码, > > >>> t=BinaryTree('Capricorn') > > >>> t.addNode('Aquarius') > > >>> t.addNode('Pices') > > >>> t.addNode('Cancer') > > >>> t.printTree() > > Capricorn > > Aquarius > > Cancer > > Pices > > > > 有什么办法把 printTree 里的print 转化为list吗? > > 也就是:['Capricorn','Aquarius','Cancer','Pices'] > > > > class BinaryTree: > > > > def __init__(self, key, left=None, right=None, parent=None): > > self.key = key > > self.left = left > > self.right = right > > self.parent = parent > > > > def addNode(self,key): > > """Add a node in the proper location.""" > > if key < self.key: > > if self.left: > > self.left.addNode(key) > > else: > > self.left = BinaryTree(key, parent=self) > > elif key > self.key: > > if self.right: > > self.right.addNode(key) > > else: > > self.right = BinaryTree(key, parent=self) > > > > def printTree(self): > > print self.key > > if self.left: > > self.left.printTree() > > if self.right: > > self.right.printTree() > > > 把printTree改造一下不就行了嘛,既然可以打印,那就可以保存啊 试过,结果只给了我第一个值'Capricorn' def tree2list(self): b=[] b.append(self.key) if self.left: self.left.tree2list() if self.right: self.right.tree2list() return b
2006年07月11日 星期二 22:49
On 7/11/06, linda. s <samrobertsmith at gmail.com> wrote: > On 7/11/06, limodou <limodou at gmail.com> wrote: > > On 7/11/06, linda. s <samrobertsmith at gmail.com> wrote: > > > 用如下代码, > > > >>> t=BinaryTree('Capricorn') > > > >>> t.addNode('Aquarius') > > > >>> t.addNode('Pices') > > > >>> t.addNode('Cancer') > > > >>> t.printTree() > > > Capricorn > > > Aquarius > > > Cancer > > > Pices > > > > > > 有什么办法把 printTree 里的print 转化为list吗? > > > 也就是:['Capricorn','Aquarius','Cancer','Pices'] > > > > > > class BinaryTree: > > > > > > def __init__(self, key, left=None, right=None, parent=None): > > > self.key = key > > > self.left = left > > > self.right = right > > > self.parent = parent > > > > > > def addNode(self,key): > > > """Add a node in the proper location.""" > > > if key < self.key: > > > if self.left: > > > self.left.addNode(key) > > > else: > > > self.left = BinaryTree(key, parent=self) > > > elif key > self.key: > > > if self.right: > > > self.right.addNode(key) > > > else: > > > self.right = BinaryTree(key, parent=self) > > > > > > def printTree(self): > > > print self.key > > > if self.left: > > > self.left.printTree() > > > if self.right: > > > self.right.printTree() > > > > > 把printTree改造一下不就行了嘛,既然可以打印,那就可以保存啊 > > 试过,结果只给了我第一个值'Capricorn' > def tree2list(self): > b=[] > b.append(self.key) > if self.left: > self.left.tree2list() > if self.right: > self.right.tree2list() > return b > 把b设成一个实例变量即可,你这样每次调用都会生成一个新的变量,因此只能存住一个值。 -- I like python! My Blog: http://www.donews.net/limodou My Django Site: http://www.djangocn.org NewEdit Maillist: http://groups.google.com/group/NewEdit
2006年07月11日 星期二 23:15
把b作为BinaryTree的类成员变量声明
2006年07月12日 星期三 05:29
On 7/11/06, limodou <limodou at gmail.com> wrote: > On 7/11/06, linda. s <samrobertsmith at gmail.com> wrote: > > On 7/11/06, limodou <limodou at gmail.com> wrote: > > > On 7/11/06, linda. s <samrobertsmith at gmail.com> wrote: > > > > 用如下代码, > > > > >>> t=BinaryTree('Capricorn') > > > > >>> t.addNode('Aquarius') > > > > >>> t.addNode('Pices') > > > > >>> t.addNode('Cancer') > > > > >>> t.printTree() > > > > Capricorn > > > > Aquarius > > > > Cancer > > > > Pices > > > > > > > > 有什么办法把 printTree 里的print 转化为list吗? > > > > 也就是:['Capricorn','Aquarius','Cancer','Pices'] > > > > > > > > class BinaryTree: > > > > > > > > def __init__(self, key, left=None, right=None, parent=None): > > > > self.key = key > > > > self.left = left > > > > self.right = right > > > > self.parent = parent > > > > > > > > def addNode(self,key): > > > > """Add a node in the proper location.""" > > > > if key < self.key: > > > > if self.left: > > > > self.left.addNode(key) > > > > else: > > > > self.left = BinaryTree(key, parent=self) > > > > elif key > self.key: > > > > if self.right: > > > > self.right.addNode(key) > > > > else: > > > > self.right = BinaryTree(key, parent=self) > > > > > > > > def printTree(self): > > > > print self.key > > > > if self.left: > > > > self.left.printTree() > > > > if self.right: > > > > self.right.printTree() > > > > > > > 把printTree改造一下不就行了嘛,既然可以打印,那就可以保存啊 > > > > 试过,结果只给了我第一个值'Capricorn' > > def tree2list(self): > > b=[] > > b.append(self.key) > > if self.left: > > self.left.tree2list() > > if self.right: > > self.right.tree2list() > > return b > > > 把b设成一个实例变量即可,你这样每次调用都会生成一个新的变量,因此只能存住一个值。 我还是不明白.新手上路,多多原谅.
2006年07月12日 星期三 06:02
On 7/11/06, linda. s <samrobertsmith at gmail.com> wrote: > On 7/11/06, limodou <limodou at gmail.com> wrote: > > On 7/11/06, linda. s <samrobertsmith at gmail.com> wrote: > > > On 7/11/06, limodou <limodou at gmail.com> wrote: > > > > On 7/11/06, linda. s <samrobertsmith at gmail.com> wrote: > > > > > 用如下代码, > > > > > >>> t=BinaryTree('Capricorn') > > > > > >>> t.addNode('Aquarius') > > > > > >>> t.addNode('Pices') > > > > > >>> t.addNode('Cancer') > > > > > >>> t.printTree() > > > > > Capricorn > > > > > Aquarius > > > > > Cancer > > > > > Pices > > > > > > > > > > 有什么办法把 printTree 里的print 转化为list吗? > > > > > 也就是:['Capricorn','Aquarius','Cancer','Pices'] > > > > > > > > > > class BinaryTree: > > > > > > > > > > def __init__(self, key, left=None, right=None, parent=None): > > > > > self.key = key > > > > > self.left = left > > > > > self.right = right > > > > > self.parent = parent > > > > > > > > > > def addNode(self,key): > > > > > """Add a node in the proper location.""" > > > > > if key < self.key: > > > > > if self.left: > > > > > self.left.addNode(key) > > > > > else: > > > > > self.left = BinaryTree(key, parent=self) > > > > > elif key > self.key: > > > > > if self.right: > > > > > self.right.addNode(key) > > > > > else: > > > > > self.right = BinaryTree(key, parent=self) > > > > > > > > > > def printTree(self): > > > > > print self.key > > > > > if self.left: > > > > > self.left.printTree() > > > > > if self.right: > > > > > self.right.printTree() > > > > > > > > > 把printTree改造一下不就行了嘛,既然可以打印,那就可以保存啊 > > > > > > 试过,结果只给了我第一个值'Capricorn' > > > def tree2list(self): > > > b=[] > > > b.append(self.key) > > > if self.left: > > > self.left.tree2list() > > > if self.right: > > > self.right.tree2list() > > > return b > > > > > 把b设成一个实例变量即可,你这样每次调用都会生成一个新的变量,因此只能存住一个值。 > > 我还是不明白.新手上路,多多原谅. > 我做了这些改动: 加了self.list= [], def __init__(self, key, left=None, right=None, parent=None): self.key = key self.left = left self.right = right self.parent = parent self.list= [] def tree2list(self): self.list.append(self.key) if self.left: self.left.tree2list() if self.right: self.right.tree2list() return self.list 但是结果还是一样,只给了我第一个值'Capricorn'.
2006年07月12日 星期三 07:33
看了你最后的这个代码,是不是四个值分别被return,但是只有最后一次return的Capricorn返回给了调用函数。 如果把ruturn改成yield,应该就差不多了吧。 我写的tree2list函数是 def tree2list(self): yield self.key if self.left: for foo in self.left.tree2list(): yield foo if self.right: for foo in self.right.tree2list(): yield foo 调用代码是 test = [item for item in t.tree2list()] python的缩进,好像在邮件列表里面有问题啊...我把代码放在附件里面了。 运行结果是 Capricorn Aquarius Cancer Pices test= ['Capricorn', 'Aquarius', 'Cancer', 'Pices'] On 7/12/06, linda. s <samrobertsmith at gmail.com> wrote: > On 7/11/06, linda. s <samrobertsmith at gmail.com> wrote: > > On 7/11/06, limodou <limodou at gmail.com> wrote: > > > On 7/11/06, linda. s <samrobertsmith at gmail.com> wrote: > > > > On 7/11/06, limodou <limodou at gmail.com> wrote: > > > > > On 7/11/06, linda. s <samrobertsmith at gmail.com> wrote: > > > > > > 用如下代码, > > > > > > >>> t=BinaryTree('Capricorn') > > > > > > >>> t.addNode('Aquarius') > > > > > > >>> t.addNode('Pices') > > > > > > >>> t.addNode('Cancer') > > > > > > >>> t.printTree() > > > > > > Capricorn > > > > > > Aquarius > > > > > > Cancer > > > > > > Pices > > > > > > > > > > > > 有什么办法把 printTree 里的print 转化为list吗? > > > > > > 也就是:['Capricorn','Aquarius','Cancer','Pices'] > > > > > > > > > > > > class BinaryTree: > > > > > > > > > > > > def __init__(self, key, left=None, right=None, parent=None): > > > > > > self.key = key > > > > > > self.left = left > > > > > > self.right = right > > > > > > self.parent = parent > > > > > > > > > > > > def addNode(self,key): > > > > > > """Add a node in the proper location.""" > > > > > > if key < self.key: > > > > > > if self.left: > > > > > > self.left.addNode(key) > > > > > > else: > > > > > > self.left = BinaryTree(key, parent=self) > > > > > > elif key > self.key: > > > > > > if self.right: > > > > > > self.right.addNode(key) > > > > > > else: > > > > > > self.right = BinaryTree(key, parent=self) > > > > > > > > > > > > def printTree(self): > > > > > > print self.key > > > > > > if self.left: > > > > > > self.left.printTree() > > > > > > if self.right: > > > > > > self.right.printTree() > > > > > > > > > > > 把printTree改造一下不就行了嘛,既然可以打印,那就可以保存啊 > > > > > > > > 试过,结果只给了我第一个值'Capricorn' > > > > def tree2list(self): > > > > b=[] > > > > b.append(self.key) > > > > if self.left: > > > > self.left.tree2list() > > > > if self.right: > > > > self.right.tree2list() > > > > return b > > > > > > > 把b设成一个实例变量即可,你这样每次调用都会生成一个新的变量,因此只能存住一个值。 > > > > 我还是不明白.新手上路,多多原谅. > > > 我做了这些改动: > 加了self.list= [], > def __init__(self, key, left=None, right=None, parent=None): > self.key = key > self.left = left > self.right = right > self.parent = parent > self.list= [] > def tree2list(self): > self.list.append(self.key) > if self.left: > self.left.tree2list() > if self.right: > self.right.tree2list() > return self.list > 但是结果还是一样,只给了我第一个值'Capricorn'. > > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > -------------- next part -------------- #!/usr/bin/env python class BinaryTree: def __init__(self, key, left=None, right=None, parent=None): self.key = key self.left = left self.right = right self.parent = parent def addNode(self,key): """Add a node in the proper location.""" if key < self.key: if self.left: self.left.addNode(key) else: self.left = BinaryTree(key, parent=self) elif key > self.key: if self.right: self.right.addNode(key) else: self.right = BinaryTree(key, parent=self) def printTree(self): print self.key if self.left: self.left.printTree() if self.right: self.right.printTree() def tree2list(self): yield self.key if self.left: for foo in self.left.tree2list(): yield foo if self.right: for foo in self.right.tree2list(): yield foo def main(): t=BinaryTree('Capricorn') t.addNode('Aquarius') t.addNode('Pices') t.addNode('Cancer') t.printTree() test = [item for item in t.tree2list()] print 'test= ', test if __name__ == "__main__": main()
2006年07月12日 星期三 10:08
class BinaryTree: def __init__(self, key, left=None, right=None, parent=None): self.key = key self.left = left self.right = right self.parent = parent def addNode(self,key): """Add a node in the proper location.""" if key < self.key: if self.left: self.left.addNode(key) else: self.left = BinaryTree(key, parent=self) elif key > self.key: if self.right: self.right.addNode(key) else: self.right = BinaryTree(key, parent=self) def printTree(self): print self.key if self.left: self.left.printTree() if self.right: self.right.printTree() def createKeyList(self, outLs): outLs.append(self.key) if self.left: self.left.createKeyList(outLs) if self.right: self.right.createKeyList(outLs) if __name__ == '__main__': t=BinaryTree('Capricorn') t.addNode('Aquarius') t.addNode('Pices') t.addNode('Cancer') t.printTree() ls = [] t.createKeyList(ls) print ls On 7/11/06, linda. s <samrobertsmith at gmail.com> wrote: > > 用如下代码, > >>> t=BinaryTree('Capricorn') > >>> t.addNode('Aquarius') > >>> t.addNode('Pices') > >>> t.addNode('Cancer') > >>> t.printTree() > Capricorn > Aquarius > Cancer > Pices > > 有什么办法把 printTree 里的print 转化为list吗? > 也就是:['Capricorn','Aquarius','Cancer','Pices'] > > class BinaryTree: > > def __init__(self, key, left=None, right=None, parent=None): > self.key = key > self.left = left > self.right = right > self.parent = parent > > def addNode(self,key): > """Add a node in the proper location.""" > if key < self.key: > if self.left: > self.left.addNode(key) > else: > self.left = BinaryTree(key, parent=self) > elif key > self.key: > if self.right: > self.right.addNode(key) > else: > self.right = BinaryTree(key, parent=self) > > def printTree(self): > print self.key > if self.left: > self.left.printTree() > if self.right: > self.right.printTree() > > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060712/67e84a98/attachment.html
2006年07月12日 星期三 11:56
> > > 我做了这些改动: > 加了self.list= [], > def __init__(self, key, left=None, right=None, parent=None): > self.key = key > self.left = left > self.right = right > self.parent = parent > self.list= [] > def tree2list(self): > self.list.append(self.key) > if self.left: > self.left.tree2list() > if self.right: > self.right.tree2list() > return self.list > 但是结果还是一样,只给了我第一个值'Capricorn'. 因为你这棵树拥有了许多实例 self.list.append(self.key)这句都是往不同实例的list插入(用内置型名做Name好像不好) 你可以把递归函数改成 def tree2list(self,alist): 把list从外面传进去 -- If U can see it, then U can do it If U just believe it, there's nothing to it I believe U can fly JetPort:80 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060712/8f189125/attachment.html
2006年07月12日 星期三 14:29
有什么办法把 test = [item for item in t.tree2list()] 能直接放进类里,也就是, t.tree2list()直接就给['Capricorn', 'Aquarius', 'Cancer', 'Pices'] On 7/11/06, imcs ee <imcsee at gmail.com> wrote: > 看了你最后的这个代码,是不是四个值分别被return,但是只有最后一次return的Capricorn返回给了调用函数。 > 如果把ruturn改成yield,应该就差不多了吧。 > 我写的tree2list函数是 > def tree2list(self): > yield self.key > if self.left: > for foo in self.left.tree2list(): > yield foo > if self.right: > for foo in self.right.tree2list(): > yield foo > 调用代码是 > test = [item for item in t.tree2list()] > python的缩进,好像在邮件列表里面有问题啊...我把代码放在附件里面了。 > 运行结果是 > Capricorn > Aquarius > Cancer > Pices > test= ['Capricorn', 'Aquarius', 'Cancer', 'Pices'] > > On 7/12/06, linda. s <samrobertsmith at gmail.com> wrote: > > On 7/11/06, linda. s <samrobertsmith at gmail.com> wrote: > > > On 7/11/06, limodou <limodou at gmail.com> wrote: > > > > On 7/11/06, linda. s <samrobertsmith at gmail.com> wrote: > > > > > On 7/11/06, limodou <limodou at gmail.com> wrote: > > > > > > On 7/11/06, linda. s <samrobertsmith at gmail.com> wrote: > > > > > > > 用如下代码, > > > > > > > >>> t=BinaryTree('Capricorn') > > > > > > > >>> t.addNode('Aquarius') > > > > > > > >>> t.addNode('Pices') > > > > > > > >>> t.addNode('Cancer') > > > > > > > >>> t.printTree() > > > > > > > Capricorn > > > > > > > Aquarius > > > > > > > Cancer > > > > > > > Pices > > > > > > > > > > > > > > 有什么办法把 printTree 里的print 转化为list吗? > > > > > > > 也就是:['Capricorn','Aquarius','Cancer','Pices'] > > > > > > > > > > > > > > class BinaryTree: > > > > > > > > > > > > > > def __init__(self, key, left=None, right=None, parent=None): > > > > > > > self.key = key > > > > > > > self.left = left > > > > > > > self.right = right > > > > > > > self.parent = parent > > > > > > > > > > > > > > def addNode(self,key): > > > > > > > """Add a node in the proper location.""" > > > > > > > if key < self.key: > > > > > > > if self.left: > > > > > > > self.left.addNode(key) > > > > > > > else: > > > > > > > self.left = BinaryTree(key, parent=self) > > > > > > > elif key > self.key: > > > > > > > if self.right: > > > > > > > self.right.addNode(key) > > > > > > > else: > > > > > > > self.right = BinaryTree(key, parent=self) > > > > > > > > > > > > > > def printTree(self): > > > > > > > print self.key > > > > > > > if self.left: > > > > > > > self.left.printTree() > > > > > > > if self.right: > > > > > > > self.right.printTree() > > > > > > > > > > > > > 把printTree改造一下不就行了嘛,既然可以打印,那就可以保存啊 > > > > > > > > > > 试过,结果只给了我第一个值'Capricorn' > > > > > def tree2list(self): > > > > > b=[] > > > > > b.append(self.key) > > > > > if self.left: > > > > > self.left.tree2list() > > > > > if self.right: > > > > > self.right.tree2list() > > > > > return b > > > > > > > > > 把b设成一个实例变量即可,你这样每次调用都会生成一个新的变量,因此只能存住一个值。 > > > > > > 我还是不明白.新手上路,多多原谅. > > > > > 我做了这些改动: > > 加了self.list= [], > > def __init__(self, key, left=None, right=None, parent=None): > > self.key = key > > self.left = left > > self.right = right > > self.parent = parent > > self.list= [] > > def tree2list(self): > > self.list.append(self.key) > > if self.left: > > self.left.tree2list() > > if self.right: > > self.right.tree2list() > > return self.list > > 但是结果还是一样,只给了我第一个值'Capricorn'. > > > > _______________________________________________ > > python-chinese > > Post: send python-chinese at lists.python.cn > > Subscribe: send subscribe to python-chinese-request at lists.python.cn > > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > > > > > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > >
2006年07月12日 星期三 14:38
On 7/12/06, linda. s <samrobertsmith at gmail.com> wrote: > > 有什么办法把 > test = [item for item in t.tree2list()] > 能直接放进类里,也就是, > t.tree2list()直接就给['Capricorn', 'Aquarius', 'Cancer', 'Pices'] class BinaryTree: def __init__(self, key, left=None, right=None, parent=None): self.key = key self.left = left self.right = right self.parent = parent def addNode(self,key): """Add a node in the proper location.""" if key < self.key: if self.left: self.left.addNode(key) else: self.left = BinaryTree(key, parent=self) elif key > self.key: if self.right: self.right.addNode(key) else: self.right = BinaryTree(key, parent=self) def printTree(self): print self.key if self.left: self.left.printTree() if self.right: self.right.printTree() def createKeyList(self, outLs): outLs.append(self.key) if self.left: self.left.createKeyList(outLs) if self.right: self.right.createKeyList(outLs) def printKeyList(self): ls = [] self.createKeyList(ls) print ls if __name__ == '__main__': t=BinaryTree('Capricorn') t.addNode('Aquarius') t.addNode('Pices') t.addNode('Cancer') t.printTree() ls = [] t.createKeyList(ls) print ls t.printKeyList() -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060712/91b0db9a/attachment.htm
2006年07月12日 星期三 17:27
把原来的 def tree2list(self): yield self.key if self.left: for foo in self.left.tree2list(): yield foo if self.right: for foo in self.right.tree2list(): yield foo 换为 def tree2list(self): return [item for item in self.tree2yield()] def tree2yield(self): yield self.key if self.left: for foo in self.left.tree2list(): yield foo if self.right: for foo in self.right.tree2list(): yield foo 就可以了吧... On 7/12/06, linda. s <samrobertsmith at gmail.com> wrote: > 有什么办法把 > test = [item for item in t.tree2list()] > 能直接放进类里,也就是, > t.tree2list()直接就给['Capricorn', 'Aquarius', 'Cancer', 'Pices'] > > On 7/11/06, imcs ee <imcsee at gmail.com> wrote: > > 看了你最后的这个代码,是不是四个值分别被return,但是只有最后一次return的Capricorn返回给了调用函数。 > > 如果把ruturn改成yield,应该就差不多了吧。 > > 我写的tree2list函数是 > > def tree2list(self): > > yield self.key > > if self.left: > > for foo in self.left.tree2list(): > > yield foo > > if self.right: > > for foo in self.right.tree2list(): > > yield foo > > 调用代码是 > > test = [item for item in t.tree2list()] > > python的缩进,好像在邮件列表里面有问题啊...我把代码放在附件里面了。 > > 运行结果是 > > Capricorn > > Aquarius > > Cancer > > Pices > > test= ['Capricorn', 'Aquarius', 'Cancer', 'Pices'] > > > > On 7/12/06, linda. s <samrobertsmith at gmail.com> wrote: > > > On 7/11/06, linda. s <samrobertsmith at gmail.com> wrote: > > > > On 7/11/06, limodou <limodou at gmail.com> wrote: > > > > > On 7/11/06, linda. s <samrobertsmith at gmail.com> wrote: > > > > > > On 7/11/06, limodou <limodou at gmail.com> wrote: > > > > > > > On 7/11/06, linda. s <samrobertsmith at gmail.com> wrote: > > > > > > > > 用如下代码, > > > > > > > > >>> t=BinaryTree('Capricorn') > > > > > > > > >>> t.addNode('Aquarius') > > > > > > > > >>> t.addNode('Pices') > > > > > > > > >>> t.addNode('Cancer') > > > > > > > > >>> t.printTree() > > > > > > > > Capricorn > > > > > > > > Aquarius > > > > > > > > Cancer > > > > > > > > Pices > > > > > > > > > > > > > > > > 有什么办法把 printTree 里的print 转化为list吗? > > > > > > > > 也就是:['Capricorn','Aquarius','Cancer','Pices'] > > > > > > > > > > > > > > > > class BinaryTree: > > > > > > > > > > > > > > > > def __init__(self, key, left=None, right=None, parent=None): > > > > > > > > self.key = key > > > > > > > > self.left = left > > > > > > > > self.right = right > > > > > > > > self.parent = parent > > > > > > > > > > > > > > > > def addNode(self,key): > > > > > > > > """Add a node in the proper location.""" > > > > > > > > if key < self.key: > > > > > > > > if self.left: > > > > > > > > self.left.addNode(key) > > > > > > > > else: > > > > > > > > self.left = BinaryTree(key, parent=self) > > > > > > > > elif key > self.key: > > > > > > > > if self.right: > > > > > > > > self.right.addNode(key) > > > > > > > > else: > > > > > > > > self.right = BinaryTree(key, parent=self) > > > > > > > > > > > > > > > > def printTree(self): > > > > > > > > print self.key > > > > > > > > if self.left: > > > > > > > > self.left.printTree() > > > > > > > > if self.right: > > > > > > > > self.right.printTree() > > > > > > > > > > > > > > > 把printTree改造一下不就行了嘛,既然可以打印,那就可以保存啊 > > > > > > > > > > > > 试过,结果只给了我第一个值'Capricorn' > > > > > > def tree2list(self): > > > > > > b=[] > > > > > > b.append(self.key) > > > > > > if self.left: > > > > > > self.left.tree2list() > > > > > > if self.right: > > > > > > self.right.tree2list() > > > > > > return b > > > > > > > > > > > 把b设成一个实例变量即可,你这样每次调用都会生成一个新的变量,因此只能存住一个值。 > > > > > > > > 我还是不明白.新手上路,多多原谅. > > > > > > > 我做了这些改动: > > > 加了self.list= [], > > > def __init__(self, key, left=None, right=None, parent=None): > > > self.key = key > > > self.left = left > > > self.right = right > > > self.parent = parent > > > self.list= [] > > > def tree2list(self): > > > self.list.append(self.key) > > > if self.left: > > > self.left.tree2list() > > > if self.right: > > > self.right.tree2list() > > > return self.list > > > 但是结果还是一样,只给了我第一个值'Capricorn'. > > > > > > _______________________________________________ > > > python-chinese > > > Post: send python-chinese at lists.python.cn > > > Subscribe: send subscribe to python-chinese-request at lists.python.cn > > > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > > > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > > > > > > > > > _______________________________________________ > > python-chinese > > Post: send python-chinese at lists.python.cn > > Subscribe: send subscribe to python-chinese-request at lists.python.cn > > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > > > > > > > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > -------------- next part -------------- #!/usr/bin/env python class BinaryTree: def __init__(self, key, left=None, right=None, parent=None): self.key = key self.left = left self.right = right self.parent = parent def addNode(self,key): """Add a node in the proper location.""" if key < self.key: if self.left: self.left.addNode(key) else: self.left = BinaryTree(key, parent=self) elif key > self.key: if self.right: self.right.addNode(key) else: self.right = BinaryTree(key, parent=self) def printTree(self): print self.key if self.left: self.left.printTree() if self.right: self.right.printTree() def tree2list(self): return [item for item in self.tree2yield()] def tree2yield(self): yield self.key if self.left: for foo in self.left.tree2list(): yield foo if self.right: for foo in self.right.tree2list(): yield foo def main(): t=BinaryTree('Capricorn') t.addNode('Aquarius') t.addNode('Pices') t.addNode('Cancer') t.printTree() test = t.tree2list() print 'test= ', test if __name__ == "__main__": main()
2006年07月12日 星期三 22:24
我想把 [11, 12, 13, 14, 15, 16, 17, 18, 19] 变成 [15,12,17,11,13,16,18,14,19]; 也就是每一次取中值,左右再取中值,一直到取完. 不知道怎么样和原来邮件里的tree结合起来? On 7/11/06, imcs ee <imcsee at gmail.com> wrote: > 看了你最后的这个代码,是不是四个值分别被return,但是只有最后一次return的Capricorn返回给了调用函数。 > 如果把ruturn改成yield,应该就差不多了吧。 > 我写的tree2list函数是 > def tree2list(self): > yield self.key > if self.left: > for foo in self.left.tree2list(): > yield foo > if self.right: > for foo in self.right.tree2list(): > yield foo > 调用代码是 > test = [item for item in t.tree2list()] > python的缩进,好像在邮件列表里面有问题啊...我把代码放在附件里面了。 > 运行结果是 > Capricorn > Aquarius > Cancer > Pices > test= ['Capricorn', 'Aquarius', 'Cancer', 'Pices'] > > On 7/12/06, linda. s <samrobertsmith at gmail.com> wrote: > > On 7/11/06, linda. s <samrobertsmith at gmail.com> wrote: > > > On 7/11/06, limodou <limodou at gmail.com> wrote: > > > > On 7/11/06, linda. s <samrobertsmith at gmail.com> wrote: > > > > > On 7/11/06, limodou <limodou at gmail.com> wrote: > > > > > > On 7/11/06, linda. s <samrobertsmith at gmail.com> wrote: > > > > > > > 用如下代码, > > > > > > > >>> t=BinaryTree('Capricorn') > > > > > > > >>> t.addNode('Aquarius') > > > > > > > >>> t.addNode('Pices') > > > > > > > >>> t.addNode('Cancer') > > > > > > > >>> t.printTree() > > > > > > > Capricorn > > > > > > > Aquarius > > > > > > > Cancer > > > > > > > Pices > > > > > > > > > > > > > > 有什么办法把 printTree 里的print 转化为list吗? > > > > > > > 也就是:['Capricorn','Aquarius','Cancer','Pices'] > > > > > > > > > > > > > > class BinaryTree: > > > > > > > > > > > > > > def __init__(self, key, left=None, right=None, parent=None): > > > > > > > self.key = key > > > > > > > self.left = left > > > > > > > self.right = right > > > > > > > self.parent = parent > > > > > > > > > > > > > > def addNode(self,key): > > > > > > > """Add a node in the proper location.""" > > > > > > > if key < self.key: > > > > > > > if self.left: > > > > > > > self.left.addNode(key) > > > > > > > else: > > > > > > > self.left = BinaryTree(key, parent=self) > > > > > > > elif key > self.key: > > > > > > > if self.right: > > > > > > > self.right.addNode(key) > > > > > > > else: > > > > > > > self.right = BinaryTree(key, parent=self) > > > > > > > > > > > > > > def printTree(self): > > > > > > > print self.key > > > > > > > if self.left: > > > > > > > self.left.printTree() > > > > > > > if self.right: > > > > > > > self.right.printTree() > > > > > > > > > > > > > 把printTree改造一下不就行了嘛,既然可以打印,那就可以保存啊 > > > > > > > > > > 试过,结果只给了我第一个值'Capricorn' > > > > > def tree2list(self): > > > > > b=[] > > > > > b.append(self.key) > > > > > if self.left: > > > > > self.left.tree2list() > > > > > if self.right: > > > > > self.right.tree2list() > > > > > return b > > > > > > > > > 把b设成一个实例变量即可,你这样每次调用都会生成一个新的变量,因此只能存住一个值。 > > > > > > 我还是不明白.新手上路,多多原谅. > > > > > 我做了这些改动: > > 加了self.list= [], > > def __init__(self, key, left=None, right=None, parent=None): > > self.key = key > > self.left = left > > self.right = right > > self.parent = parent > > self.list= [] > > def tree2list(self): > > self.list.append(self.key) > > if self.left: > > self.left.tree2list() > > if self.right: > > self.right.tree2list() > > return self.list > > 但是结果还是一样,只给了我第一个值'Capricorn'. > > > > _______________________________________________ > > python-chinese > > Post: send python-chinese at lists.python.cn > > Subscribe: send subscribe to python-chinese-request at lists.python.cn > > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > > > > > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > >
Zeuux © 2025
京ICP备05028076号