递归函数返回列表 [英] Recursive function returning a list

查看:61
本文介绍了递归函数返回列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!


我有一个Python设备具有属性(列表)的对象

称为子项,其中包含其他几个设备。对象。我用这种方式实现了
以实现一种父/子

的关系。


现在我想获得给定设备的所有孩子对象和

认为这是使用递归函数的最佳方式。


这是我到目前为止所得到的:

def getAllChildren(self,children = []):

if self.children:

children.extend(self.children)

for self.children:

child.getAllChildren(儿童)

返回孩子


不幸的是,它没有由于默认参数

children = []仅评估一次,因此预期的工作方式与预期相同。这就是为什么在多次调用这个方法之后,孩子名单变得越来越大,但是我不能想象另一种可能性。


您有什么想法吗?

解决方案

< e9 ************ *@news.t-online.com> ;, Fabian Steiner写道:


这是我到目前为止所得到的:


def getAllChildren(self,children = []):

if self.children:

children.extend(self.children)

for孩子在自己。孩子:

child.getAllChildren(孩子)

返回孩子


不幸的是,它不起作用与预期一样,因为默认参数

children = []仅评估一次。这就是为什么在多次调用这个方法之后,孩子名单变得越来越大,但是我不能想象另一种可能性。


你有什么想法吗?



def get_all_children(self,accumulator = None):

如果累加器为无:

accumulator = list ()

accumulator.extend(self.children)

for self.children:

child.get_all_children(accumulator)

返回累加器


Ciao,

Marc''BlackJack''Rintsch


blockquote>

Fabian Steiner写道:


你好!


我有一个Python设备具有属性(列表)的对象

称为子项,其中包含其他几个设备。对象。我用这种方式实现了
以实现一种父/子

的关系。


现在我想获得给定设备的所有孩子对象和

认为这是使用递归函数的最佳方式。


这是我到目前为止所得到的:

def getAllChildren(self,children = []):

if self.children:

children.extend(self.children)

for self.children:

child.getAllChildren(儿童)

返回孩子


不幸的是,它没有由于默认参数

children = []仅评估一次,因此预期的工作方式与预期相同。这就是为什么在多次调用这个方法之后,孩子名单变得越来越大,但是我不能想象另一种可能性。


你有什么想法吗?



这是一个标准问题,并且有一个标准答案:替换


def getAllChildren(self,children = [] ):


with


def getAllChildren(self,children = None):

如果孩子是无:

孩子= []


这样就为每个不接收

孩子的电话创建了一个新的空列表。论点。否则相同(一次空)列表用于

所有。


问候

史蒂夫

-

Steve Holden +44 150 684 7255 +1 800 494 3119

Holden Web LLC / Ltd http://www.holdenweb.com

Skype:holdenweb http://holdenweb.blogspot.com

最近的Ramblings http://del.icio.us/steve.holden


你有什么想法吗?


你可以使用递归生成器,比如


def genAllChildren(self):

for self.children:

收益孩子

for childchild in child.genAllChildren():

收益childchild


Hello!

I have got a Python "Device" Object which has got a attribute (list)
called children which my contain several other "Device" objects. I
implemented it this way in order to achieve a kind of parent/child
relationship.

Now I would like to get all children of a given "Device" object and
thought that it would be the best way to use recursive function.

This is what I got so far:

def getAllChildren(self, children=[]):
if self.children:
children.extend(self.children)
for child in self.children:
child.getAllChildren(children)
return children

Unfortunately, it doesn''t work like expected since the default parameter
children=[] is evaluated only once. That''s why the children list becomes
larger and larger after calling the method several times but I can''t
think of another possibility.

Do you have any ideas?

解决方案

In <e9*************@news.t-online.com>, Fabian Steiner wrote:

This is what I got so far:

def getAllChildren(self, children=[]):
if self.children:
children.extend(self.children)
for child in self.children:
child.getAllChildren(children)
return children

Unfortunately, it doesn''t work like expected since the default parameter
children=[] is evaluated only once. That''s why the children list becomes
larger and larger after calling the method several times but I can''t
think of another possibility.

Do you have any ideas?

def get_all_children(self, accumulator=None):
if accumulator is None:
accumulator = list()
accumulator.extend(self.children)
for child in self.children:
child.get_all_children(accumulator)
return accumulator

Ciao,
Marc ''BlackJack'' Rintsch


Fabian Steiner wrote:

Hello!

I have got a Python "Device" Object which has got a attribute (list)
called children which my contain several other "Device" objects. I
implemented it this way in order to achieve a kind of parent/child
relationship.

Now I would like to get all children of a given "Device" object and
thought that it would be the best way to use recursive function.

This is what I got so far:

def getAllChildren(self, children=[]):
if self.children:
children.extend(self.children)
for child in self.children:
child.getAllChildren(children)
return children

Unfortunately, it doesn''t work like expected since the default parameter
children=[] is evaluated only once. That''s why the children list becomes
larger and larger after calling the method several times but I can''t
think of another possibility.

Do you have any ideas?

This is a standard question, and has a standard answer: replace

def getAllChildren(self, children=[]):

with

def getAllChildren(self, children=None):
if children is None:
children = []

That way a new empty list is created for each call that receives no
"children" argument. Otherwise the same (once-empty) list is used for
them all.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden


Do you have any ideas?

you could use a recursive generator, like

def genAllChildren(self) :
for child in self.children :
yield child
for childchild in child.genAllChildren() :
yield childchild


这篇关于递归函数返回列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆