2014-02-18 90 views
1

我对编程有点不熟悉,并且对返回函数的工作原理有些困惑。我正在尝试编写一个将函数映射到嵌套列表的元素的程序。变量级别表示列表中嵌套级别的次数。目前,我可以得到程序通过打印我的最终映射列表,totlist工作:从Python中的递归函数返回

def map_nested(listbasket, function, levels): #listbasket is the list that contains lists 
    totlist=[] #this list will store the list after the function has been mapped to it 
    for listelement in listbasket: 

     if levels<=2: #once we get to the level that just contains a list of lists 
      newlist=list(map(function,listelement)) 
      totlist.append(newlist) #add to final mapped list 

     else: 
      map_nested(listelement, function, levels-1) #recursively call for next level 
    print(totlist) 

map_nested([[[1,2],[3,4]],[[5,6],[7,8]]], math.sqrt, 3) # my test function 

相反,我想要的东西,返回totlist,但我无法弄清楚如何做到这一点。每次我尝试返回它时,它都会返回一个空列表或列表的一部分。我觉得我已经尝试了我能想到的每种回报配置。

+0

是否尝试用'''return totlist'''替换'''print(totlist)''? – bj0

+0

我做到了。它重新调整了[] – Beth

+0

你说它打印正确,如果你真的做了替换,它将返回它正在打印的内容。您必须记住将该返回值分配给呼叫站点上的变量并将其打印出来。 – bj0

回答

2

这将工作:

import math 

def map_nested(listbasket, function, levels): #listbasket is the list that contains lists 
    totlist=[] #this list will store the list after the function has been mapped to it 
    for listelement in listbasket: 

     if levels<=2: #once we get to the level that just contains a list of lists 
      newlist=list(map(function,listelement)) 
      totlist.append(newlist) #add to final mapped list 
     else: 
      totlist.append(map_nested(listelement, function, levels-1)) 
    return totlist 

map_nested([[[1,2],[3,4]],[[5,6],[7,8]]], math.sqrt, 3) # my test function 

或稍微整洁的解决方案:

import math 

def map_nested(input, function): 
    if type(input) is list: 
     return [map_nested(e, function) for e in input] 
    else: 
     return function(input) 

print map_nested([[[1,2],[3,4]],[[5,6],[7,8]]], math.sqrt) 

这是递归应用map_nested方法在层次结构中的每个列表。当递归到达列表中的元素时,它将应用原始调用中提供的函数。

请注意,这适用于任意深度嵌套的列表,也适用于不平衡的嵌套列表(例如,[1, 2, [3, 4]])。

+0

谢谢,这正是我一直在寻找的。但是,我不太清楚我是否理解它是如何工作的。看起来追加函数最终会多次添加元素。你能解释一下这里发生了什么吗? – Beth

0

我会让totlist参数:

def map_nested(listbasket, function, levels, totlist=None): 
    if totlist is None: 
     totlist = [] 
    for listelement in listbasket: 
     if levels <= 2: 
      newlist = list(map(function, listelement)) 
      totlist.append(newlist) #add to final mapped list 
     else: 
      map_nested(listelement, function, levels-1, totlist) 
    return totlist 

现在:

>>> map_nested([[[1,2],[3,4]],[[5,6],[7,8]]], math.sqrt, 3) 
[[1.0, 1.4142135623730951], 
[1.7320508075688772, 2.0], 
[2.23606797749979, 2.449489742783178], 
[2.6457513110645907, 2.8284271247461903]] 

如果你想简化(不手动传递水平)和扁平巢,当您去,是这样的:

def map_nested_2(lst, f, out=None): 
    if out is None: 
     out = [] 
    for item in lst: 
     if isinstance(item, list): 
      map_nested_2(item, f, out) 
     else: 
      out.append(f(item)) 
    return out 

会给:

[1.0, 1.4142135623730951, 1.7320508075688772, 2.0, 2.23606797749979, 
2.449489742783178, 2.6457513110645907, 2.8284271247461903] 
+0

哦,很酷,也可以。谢谢! – Beth