Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中将函数更改为一行_Python_Python 3.x_Dictionary_Filter_Tree - Fatal编程技术网

如何在python中将函数更改为一行

如何在python中将函数更改为一行,python,python-3.x,dictionary,filter,tree,Python,Python 3.x,Dictionary,Filter,Tree,大家好,有人能帮我把这个函数改成pipline吗? 使用map、filter、lambda 您不需要写入def高度 您只需要与lambda map filter(PIPLINE)一起使用,因为只有在而不是t时才返回0,而在其他情况下返回1,您可以使用以下简单的行: def height (t): if not t: return 0 # (1) elif not t.nodes: return 1

大家好,有人能帮我把这个函数改成pipline吗? 使用map、filter、lambda

您不需要写入def高度
您只需要与lambda map filter(PIPLINE)一起使用

,因为只有在
而不是t
时才返回0,而在其他情况下返回1,您可以使用以下简单的行:

def height (t):
  if not t:
    return 0                                   # (1)
  elif not t.nodes:
    return 1                                   # (2)
  else:
    return 1 + max(height(n) for n in t.nodes) # (3)

为什么?单行并不等于更好、更快或更易读的代码我知道,但这是我的使命。如果可以接受,单行吗?
not t
t is None
的委婉说法吗,因此,将来使用
t.nodes
时不会产生
AttributeError
?是的,我只需要一行,并且也有一个递归,但我需要这样做Recursion@GalBirka但我要的不是def,你能在pipline做吗?
return 0 if not t else 1 if not t.nodes else 1 + max(height(n) for n in t.nodes)