Python论坛  - 讨论区

标题:[python-chinese] 取中值

2006年07月12日 星期三 22:27

linda.s samrobertsmith at gmail.com
Wed Jul 12 22:27:58 HKT 2006

我想把
[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:

- Hide quoted text -
> 看了你最后的这个代码,是不是四个值分别被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()

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年07月13日 星期四 08:31

limon lin limon7 at gmail.com
Thu Jul 13 08:31:22 HKT 2006

看起来像层次遍历

在06-7-12,linda. s <samrobertsmith at gmail.com> 写道:
>
> 我想把
> [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:
>
> - Hide quoted text -
> > 看了你最后的这个代码,是不是四个值分别被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()
>
> _______________________________________________
> 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/20060713/baf1249b/attachment-0001.html

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年07月13日 星期四 10:11

lubiao lubiao.py at gmail.com
Thu Jul 13 10:11:35 HKT 2006

  def buildMidTree(self):
    klen = len(self.key)
    assert(0 != klen)

    if len(self.key[:klen / 2]):
      self.left = BinaryTree(self.key[:klen / 2])
      self.left.buildMidTree()
    if len(self.key[klen / 2 + 1:]):
      self.right= BinaryTree(self.key[klen / 2 + 1:])
      self.right.buildMidTree()

    self.key = self.key[klen/2]



if __name__ == '__main__':
   t=BinaryTree([11, 12, 13, 14, 15, 16, 17, 18, 19])
   t.buildMidTree()
   t.printTree()

输出结果
15
13
12
11
14
18
17
16
19

因为 printTree 是按深度优先遍历的,如果按 广度优先遍历,就可以得到你要的结果,


On 7/12/06, linda. s <samrobertsmith at gmail.com> wrote:
>
> 我想把
> [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:
>
> - Hide quoted text -
> > 看了你最后的这个代码,是不是四个值分别被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()
>
> _______________________________________________
> 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/20060713/7168c7b8/attachment-0001.htm

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年07月13日 星期四 16:12

linda.s samrobertsmith at gmail.com
Thu Jul 13 16:12:38 HKT 2006

怎么样广度优先遍历?
On 7/12/06, lubiao <lubiao.py at gmail.com> wrote:
>
>   def buildMidTree(self):
>     klen = len(self.key)
>     assert(0 != klen)
>
>     if len(self.key[:klen / 2]):
>       self.left = BinaryTree(self.key[:klen / 2])
>       self.left.buildMidTree()
>     if len( self.key[klen / 2 + 1:]):
>       self.right= BinaryTree(self.key[klen / 2 + 1:])
>       self.right.buildMidTree()
>
>     self.key = self.key[klen/2]
>
>
>
> if __name__ == '__main__':
>    t=BinaryTree([11, 12, 13, 14, 15, 16, 17, 18, 19])
>    t.buildMidTree()
>    t.printTree()
>
> 输出结果
> 15
> 13
> 12
> 11
> 14
> 18
> 17
> 16
> 19
>
> 因为 printTree 是按深度优先遍历的,如果按 广度优先遍历,就可以得到你要的结果,
>
>
> On 7/12/06, linda. s <samrobertsmith at gmail.com> wrote:
> >
> 我想把
> [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:
>
> - Hide quoted text -
> >
> 看了你最后的这个代码,是不是四个值分别被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()
>
> _______________________________________________
>
> 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
>
>

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年07月13日 星期四 17:23

lubiao lubiao.py at gmail.com
Thu Jul 13 17:23:56 HKT 2006

On 7/13/06, linda. s <samrobertsmith at gmail.com> wrote:

> 怎么样广度优先遍历?



  def printTree2(self):
    ls = [self]
    while (ls):
      node = ls.pop(0)
      print node.key
      if node.left:
        ls.append(node.left)
      if node.right:
       ls.append(node.right)


需要一个队列(代码中是 ls) 来实现

if __name__ == '__main__':
   t=BinaryTree([11, 12, 13, 14, 15, 16, 17, 18, 19])
   t.buildMidTree()
   t.printTree()
   print '***'
   t.printTree2()

输出结果

E:\linda.py
15
13
12
11
14
18
17
16
19
***
15
13
18
12
14
17
19
11
16
跟你要的结果有些不同
[11, 12, 13, 14]
4 / 2 = 2
所以 当链表里有 4 个元素时, 认为中间的是   index 为2 的那个,就是 第3个元素。
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060713/f639a315/attachment.htm

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年07月13日 星期四 18:26

linda.s samrobertsmith at gmail.com
Thu Jul 13 18:26:56 HKT 2006

如果要知道多深(几层)呢?

On 7/13/06, lubiao <lubiao.py at gmail.com> wrote:
>
>
> On 7/13/06, linda. s <samrobertsmith at gmail.com> wrote:
> > 怎么样广度优先遍历?
>
>
>   def printTree2(self):
>     ls = [self]
>     while (ls):
>       node = ls.pop(0)
>       print node.key
>       if node.left:
>         ls.append(node.left)
>       if node.right:
>        ls.append(node.right )
>
>
> 需要一个队列(代码中是 ls) 来实现
>
> if __name__ == '__main__':
>    t=BinaryTree([11, 12, 13, 14, 15, 16, 17, 18, 19])
>    t.buildMidTree()
>    t.printTree()
>    print '***'
>    t.printTree2()
>
> 输出结果
>
>
> E:\linda.py
>
> 15
> 13
> 12
> 11
> 14
> 18
> 17
> 16
> 19
> ***
> 15
> 13
> 18
> 12
> 14
> 17
> 19
> 11
> 16
>
> 跟你要的结果有些不同
> [11, 12, 13, 14]
> 4 / 2 = 2
> 所以 当链表里有 4 个元素时, 认为中间的是   index 为2 的那个,就是 第3个元素。
>
> _______________________________________________
> 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
>
>

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年07月13日 星期四 20:35

lubiao lubiao.py at gmail.com
Thu Jul 13 20:35:31 HKT 2006

  def getDepth(self):
    lDepth = 0
    rDepth = 0
    if self.left:
      lDepth = self.left.getDepth()
    if self.right:
      rDepth = self.right.getDepth()
    return max(lDepth, rDepth) + 1



On 7/13/06, linda. s <samrobertsmith at gmail.com> wrote:
>
> 如果要知道多深(几层)呢?
>
> On 7/13/06, lubiao <lubiao.py at gmail.com> wrote:
> >
> >
> > On 7/13/06, linda. s <samrobertsmith at gmail.com> wrote:
> > > 怎么样广度优先遍历?
> >
> >
> >   def printTree2(self):
> >     ls = [self]
> >     while (ls):
> >       node = ls.pop(0)
> >       print node.key
> >       if node.left:
> >         ls.append(node.left)
> >       if node.right:
> >        ls.append(node.right )
> >
> >
> > 需要一个队列(代码中是 ls) 来实现
> >
> > if __name__ == '__main__':
> >    t=BinaryTree([11, 12, 13, 14, 15, 16, 17, 18, 19])
> >    t.buildMidTree()
> >    t.printTree()
> >    print '***'
> >    t.printTree2()
> >
> > 输出结果
> >
> >
> > E:\linda.py
> >
> > 15
> > 13
> > 12
> > 11
> > 14
> > 18
> > 17
> > 16
> > 19
> > ***
> > 15
> > 13
> > 18
> > 12
> > 14
> > 17
> > 19
> > 11
> > 16
> >
> > 跟你要的结果有些不同
> > [11, 12, 13, 14]
> > 4 / 2 = 2
> > 所以 当链表里有 4 个元素时, 认为中间的是   index 为2 的那个,就是 第3个元素。
> >
> > _______________________________________________
> > 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 --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060713/337e42eb/attachment-0001.html

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号