函数返回的奇怪行为

时间:2018-05-19 19:47:04

标签: python python-3.x

我花了很多时间搜索所有"我的函数没有返回"和#34;嵌套字典搜索"消息,没有一个特别适用,也没有解决我的问题。

我创建了一个搜索嵌套字典并返回路径的函数。这很棒!我可以在函数中打印结果,但紧接在print下面的返回返回None。也许我已经看了太长时间,它可能就在我的脸前,但我只是没有看到这里的错误。这是我的完整代码:

def search(v, searchterm, vid, path=(),):
    if isinstance(v, dict):
        for k, v2 in v.items():
            p2 = path + ('{}'.format(k),)
            search(v2, searchterm, vid, p2)
    else:
      if searchterm in v:
         a = {}
         a[0] = path
         a[1] = v[vid]
         print(a)
         return(a)


def main():

    mydata = {}
    mydata[1] = {}
    mydata[1][1] = 'data-1-1','reason-1-1','notes-1-1'
    mydata[1][2] = 'data-1-2','reason-1-2','notes-1-2'
    mydata[1][3] = 'data-1-3','reason-1-3','notes-1-3'
    mydata[1][4] = 'data-1-4','reason-1-4','notes-1-4'
    mydata[1][5] = 'data-1-5','reason-1-5','notes-1-5'
    mydata[1][6] = 'data-1-6','reason-1-6','notes-1-6'
    mydata[1][7] = 'data-1-7','reason-1-7','notes-1-7'
    mydata[1][8] = 'data-1-8','reason-1-8','notes-1-8'
    mydata[1][9] = 'data-1-9','reason-1-9','notes-1-9'
    mydata[1][10] = 'data-1-10','reason-1-10','notes-1-10'
    mydata[2] = {}
    mydata[2][1] = 'data-2-1','reason-2-1','notes-2-1'
    mydata[2][2] = 'data-2-2','reason-2-2','notes-2-2'
    mydata[2][3] = 'data-2-3','reason-2-3','notes-2-3'
    mydata[2][4] = 'data-2-4','reason-2-4','notes-2-4'
    mydata[2][5] = 'data-2-5','reason-2-5','notes-2-5'
    mydata[2][6] = 'data-2-6','reason-2-6','notes-2-6'
    mydata[2][7] = 'data-2-7','reason-2-7','notes-2-7'
    mydata[2][8] = 'data-2-8','reason-2-8','notes-2-8'
    mydata[2][9] = 'data-2-9','reason-2-9','notes-2-9'
    mydata[2][10] = 'data-2-10','reason-2-10','notes-2-10'

    b = search(mydata,'reason-2-4', 2)
    print(b)

if __name__ == '__main__':
   main()

结果:

{0: ('2', '4'), 1: 'notes-2-4'}
None

您可以在函数中看到打印效果很好但返回并从main打印返回None。我已经用Python编程了几年了,有很多函数,类和方法都写了回报但是这个让我感到困惑。

2 个答案:

答案 0 :(得分:4)

您正在进行递归调用,并且在嵌套调用中发出print语句。但是,search的返回值未被使用,这就是为什么它永远不会到达main函数。

下面,我添加了一个已检查的nested变量,如果找到了实际返回的任何内容。

def search(v, searchterm, vid, path=(),):
  if isinstance(v, dict):
    for k, v2 in v.items():
      p2 = path + ('{}'.format(k),)
      nested = search(v2, searchterm, vid, p2)
      if nested:
        # before, nothing was ever returned here
        return nested  
    else:
      if searchterm in v:
         a = {}
         a[0] = path
         a[1] = v[vid]
         print(a)
         return(a)

没有关系,但在这里你可以充分利用python

的强大字典文字
  mydata = {
    1: {
      1: ('data-1-1', 'reason-1-1', 'notes-1-1'),
      2: ('data-1-2', 'reason-1-2', 'notes-1-2')
    2: {
      1: ('data-2-1', 'reason-2-1', 'notes-2-1'),
      2: ('data-2-2', 'reason-2-2', 'notes-2-2')
    }

此外,如果您的所有词典键都是int,那么您也可以使用list

答案 1 :(得分:2)

在search()中的3条路径中,只返回1:

def search(v, searchterm, vid, path=(),):
    if isinstance(v, dict):
        # Path 1
        for k, v2 in v.items():
            p2 = path + ('{}'.format(k),)
            search(v2, searchterm, vid, p2)
        # Nothing returned here
    else:
      if searchterm in v:
         # Path 2
         a = {}
         a[0] = path
         a[1] = v[vid]
         print(a)
         return(a)
      else:
         # Path 3
         # Nothing returned here

路径1调用路径2,它解释了为什么打印了一些东西但没有返回到main()。