2013-04-25 209 views
1

我在python中编写了一个函数式代码,我不明白为什么它返回一个None而不是代码明确生成的正确值。在Python中的函数式编程:不返回正确的值

此脚本的目的是从CSV中获取line.split(',')并重新组合从'{value1,value2,value3}'无意中拆分为'value1,value2 ...'的所有值。值N”。

def reassemble(list_name): 
    def inner_iter(head, tail): 
     if head[0][0] == '{': 
      new_head = [head[0] + ',' + tail[0]] 
      if tail[0][-1] == '}': 
       return [new_head[0][1:-1]], tail[1:] 
      else: 
       inner_iter(new_head, tail[1:]) 

    def outer_iter(corrected_list, head, tail): 
     if tail == []: 
      print corrected_list + head 
      return corrected_list + head 
     else: 
      if head[0][0] == '{': 
       head, tail = inner_iter(head, tail) 
       outer_iter(corrected_list + head, [tail[0]], tail[1:]) 

      else: 
       outer_iter(corrected_list + head, [tail[0]], tail[1:]) 

    return outer_iter([], [list_name[0]], list_name[1:]) 

下面是一个测试:

x = ['x','y', '{a', 'b}', 'c'] 
print reassemble(x) 

这是奇怪的结果:

['x', 'y', 'a,b', 'c'] #from the print inside "outer_iter" 
None     #from the print reassemble(x) 

注:我想保持代码的功能作为一个练习。

+1

没有运行代码,我可以看到一些潜在的问题。你的inner和outer_iter函数并不总是返回一个值 – spicavigo 2013-04-25 12:11:18

+0

这个问题是如何“过于本地化”的?我可以理解,我试图将这篇文章与Python对函数式编程的解释分开,可能被误解或可能违反Stackoverflow策略(这是有争议的),但这个问题仍然非常相关 - 请参阅Elazar的文章评论。 – TimY 2013-04-25 22:34:47

+0

这个问题绝对不是“过于本地化”。 – Elazar 2013-04-27 17:05:54

回答

4

您在else条款outer_iter处忘了return

在python中,不返回特定值的函数返回None

[编辑]

完整源:

def reassemble(list_name): 
    def inner_iter(head, tail): 
     if head[0][0] == '{': 
      new_head = [head[0] + ',' + tail[0]] 
      if tail[0][-1] == '}': 
       return [new_head[0][1:-1]], tail[1:] 
      else: 
       return inner_iter(new_head, tail[1:]) 
       #before the change, control reached here upon return 
     #control might still reach here, in case 'if' condition was not met 
     #implicitly "return None" 

    def outer_iter(corrected_list, head, tail): 
     if tail == []: 
      print corrected_list + head 
      return corrected_list + head 
     else: 
      if head[0][0] == '{': 
       head, tail = inner_iter(head, tail) 
       return outer_iter(corrected_list + head, [tail[0]], tail[1:]) 
       #before the change, control reached here upon return 
      else: 
       return outer_iter(corrected_list + head, [tail[0]], tail[1:]) 
       #before the change, control reached here upon return 
     #before the change, control reached here upon return 
     #implicitly "return None" 

    return outer_iter([], [list_name[0]], list_name[1:]) 

测试:

x = ['x','y', '{a', 'b}', 'c'] 
print reassemble(x) 

输出:

['x', 'y', 'a,b', 'c'] 
['x', 'y', 'a,b', 'c'] 
+0

我不确定我明白为什么如果函数总是收敛到尾部== [],为什么需要“返回”。如果需要“返回”来使该函数重复出现,为什么Python在第一种情况下仍然打印正确的值? Ps:谢谢 – TimY 2013-04-25 12:21:45

+2

我想我知道你的问题来自哪里:你习惯于函数语言,比如ML,Haskell等等。在这样的语言中,你不会写任何'return'语句。那么,Python不是一种功能语言,它只是支持函数式编程。 – Elazar 2013-04-25 12:25:19

+0

(第一条评论不是对你的评论的回答) 代码收敛,但是你会看到在这里发生了什么,如果你在函数的末尾放置'return None',并遵循通常的命令控制流程。 – Elazar 2013-04-25 12:27:26

1
def reassemble(list_name): 
    def inner_iter(head, tail): 
     if head[0][0] == '{': 
      new_head = [head[0] + ',' + tail[0]] 
      if tail[0][-1] == '}': 
       return [new_head[0][1:-1]], tail[1:] 
      else: 
       return inner_iter(new_head, tail[1:]) 

    def outer_iter(corrected_list, head, tail): 
     if tail == []: 
      print corrected_list + head 
      return corrected_list + head 
     else: 
      if head[0][0] == '{': 
       head, tail = inner_iter(head, tail) 
       return outer_iter(corrected_list + head, [tail[0]], tail[1:]) 

      else: 
       return outer_iter(corrected_list + head, [tail[0]], tail[1:]) 

    return outer_iter([], [list_name[0]], list_name[1:]) 



x = ['x','y', '{a', 'b}', 'c'] 

print reassemble(x) 

查看此Bunk中的运行代码http://codebunk.com/bunk#-It06-ImaZDpSrCrQSmM