Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.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_List_Recursion - Fatal编程技术网

Python 什么';我的代码怎么了?递归查找嵌套列表的和

Python 什么';我的代码怎么了?递归查找嵌套列表的和,python,list,recursion,Python,List,Recursion,我尝试创建一个函数,该函数以以下格式返回树列表中所有整数的总和: element 1 is an integer element 2 is another treelist or None element 3 is another treelist or None ex: [1,[1,None,None],None] 基本上,我希望我的函数将列表中的所有整数相加,然后返回2 这就是我到目前为止所做的 def sum_nested(t): sum = 0 for i in t:

我尝试创建一个函数,该函数以以下格式返回树列表中所有整数的总和:

element 1 is an integer
element 2 is another treelist or None
element 3 is another treelist or None

ex: [1,[1,None,None],None]
基本上,我希望我的函数将列表中的所有整数相加,然后返回2

这就是我到目前为止所做的

def sum_nested(t):
    sum = 0
    for i in t:
        if type(i) == type(None):
           sum += 0
        if type(i) == list:
           return sum_nested(i)
        else:
            sum = sum + i

    return sum  
但是,我得到了一个错误:

builtins.TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'
当我点击“无”类型时,似乎不知道该怎么办。有什么建议吗?

试试这个:

t = [1, [1, None, None], None]
def sum_nested(t):
    sum = 0
    for i in t:
        if type(i) is int:
            sum += 1
        elif type(i) == list:
           sum += sum_nested(i)

    return sum

print(sum_nested(t))
如果要测试某个内容是否为
None
,最短的形式是:

if i:
但在您的情况下,这并不是真正必要的,因为您无论如何都不会更改总和

尝试以下方法:

t = [1, [1, None, None], None]
def sum_nested(t):
    sum = 0
    for i in t:
        if type(i) is int:
            sum += 1
        elif type(i) == list:
           sum += sum_nested(i)

    return sum

print(sum_nested(t))
如果要测试某个内容是否为
None
,最短的形式是:

if i:
但在您的情况下,这并不是真正必要的,因为您无论如何都不会更改总和

那么:

def sum_nested(seq):

    # Don't use `sum` as a variable name, as it shadows the builtin `sum()`
    total = 0
    for item in seq:

        # This will allow you to sum *any* iterable (tuples, for example)
        if hasattr(item, '__iter__'): 

            # Use the `+=` syntactic sugar
            total += sum_nested(item) 
        else:

            # `None or 0` evaluates to 0 :)
            total += item or 0 

    return total
那么:

def sum_nested(seq):

    # Don't use `sum` as a variable name, as it shadows the builtin `sum()`
    total = 0
    for item in seq:

        # This will allow you to sum *any* iterable (tuples, for example)
        if hasattr(item, '__iter__'): 

            # Use the `+=` syntactic sugar
            total += sum_nested(item) 
        else:

            # `None or 0` evaluates to 0 :)
            total += item or 0 

    return total

sum+=0
等于
sum
,所以
if
block是多余的。
sum+=0
等于
sum
,所以
if
block是多余的。嘿,这很有效,谢谢你,但我不明白你是如何做出最后第二个陈述的“总计+=项目或0。它如何知道何时将0添加到总数中?@user305527:
None
'
{}
0
[]
,当然,
False
是Python中的“False-y”值。这意味着它们在逻辑表达式中的计算结果都为false
A或B
就是这样一个表达式。这解释得很清楚。嘿,这很有效,谢谢你,但我不明白的是你是如何做出最后第二句话“总计+=项目还是0。它如何知道何时将0添加到总计中?@user305527:
{}
0
[]
,当然
是“假”“Python中的值。这意味着它们在逻辑表达式中的计算结果都为false
A或B
就是这样一个表达式。这解释得很清楚。